gpt4 book ai didi

python - 我如何要求元素在 XSD 架构中具有一组属性或另一组属性?

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:34 25 4
gpt4 key购买 nike

我正在处理一个 XML 文档,其中标记必须具有一组属性或另一组属性。例如,它需要看起来像 <tag foo="hello" bar="kitty" /><tag spam="goodbye" eggs="world" />例如

<root>
<tag foo="hello" bar="kitty" />
<tag spam="goodbye" eggs="world" />
</root>

所以我有一个 XSD 模式,我在其中使用 xs:choice在两个不同的属性组之间进行选择的元素:

<xsi:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="tag">
<xs:choice>
<xs:complexType>
<xs:attribute name="foo" type="xs:string" use="required" />
<xs:attribute name="bar" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType>
<xs:attribute name="spam" type="xs:string" use="required" />
<xs:attribute name="eggs" type="xs:string" use="required" />
</xs:complexType>
</xs:choice>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xsi:schema>

但是,当使用 lxml 时尝试加载此模式时,出现以下错误:

>>> from lxml import etree  
>>> etree.XMLSchema( etree.parse("schema_choice.xsd") )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "xmlschema.pxi", line 85, in lxml.etree.XMLSchema.__init__ (src/lxml/lxml.etree.c:118685)
lxml.etree.XMLSchemaParseError: Element '{http://www.w3.org/2001/XMLSchema}element': The content is not valid. Expected is (annotation?, ((simpleType | complexType)?, (unique | key | keyref)*))., line 7

因为错误与我的 xs:choice 的位置有关元素,我试过将它放在不同的地方,但无论我尝试什么,我似乎都无法使用它来定义一个标签,使其具有一组属性( foobar )或另一组( spameggs )。

这可能吗?如果是这样,那么正确的语法是什么?

最佳答案

不幸的是,无法在 XML 模式中对属性使用选择。您将需要在更高级别实现此验证。

关于python - 我如何要求元素在 XSD 架构中具有一组属性或另一组属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2806880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com