2015-09-19 104 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