XSD 1.1声明以计数和比较元素 [英] XSD 1.1 Assert to Count and Compare Elements
问题描述
我目前有一个XSD文件,该文件控制着对相应XML文件的验证等,并且我想进行控制(最好使用assert命令而不是XLST [因为我对此不了解])并且能够确保abc:Country标签与abc:AccountNumber标签的数量相同,因为其中一个应与另一个相对应
I currently have an XSD file which controls the validation etc towards my corresponding XML file, and I would like to control (preferably using an assert command rather than XLST [as I have no prior knowledge of this]) and be able to ensure there are the same number of abc:Country tags to abc:AccountNumber tags, as one should correspond to the other
<abc:Account>
<abc:Individual>
<abc:Country>Germany</abc:Country>
<abc:Country>Australia</abc:Country>
<abs:AccountNumber issuedBy="DE">123456</abs:AccountNumber>
<abs:AccountNumber issuedBy="AU">654321</abs:AccountNumber>
</abc:Individual>
</abc:Account>
请有人可以用我可以使用的assert命令帮助我执行此验证吗?
Please can someone help me out with the assert command I can use perform this validation?
我尝试了以下操作,但无济于事...
I have tried the following to no avail...
<xsd:assert test="if (count (abc:Account/abc:Individual/abc:Country) eq (count (abc:Account/abc:Individual/AccountNumber))) then true() else false() "/>
或这个......
<xsd:assert test="count (abc:Account/abc:Individual/abc:Country) eq count (abc:Account/abc:Individual/AccountNumber)"/>
我认为使用XSD 1.1可以做到这一点吗?
I presume this is doable using XSD 1.1?
任何帮助将不胜感激....谢谢
any help will be greatly appreciated.... thanks
推荐答案
我认为在abc:Individual
元素的类型定义中使用断言是最有意义的,那么断言就是:
I think it makes most sense to have the assert in the type definition for the abc:Individual
element, then the assert is simply:
count(abc:Country) eq count(abc:AccountNumber)
完整的架构是这样的.为简单起见,我将AccountNumber
保留在abc
命名空间中,但是可以很容易地用引用替换它.
The complete schema is like so. For simplicity I kept AccountNumber
in the abc
namespace, but it can be easily replaced with a reference otherwise.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:abc="http://www.example.com/abc"
targetNamespace="http://www.example.com/abc"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
vc:minVersion="1.1">
<xs:element name="Account">
<xs:complexType>
<xs:sequence>
<xs:element ref="abc:Individual" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Individual">
<xs:complexType>
<xs:sequence>
<xs:element ref="abc:Country" maxOccurs="unbounded" />
<xs:element ref="abc:AccountNumber" maxOccurs="unbounded" />
</xs:sequence>
<xs:assert test="count(abc:Country) eq count(abc:AccountNumber)"/>
</xs:complexType>
</xs:element>
<xs:element name="Country" type="xs:string"/>
<xs:element name="AccountNumber">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="issuedBy" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
除了将abs
更改为abc
之外,原始文档还可以针对架构成功验证,即:
Apart from changing abs
to abc
, the original document validates successfully against the schema, i.e.:
<?xml version="1.0" encoding="UTF-8"?>
<abc:Account
xmlns:abc="http://www.example.com/abc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.com/abc test.xsd">
<abc:Individual>
<abc:Country>Germany</abc:Country>
<abc:Country>Australia</abc:Country>
<abc:AccountNumber issuedBy="DE">123456</abc:AccountNumber>
<abc:AccountNumber issuedBy="AU">654321</abc:AccountNumber>
</abc:Individual>
</abc:Account>
这篇关于XSD 1.1声明以计数和比较元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!