英文:
XSD Multiple elements with the same name but different attribute value
问题
我尝试创建以下内容的XML模式:
<SomeApp>
<Org>Dataset</Org>
<Name>Foo</Name>
<Version>1.00</Version>
<Source>25839</Source>
<UniversalID>867-5309</UniversalID>
<TestField Name="Price">7.00</TestField>
<TestField Name="Level">5.00</TestField>
<TestField Name="new">false</TestField>
<TestField Name="Description">Bar</TestField>
</SomeApp>
当解析器遇到<TestField>
元素时,我遇到了问题。我可以像这样处理一个这样的元素…
<xs:element name="TestField" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="Name" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
但是解析器不允许下一个同名元素,再次定义它(并将simpleContent设置为匹配下一个数据类型)也无济于事。这是我尝试的XSD格式之一,只是为了通过前两个TestField元素:
<xs:complexType name="TestFieldType">
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="Name" type="xs:string" use="required"/>
<xs:simpleType>
<xs:extension base="xs:string">
<xs:enumeration value="Price"/>
<xs:enumeration value="Level"/>
</xs:extension>
</xs:simpleType>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:element name="TestField" type="TestFieldType"/>
我会感激任何帮助!
英文:
I'm trying to create an XML schema for the following:
<SomeApp>
<Org>Dataset</Org>
<Name>Foo</Name>
<Version>1.00</Version>
<Source>25839</Source>
<UniversalID>867-5309</UniversalID>
<TestField Name="Price">7.00</TestField>
<TestField Name="Level">5.00</TestField>
<TestField Name="new">false</TestField>
<TestField Name="Description">Bar</TestField>
</SomeApp>
I'm OK until the parser reaches the <TestField>
elements. I can handle one of these elements like so...
<xs:element name="TestField" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="Name" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
...but of course the parser doesn't allow the next element of the same name, and defining it again (with the simpleContent set to match the next data type) doesn't do the trick either. Here is one of the XSD formats I've tried, just to pass the first two TestField elements:
<xs:complexType name="TestFieldType">
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="Name" type="xs:string" use="required"/>
<xs:simpleType>
<xs:extension base="xs:string">
<xs:enumeration value="Price"/>
<xs:enumeration value="Level"/>
</xs:extension>
</xs:simpleType>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:element name="TestField" type="TestFieldType"/>
I would be grateful for any help!
答案1
得分: 3
下面是您要的翻译:
XML
<SomeApp>
<Org>数据集</Org>
<Name>Foo</Name>
<Version>1.00</Version>
<Source>25839</Source>
<UniversalID>867-5309</UniversalID>
<TestField Name="Price">7.00</TestField>
<TestField Name="Level">5.00</TestField>
<TestField Name="Description">Bar</TestField>
</SomeApp>
XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="SomeApp">
<xs:complexType>
<xs:sequence>
<xs:element ref="Org"/>
<xs:element ref="Name"/>
<xs:element ref="Version"/>
<xs:element ref="Source"/>
<xs:element ref="UniversalID"/>
<xs:element maxOccurs="unbounded" ref="TestField"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Org" type="xs:string"/>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Version" type="xs:decimal"/>
<xs:element name="Source" type="xs:integer"/>
<xs:element name="UniversalID" type="xs:string"/>
<xs:element name="TestField">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="Name" use="required" type="TestFieldType"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:simpleType name="TestFieldType">
<xs:restriction base="xs:string">
<xs:enumeration value="Price"/>
<xs:enumeration value="Level"/>
<xs:enumeration value="Description"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
英文:
Check it out.
XML
<SomeApp>
<Org>Dataset</Org>
<Name>Foo</Name>
<Version>1.00</Version>
<Source>25839</Source>
<UniversalID>867-5309</UniversalID>
<TestField Name="Price">7.00</TestField>
<TestField Name="Level">5.00</TestField>
<TestField Name="Description">Bar</TestField>
</SomeApp>
XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="SomeApp">
<xs:complexType>
<xs:sequence>
<xs:element ref="Org"/>
<xs:element ref="Name"/>
<xs:element ref="Version"/>
<xs:element ref="Source"/>
<xs:element ref="UniversalID"/>
<xs:element maxOccurs="unbounded" ref="TestField"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Org" type="xs:string"/>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Version" type="xs:decimal"/>
<xs:element name="Source" type="xs:integer"/>
<xs:element name="UniversalID" type="xs:string"/>
<xs:element name="TestField">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="Name" use="required" type="TestFieldType"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:simpleType name="TestFieldType">
<xs:restriction base="xs:string">
<xs:enumeration value="Price"/>
<xs:enumeration value="Level"/>
<xs:enumeration value="Description"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
答案2
得分: 1
有一个规则叫做ElementDeclarationsConsistent
,它指出如果两个元素是彼此的兄弟并且具有相同的名称,那么它们必须具有相同的类型。
我不确定您想要施加的确切约束是什么,但是例如,如果您想要表达对于任何TestField
,如果@Name
的值是'Price',那么该值必须是xs:decimal
,那么这需要 XSD 1.1 的断言:
<xs:assert test="@Name ne 'Price' or string(.) castable as xs:decimal"/>
同样,如果您想要断言第一个TestField必须具有@Name="Price"
,那么这需要在SomeApp
元素上加入一个断言:
<xs:assert test="TestField[1]/@Name = 'Price'"/>
英文:
There's a rule ElementDeclarationsConsistent
which says that if two elements are siblings of each other and have the same name, then they must have the same type.
I'm not sure exactly what constraints you want to impose, but if for example you want to say that for any TestField
, if the value of @Name
is 'Price' then the value must be xs:decimal
, then this needs XSD 1.1 assertions:
<xs:assert test="@Name ne 'Price' or string(.) castable as xs:decimal"/>
Similarly if you want to assert that the first TestField must have @Name="Price"
then this needs an assertion on the SomeApp
element:
<xs:assert test="TestField[1]/@Name = 'Price'"/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论