解析 XSD 文件以获取名称和描述

Parse XSD file to get names and descriptions

我正在尝试解析这个 XSD 文件,目前正在尝试 python,以获取元素的名称和数据的描述。

示例XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" version="07112016">
    <xs:annotation>
        <xs:documentation>Level 1: top level of Procurement Data Standard for a procurement instrument document.</xs:documentation>
    </xs:annotation>
    <xs:element name="ProcurementDocument">
        <xs:annotation>
            <xs:documentation>The root element for any procurement instrument document</xs:documentation>

这里它会抓取 name: ProcurementDocumentdesc:The root element for any procurement instrument document

here is more data where I tried to pull it using regular expression. I had more success when I minified 它全部在一条线上,但仍然没有拉出每个实例。

这是我的完整代码,我试图用它从我的缩小 XSD 中获取所有案例,但只找到了我试图找到的 ~1500 个案例中的 ~120 个。

import re
import pandas as pd

df = pd.DataFrame({'Names': [ ], 'Description': [ ]})

search_str = r"name=\"(?P<name>\w+)\"\>[\w\<\/\.\>\d:]+documentation\>(?P<desc>[\w\s\.]+)\<\/"
file1 = 'mini_text.xml'

with open(file1, 'r') as f:
    xml_string = f.read()
idx = 0
for m in re.finditer(search_str, xml_string):
    df.loc[idx, 'Names'] = m.group('name')
    df.loc[idx, 'Description'] = m.group('desc')
    idx += 1

df.to_csv('output.txt', index=False, sep="\t")

您应该避免使用正则表达式解析 xml/html/json,因为正则表达式不足以解析嵌套结构。

您的正则表达式未捕获文本中所有名称和描述实例的原因是,您为捕获描述选择的字符集 [\w\s\.]+ 不够,因为描述中存在如下字符括号 (see list) 由于进一步的预期匹配失败。尝试将 [\w\s\.]+ 更改为 .+? 然后它将起作用。在下面检查您更新的 regex101 演示 link.

Working Demo of your modified regex

编辑:显示如何使用 Beautiful Soup 解析您的 xml 以获得所需信息的示例示例

import re
from bs4 import BeautifulSoup

data = '''<xs:element name="ProductDescription"><xs:annotation><xs:documentation>Provides the description of the product</xs:documentation></xs:annotation><xs:complexType><xs:sequence><xs:element name="ProductName"><xs:annotation><xs:documentation>Provides a name for the product. (see list)</xs:documentation></xs:annotation><xs:simpleType><xs:restriction base="xs:token"><xs:enumeration value="Barbie Doll"/><xs:enumeration value="Ken Doll"/></xs:restriction></xs:simpleType></xs:element><xs:element name="ProductSize"><xs:annotation><xs:documentation>Describes the size of the product. (see list)</xs:documentation></xs:annotation><xs:simpleType><xs:restriction base="xs:token"><xs:enumeration value="Small"/><xs:enumeration value="Medium"/><xs:enumeration value="Large"/><xs:enumeration value="Dayum"/></xs:restriction></xs:simpleType></xs:element></xs:sequence></xs:complexType></xs:element>'''

soup = BeautifulSoup(data)

for element in soup.find_all('xs:element'):
 print(element['name'])  # prints name attribute value
 print(element.find('xs:documentation').get_text(),'\n')  # prints inner text of xs:documentation tag

打印您想要的名称和描述,

ProductDescription
Provides the description of the product

ProductName
Provides a name for the product. (see list)

ProductSize
Describes the size of the product. (see list)