xsd唯一选择器路径:排除特定元素

时间:2012-07-27 08:13:21

标签: xml xpath xsd selector unique

我跟随xml和xsd作为我的问题的基本示例:

XML

  <parentgroup>

    <parent id="1">
      <child id="2" />
      <child id="3" />
      <childgroup id="4">
        <child id="5" />
        <child id="2" />
      </childgroup>
      <child id="5" />
    </parent>


    <parent id="2">
      <childgroup id="33">
        <child id="1" />
        <child id="4" />
      </childgroup>
      <child id="1" />
      <child id="5" />
      <childgroup id="4">
        <child id="1" />
        <child id="6" />
      </childgroup>
    </parent>

  </parentgroup>

XSD

<xsd:element name="parentgroup" type="parentgroup">
    <xsd:unique name="UniqueParentId">
      <xsd:selector xpath="child::parent"></xsd:selector>
      <xsd:field xpath="@id"></xsd:field>
    </xsd:unique>
  </xsd:element>

  <xsd:complexType name="parentgroup">
    <xsd:sequence maxOccurs="unbounded">
      <xsd:element name="parent" type="parent" maxOccurs="unbounded" minOccurs="1">

        <xsd:unique name="UniqueChildId">
          <xsd:selector xpath=".//*"></xsd:selector>
          <xsd:field xpath="@id"></xsd:field>
        </xsd:unique>

      </xsd:element>
    </xsd:sequence>
    <xsd:attribute name="id" type="xsd:int" />
  </xsd:complexType>

  <xsd:complexType name="parent">
    <xsd:choice maxOccurs="unbounded" minOccurs="1">
      <xsd:element name="child" type="child" maxOccurs="unbounded" minOccurs="1"></xsd:element>
      <xsd:element name="childgroup" type="childgroup" maxOccurs="unbounded" minOccurs="1"></xsd:element>

    </xsd:choice>

    <xsd:attribute name="id" type="xsd:int" />
  </xsd:complexType>

  <xsd:complexType name="childgroup">
    <xsd:sequence maxOccurs="unbounded">
      <xsd:element name="child" type="child" maxOccurs="unbounded" minOccurs="1"></xsd:element>

    </xsd:sequence>
    <xsd:attribute name="id" type="xsd:int" />
  </xsd:complexType>

  <xsd:complexType name="child">
    <xsd:attribute name="id" type="xsd:int" />
  </xsd:complexType>

任务

  1. 所有父母都有一个独特的“id”attr。并且没有孩子拥有相同的“id”attr。喜欢他的父母。
  2. 父级中的Childs和Childgroups具有唯一的“id”attr。排除Childgroup中的Childs。 (ids在第一深度加父母时是唯一的)
  3. Childgroup中的孩子具有唯一的“id”attr。 (因此,允许子组中的子项的id等于同一父项中子组的子ID,但不允许它对于自己的子组的id是euqal,如2. point [(ids是独特的第一深度加父母)])
  4. 1。问题

    第1点和第2点是清楚的,但我不知道如何排除childgroup的后代,我试过跟随但总是得到xpath语法错误

    第一次尝试
         <xsd:element name="parent" type="parent" maxOccurs="unbounded" minOccurs="1">
    
            <xsd:unique name="UniqueChildId">
              <xsd:selector xpath=".//*[not(childgroup)]"></xsd:selector>
              <xsd:field xpath="@id"></xsd:field>
            </xsd:unique>
    
          </xsd:element>
    
    第二次尝试
         <xsd:element name="parent" type="parent" maxOccurs="unbounded" minOccurs="1">
    
            <xsd:unique name="UniqueChildId">
              <xsd:selector xpath=".//*[not(child::childgroup)]"></xsd:selector>
              <xsd:field xpath="@id"></xsd:field>
            </xsd:unique>
    
          </xsd:element>
    
    第三次尝试
         <xsd:element name="parent" type="parent" maxOccurs="unbounded" minOccurs="1">
    
            <xsd:unique name="UniqueChildId">
              <xsd:selector xpath="child::*[not(childgroup)]"></xsd:selector>
              <xsd:field xpath="@id"></xsd:field>
            </xsd:unique>
    
          </xsd:element>
    

    感谢您的帮助!

    无效的Xml

    E.g.1
      <parentgroup>
    
        <parent id="1">
          <child id="2" />
          <child id="3" />
          <childgroup id="7">
            <child id="5" />
            <child id="6" />
          </childgroup>
          <child id="7" />
        </parent>
    
    
        <parent id="8">
          <childgroup id=9">
            <child id="10" />
            <child id="9" />
          </childgroup>
          <child id="12" />
          <child id="13" />
          <childgroup id="14">
            <child id="15" />
            <child id="16" />
          </childgroup>
        </parent>
    
      </parentgroup>
    
    原因
    1. 无效,因为在第一个父级中:childgroup id =“7”与id id =“7”具有相同的ID
    2. 无效,因为在第二个父级中:child id =“9”与其“father”childgroup id =“9”具有相同的ID
    3. E.g.2
        <parentgroup>
      
          <parent id="1">
            <child id="2" />
            <child id="3" />
            <childgroup id="4">
              <child id="5" />
              <child id="7" />
            </childgroup>
            <child id="7" />
          </parent>
      
      
          <parent id="8">
            <childgroup id=9">
              <child id="10" />
              <child id="8" />
            </childgroup>
            <child id="12" />
            <child id="14" />
            <childgroup id="14">
              <child id="15" />
              <child id="16" />
            </childgroup>
          </parent>
      
        </parentgroup>
      
      原因
      1. 无效,因为在第二个父级中:childgroup id =“14”与id id =“14”具有相同的ID
      2. E.g.2
          <parentgroup>
        
            <parent id="1">
              <child id="2" />
              <child id="7" />
              <childgroup id="4">
                <child id="5" />
                <child id="6" />
              </childgroup>
              <child id="7" />
            </parent>
        
        
            <parent id="8">
              <childgroup id=9">
                <child id="10" />
                <child id="11" />
              </childgroup>
              <child id="12" />
              <child id="14" />
              <childgroup id="15">
                <child id="16" />
                <child id="11" />
              </childgroup>
            </parent>
        
          </parentgroup>
        
        原因
        1. 无效,因为在第一个父级中:child id =“7”与id id =“7”具有相同的ID
        2. E.g.2
            <parentgroup>
          
              <parent id="1">
                <child id="2" />
                <child id="3" />
                <childgroup id="4">
                  <child id="1" />
                  <child id="2" />
                </childgroup>
                <child id="7" />
              </parent>
          
          
              <parent id="8">
                <childgroup id=9">
                  <child id="10" />
                  <child id="11" />
                </childgroup>
                <child id="12" />
                <child id="14" />
                <childgroup id="9">
                  <child id="16" />
                  <child id="11" />
                </childgroup>
              </parent>
          
            </parentgroup>
          
          原因
          1. 无效,因为在第二个父级中:childgroup id =“9”具有与childgroup id =“9”相同的ID
          2. 有效Xml

            E.g.1
              <parentgroup>
            
                <parent id="1">
                  <child id="2" />
                  <child id="3" />
                  <childgroup id="4">
                    <child id="5" />
                    <child id="6" />
                  </childgroup>
                  <child id="7" />
                </parent>
            
            
                <parent id="8">
                  <childgroup id=1">
                    <child id="2" />
                    <child id="3" />
                  </childgroup>
                  <child id="4" />
                  <child id="5" />
                  <childgroup id="6">
                    <child id="7" />
                    <child id="9" />
                  </childgroup>
                </parent>
            
              </parentgroup>
            
            E.g.2
              <parentgroup>
            
                <parent id="1">
                  <child id="2" />
                  <child id="3" />
                  <childgroup id="4">
                    <child id="5" />
                    <child id="7" />
                  </childgroup>
                  <child id="7" />
                </parent>
            
            
                <parent id="8">
                  <childgroup id=1">
                    <child id="2" />
                    <child id="5" />
                  </childgroup>
                  <child id="4" />
                  <child id="5" />
                  <childgroup id="6">
                    <child id="2" />
                    <child id="5" />
                  </childgroup>
                </parent>
            
              </parentgroup>
            

