2015-09-19 103 views
2

我認爲這應該正在工作......但它不是!這裏是我的XSD:XSD 1.1斷言子元素

<xsd:schema targetNamespace="http://www.loc.gov/MARC21/slim" xmlns="http://www.loc.gov/MARC21/slim" 
     xmlns:xerces="http://xerces.apache.org" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.1" xml:lang="en"> 

     <xsd:element name="record" type="recordType" nillable="true" id="record.e"/> 

     <xsd:element name="collection" type="collectionType" nillable="true" id="collection.e"/> 

     <xsd:complexType name="collectionType" id="collection.ct"> 
      <xsd:sequence minOccurs="0" maxOccurs="unbounded"> 
       <xsd:element ref="record"/> 
      </xsd:sequence> 
     </xsd:complexType> 

     <xsd:complexType name="recordType" id="record.ct"> 
      <xsd:sequence minOccurs="0">   
       <xsd:element name="datafield" type="dataFieldType" minOccurs="0" maxOccurs="unbounded"/> 
      </xsd:sequence> 
      <xsd:attribute name="type" type="recordTypeType" use="optional"/> 
      <xsd:assert test="count(./datafield[@tag = '100']) = 1" 
       xerces:message="A record must only have up to one 1XX field"/> 
     </xsd:complexType> 
     <xsd:simpleType name="recordTypeType" id="type.st"> 
      <xsd:restriction base="xsd:NMTOKEN"> 
       <xsd:enumeration value="Bibliographic"/> 
       <xsd:enumeration value="Authority"/> 
       <xsd:enumeration value="Holdings"/> 
       <xsd:enumeration value="Classification"/> 
       <xsd:enumeration value="Community"/> 
      </xsd:restriction> 
     </xsd:simpleType> 
     <xsd:complexType name="dataFieldType" id="datafield.ct"> 
      <xsd:attribute name="tag" type="tagDataType" use="required"/> 
      <xsd:attribute name="ind1" type="indicatorDataType" use="required"/> 
      <xsd:attribute name="ind2" type="indicatorDataType" use="required"/> 

      <xsd:assert 
       test="not((@tag = '100' or @tag = '110' or @tag = '111' or @tag = '130') and not(@ind2 = ' '))" 
       xerces:message="1XX fields must have a blank 2nd indicator"> 
       <xsd:annotation> 
        <xsd:documentation xml:lang="en">This assertation ensures that the second indicator 
         of a 1XX field is blank.</xsd:documentation> 
       </xsd:annotation> 
      </xsd:assert> 
     </xsd:complexType> 
     <xsd:simpleType name="tagDataType" id="tag.st"> 
      <xsd:restriction base="xsd:string"> 
       <xsd:whiteSpace value="preserve"/> 
       <xsd:pattern 
        value="010|020|040|042|050|082|100|110|111|130|245|246|250|260|264|300|336|337|338|500|505|520|490|600|610|611|630|650|655|700|710|711|730|830|856" 
       /> 
      </xsd:restriction> 
     </xsd:simpleType> 
     <xsd:simpleType name="indicatorDataType" id="ind.st"> 
      <xsd:restriction base="xsd:string"> 
       <xsd:whiteSpace value="preserve"/> 
       <xsd:pattern value="[\da-z ]{1}"/> 
      </xsd:restriction> 
     </xsd:simpleType> 
    </xsd:schema> 

這裏是我的XML數據:

<collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.loc.gov/MARC21/slim file:/Users/NetanelGanin/Desktop/Test.xsd" 
    xmlns="http://www.loc.gov/MARC21/slim"> 

    <record type="Bibliographic"> 
     <datafield tag="010" ind1=" " ind2=" "/> 
     <datafield tag="020" ind1=" " ind2=" "/> 
     <datafield tag="040" ind1=" " ind2=" "/> 
     <datafield tag="042" ind1=" " ind2=" "/> 
     <datafield tag="050" ind1="0" ind2="0"/> 
     <datafield tag="082" ind1="0" ind2="0"/> 
     <datafield tag="100" ind1="1" ind2=" "/> 
     <datafield tag="245" ind1="1" ind2="0"/> 
    </record> 
</collection> 

第二斷言工作正常! (每當我測試它通過使@ind2不是空格),但第一個斷言只是不斷失敗。我試過甚至只是寫:

<xsd:assert test="./datafield"/> 

