2017-01-11 45 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謝謝你曾經這麼多!快速和簡潔的迴應,就像我想要的那樣工作!謝謝!!! :) –