1 个答案:

答案 0 :(得分:3)

我已经清理并纠正了您的错误。下面是新的XSD,它与发布的XML和我理解它们的方式一起使用。我希望下面的图表可以帮助您更好地可视化约束和应该使用的XPath。

QTAssistant diagram showing XSD constraints

修改后的XSD:

<?xml version="1.0" encoding="utf-8" ?>
<!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)-->
<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="parentgroup" type="parentgroup">
        <xsd:unique name="UniqueParentId">
            <xsd:selector xpath="parent"/>
            <xsd:field xpath="@id"/>
        </xsd:unique>
    </xsd:element>

    <xsd:complexType name="parentgroup">
        <xsd:sequence maxOccurs="unbounded">
            <xsd:element name="parent" type="parent" maxOccurs="unbounded">
                <xsd:unique name="UniqueChildId">
                    <xsd:selector xpath=".|childgroup|child"/>
                    <xsd:field xpath="@id"/>
                </xsd:unique>
            </xsd:element>
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:int"/>
    </xsd:complexType>

    <xsd:complexType name="parent">
        <xsd:choice maxOccurs="unbounded">
            <xsd:element name="child" type="child" maxOccurs="unbounded"/>
            <xsd:element name="childgroup" type="childgroup" maxOccurs="unbounded">
                <xsd:unique name="UniqueChildIdInChildGroup">
                    <xsd:selector xpath=".|child"/>
                    <xsd:field xpath="@id"/>
                </xsd:unique>
            </xsd:element>
        </xsd:choice>
        <xsd:attribute name="id" type="xsd:int"/>
    </xsd:complexType>

    <xsd:complexType name="childgroup">
        <xsd:sequence maxOccurs="unbounded">
            <xsd:element name="child" type="child" maxOccurs="unbounded"/>
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:int"/>
    </xsd:complexType>

    <xsd:complexType name="child">
        <xsd:attribute name="id" type="xsd:int"/>
    </xsd:complexType>
</xsd:schema>

更新:XSD适用于所有示例XML。以下是QTAssistant对每个问题的评论:

无效: 例如。 1:

Error occurred while loading [], line 9 position 4 There is a duplicate key sequence '7' for the 'UniqueChildId' key or unique identity constraint. Error occurred while loading [], line 15 position 5 There is a duplicate key sequence '9' for the 'UniqueChildIdInChildGroup' key or unique identity constraint. ParentChildChildGroup-Inv-Eg1.xml is invalid.

E.g。 2:

Error occurred while loading [], line 21 position 5 There is a duplicate key sequence '14' for the 'UniqueChildId' key or unique identity constraint. ParentChildChildGroup-Inv-Eg2.xml is invalid.

E.g。 2(3?):

Error occurred while loading [], line 9 position 4 There is a duplicate key sequence '7' for the 'UniqueChildId' key or unique identity constraint. ParentChildChildGroup-Inv-Eg3.xml is invalid.

E.g。 2(4?):

Error occurred while loading [], line 21 position 5 There is a duplicate key sequence '9' for the 'UniqueChildId' key or unique identity constraint. ParentChildChildGroup-Inv-Eg4.xml is invalid.

您所描述的有效XML已成功验证。