只是說,「嘿,如果有,甚至被稱爲數據域的子元素,然後通過斷言,」它是失敗!

想法?

回答

3

這是一個命名空間問題,它已在下面的XSD中修復。

此外,假設斷言消息正確表示您的意圖,第一個斷言條件應該是&lt;= 1。請注意,./是多餘的,可以簡化。

這裏是正確的第一個斷言:

<xsd:assert test="count(slim:datafield[@tag = '100']) &lt;= 1" 
       xerces:message="A record must only have up to one 1XX field"/> 

下面是完整的,正確的XSD:

<?xml version="1.0" encoding="UTF-8"?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      targetNamespace="http://www.loc.gov/MARC21/slim" 
      xmlns:slim="http://www.loc.gov/MARC21/slim" 
      xmlns:xerces="http://xerces.apache.org" 
      elementFormDefault="qualified" attributeFormDefault="unqualified" 
      xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" 
      vc:minVersion="1.1"> 

    <xsd:element name="record" type="slim:recordType" 
    nillable="true" id="record.e"/> 
    <xsd:element name="collection" type="slim:collectionType" 
    nillable="true" id="collection.e"/> 
    <xsd:complexType name="collectionType" id="collection.ct"> 
    <xsd:sequence minOccurs="0" maxOccurs="unbounded"> 
     <xsd:element ref="slim:record"/> 
    </xsd:sequence> 
    </xsd:complexType> 
    <xsd:complexType name="recordType" id="record.ct"> 
    <xsd:sequence minOccurs="0">   
     <xsd:element name="datafield" type="slim:dataFieldType" 
     minOccurs="0" maxOccurs="unbounded"/> 
    </xsd:sequence> 
    <xsd:attribute name="type" type="slim:recordTypeType" 
     use="optional"/> 
    <xsd:assert test="count(slim:datafield[@tag = '100']) &lt;= 1" 
       xerces:message="A record must only have up to one 1XX field"/> 
    </xsd:complexType> 
    <xsd:simpleType name="recordTypeType" id="type.st"> 
    <xsd:restriction base="xsd:NMTOKEN"> 
     <xsd:enumeration value="Bibliographic"/> 
     <xsd:enumeration value="Authority"/> 
     <xsd:enumeration value="Holdings"/> 
     <xsd:enumeration value="Classification"/> 
     <xsd:enumeration value="Community"/> 
    </xsd:restriction> 
    </xsd:simpleType> 
    <xsd:complexType name="dataFieldType" id="datafield.ct"> 
    <xsd:attribute name="tag" type="slim:tagDataType" use="required"/> 
    <xsd:attribute name="ind1" type="slim:indicatorDataType" use="required"/> 
    <xsd:attribute name="ind2" type="slim:indicatorDataType" use="required"/> 
    <xsd:assert 
     test="not((@tag = '100' or @tag = '110' 
       or @tag = '111' or @tag = '130') and not(@ind2 = ' '))" 
     xerces:message="1XX fields must have a blank 2nd indicator"> 
     <xsd:annotation> 
     <xsd:documentation xml:lang="en"> 
      This assertation ensures that the second indicator 
      of a 1XX field is blank. 
     </xsd:documentation> 
     </xsd:annotation> 
    </xsd:assert> 
    </xsd:complexType> 
    <xsd:simpleType name="tagDataType" id="tag.st"> 
    <xsd:restriction base="xsd:string"> 
     <xsd:whiteSpace value="preserve"/> 
     <xsd:pattern 
     value="010|020|040|042|050|082|100|110|111|130|245|246| 
       250|260|264|300|336|337|338|500|505|520|490|600| 
       610|611|630|650|655|700|710|711|730|830|856"/> 
    </xsd:restriction> 
    </xsd:simpleType> 
    <xsd:simpleType name="indicatorDataType" id="ind.st"> 
    <xsd:restriction base="xsd:string"> 
     <xsd:whiteSpace value="preserve"/> 
     <xsd:pattern value="[\da-z ]{1}"/> 
    </xsd:restriction> 
    </xsd:simpleType> 
</xsd:schema> 
+0

男孩是有雞蛋在我的臉上!我必須再次閱讀名稱空間......非常感謝! – user3530461

+0

不,很難知道什麼是正確的,什麼是不存在問題的時候。我已經刪除了雞蛋/臉部相關的方面。這是一個很好的問題。 – kjhughes