首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >解析XSD文件以获取名称和描述

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

Stack Overflow用户
提问于 2019-04-12 03:01:54
回答 1查看 1.9K关注 0票数 1

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

示例XSD:

代码语言:javascript
复制
<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是我尝试使用正则表达式拉取的更多数据。我有更多的成功,当我minified它是在一行,但仍然没有拉每个实例。

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

代码语言:javascript
复制
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")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-12 03:28:15

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

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

xml :演示如何使用 解析以获取所需信息的示例

代码语言:javascript
复制
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

打印你想要的名字和描述,

代码语言:javascript
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55639506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档