我目前有一个XSD文件控制对我的相应XML文件的验证等,我想控制(最好使用断言命令而不是XLST [因为我之前没有这个知识])并且能够确保有相同数量的abc:country标签到abc:AccountNumber标签,因为一个应该对应另一个
<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>
请有人帮我解决我可以使用的断言命令执行此验证吗?
我尝试过以下无济于事......
<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吗?
任何帮助将不胜感激....谢谢
答案 0 :(得分:1)
我认为在abc:Individual
元素的类型定义中使用assert是最有意义的,那么断言就是:
count(abc:Country) eq count(abc:AccountNumber)
完整的架构就是这样。为简单起见,我将AccountNumber
保留在abc
命名空间中,但是否则可以很容易地替换为引用。
<?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
之外,原始文档会根据模式成功验证,即:
<?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>