【问题标题】:XSD 1.1 assert on child elementXSD 1.1 断言子元素
【发布时间】:2015-09-19 01:27:42
【问题描述】:

我认为这应该工作......但它不是!这是我的 XSD:

 <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"/>

只是说,“嘿,如果有一个名为 datafield 的子元素,然后传递断言,”它失败了!

想法?

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

    这是一个命名空间问题,已在以下 XSD 中修复。

    另外,假设断言消息正确地代表了您的意图,第一个断言条件应该是&amp;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>
    

    【讨论】:

    • 男孩我脸上有鸡蛋!!我必须再次阅读名称空间...非常感谢!
    • 不,当出现问题时,很难知道什么是正确的,什么是错误的。我已经删除了与鸡蛋/面部相关的方面。这是个好问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多