2017-01-11 47 views
2

我目前有一个XSD文件,它控制验证等我的相应的XML文件,我想控制(最好使用断言命令而不是XLST [因为我没有先验知识这种]),并能保证有相同数量的ABC:国家标签为abc:账户号码标签,作为一个应该对应于其他XSD 1.1声明计数和比较元素

<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是可行的?

任何帮助将不胜感激....谢谢

回答

1

我觉得最有意义有对abc:Individual元素类型定义的断言,那么断言很简单:

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> 

除了改变absabc,原稿成功校验架构,即:

<?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> 
+0

Ghislain的Fourny谢谢你曾经这么多!快速和简洁的回应,就像我想要的那样工作!谢谢!!! :) –