我有一些XML,我正在为它编写一个验证器工具,这样我们的客户端就可以测试它们的示例提要了,但是我对XSD非常陌生。我已经到了基于cookie切割器示例验证一切都是正确的地步,所以我想抛出一些其他的场景。我想出的第一个方法是在基节点中有不同的元素顺序。
示例:我使用下面的complexType来验证每个事务节点
<xs:complexType name="transactionType">
<xs:sequence>
<xs:element type="xs:string" name="id"/>
<xs:element type="xs:string" name="type"/>
<xs:element type="xs:float" name="amount"/>
<xs:element type="xs:string" name="description"/>
<xs:element type="xs:string" name="status"/>
<xs:choice>
<xs:element name="transacted_on" type="xs:date"/>
<xs:element name="transacted_at" type="xs:date"/>
</xs:choice>
<xs:choice>
<xs:element name="posted_on" type="xs:date"/>
<xs:element name="posted_at" type="xs:date"/>
</xs:choice>
</xs:sequence>
</xs:complexType>下面的示例节点按正确的顺序排列,从而正确验证
<transaction>
<id>20150617-123456</id>
<type>DEBIT</type>
<amount>17.44</amount>
<description>Debit Card: CAFE 06/16/15</description>
<status>POSTED</status>
<transacted_on>2015-06-17</transacted_on>
<posted_on>2015-06-16</posted_on>
</transaction>但我想让元素按任何顺序排列。因此,下面这样的内容将正确地验证。其思想是,所有元素都存在,并且可以按任何顺序排列。
<transactions>
<transaction>
<amount>17.44</amount>
<id>20150617-123456</id>
<type>DEBIT</type>
<description>Debit Card: CAFE 06/16/15</description>
<status>POSTED</status>
<transacted_on>2015-06-17</transacted_on>
<posted_on>2015-06-16</posted_on>
</transaction>
<transaction>
<id>20150617-123456</id>
<type>CREDIT</type>
<amount>17.44</amount>
<description>VISA Card: payment</description>
<status>POSTED</status>
<transacted_on>2015-06-17</transacted_on>
<posted_on>2015-06-16</posted_on>
</transaction>
</transactions>我做了一些研究,很快就被指向使用xs:all元素,它应该做我想做的事情。因此,我将我的模式更改为使用xs:all而不是xs:sequence。
<xs:complexType name="transactionType">
<xs:all>
<xs:element type="xs:string" name="id"/>
<xs:element type="xs:string" name="type"/>
<xs:element type="xs:float" name="amount"/>
<xs:element type="xs:string" name="description"/>
<xs:element type="xs:string" name="status"/>
<xs:choice>
<xs:element name="transacted_on" type="xs:date"/>
<xs:element name="transacted_at" type="xs:date"/>
</xs:choice>
<xs:choice>
<xs:element name="posted_on" type="xs:date"/>
<xs:element name="posted_at" type="xs:date"/>
</xs:choice>
</xs:all>
</xs:complexType>在进行此更改后运行验证时,仍然会出现一个错误:
不需要此元素。预期是。
我在这里错过了什么?
发布于 2015-07-10 20:32:02
XML不允许xs:choice是xs:all的子结构。
解决办法:
xs:choice元素周围添加一个包装器元素。xs:all,通过xs:sequence进行固定的排序。我建议在实践中,xs:sequence可以很好地工作,避免这个问题以及xs:all经常出现的其他问题,包括违反唯一粒子原理。
https://stackoverflow.com/questions/31349025
复制相似问题