2011-11-15 21 views
8

私たちは、次のスキーマがあるとします。c_elementsはidでa_elementsとb_elementsを参照できるように特定の要素グループにIDの参照を制限

<root> 
    <a_elements> 
     <a_element id="id1"/> 
     <a_element id="id2"/> 
    </a_elements> 
    <b_elements> 
     <b_element id="id3"/> 
     <b_element id="id4"/> 
    </b_elements> 
    <c_elements> 
     <c_element id="id5" ref="id1"/> 
     <c_element id="id6" ref="id2"/> 
    </c_elements> 
</root> 

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xs:element name="root"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element name="a_elements"> 
       <xs:complexType> 
        <xs:sequence> 
         <xs:element name="a_element" maxOccurs="unbounded"> 
          <xs:complexType> 
           <xs:attribute name="id" type="xs:ID" use="required"/> 
          </xs:complexType> 
         </xs:element> 
        </xs:sequence> 
       </xs:complexType> 
      </xs:element> 
      <xs:element name="b_elements"> 
       <xs:complexType> 
        <xs:sequence> 
         <xs:element name="b_element" maxOccurs="unbounded"> 
          <xs:complexType> 
           <xs:attribute name="id" type="xs:ID" use="required"/> 
          </xs:complexType> 
         </xs:element> 
        </xs:sequence> 
       </xs:complexType> 
      </xs:element> 
      <xs:element name="c_elements"> 
       <xs:complexType> 
        <xs:sequence> 
         <xs:element name="c_element" maxOccurs="unbounded"> 
          <xs:complexType> 
           <xs:attribute name="id" type="xs:ID" use="required"/> 
           <xs:attribute name="ref" type="xs:IDREF" use="required"/> 
          </xs:complexType> 
         </xs:element> 
        </xs:sequence> 
       </xs:complexType> 
      </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 
</xs:schema> 

、ここでは、サンプルXMLファイルです。何らかの形でref属性を制限して、あるグループ、例えばa_elementsの要素への参照だけを受け入れることは可能ですか?

答えて

6

は、理論的にはあなたは純粋にID/IDREFを使用して制限することはできませんしかし、それはあなたの要件を満たすアイデンティティ制約を追加することが可能です:によって与えられた

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="root"> 

    <xs:complexType> 

     <xs:sequence> 
     <xs:element name="a_elements"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="a_element" maxOccurs="unbounded"> 
       <xs:complexType> 
        <xs:attribute name="id" type="xs:ID" use="required"/> 
       </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     <xs:element name="b_elements"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="b_element" maxOccurs="unbounded"> 
       <xs:complexType> 
        <xs:attribute name="id" type="xs:ID" use="required"/> 
       </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     <xs:element name="c_elements"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="c_element" maxOccurs="unbounded"> 
       <xs:complexType> 
        <xs:attribute name="id" type="xs:ID" use="required"/> 
        <xs:attribute name="ref" type="xs:IDREF" use="required"/> 
       </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
    <xs:keyref name="theKeyRef" refer="theKey"> 
     <xs:selector xpath="c_elements/*"/> 
     <xs:field xpath="@ref"/> 
    </xs:keyref> 
    <xs:key name="theKey"> 
     <xs:selector xpath="a_elements/*"/> 
     <xs:field xpath="@id"/> 
    </xs:key> 
    </xs:element> 
</xs:schema> 
+0

これは私が探していたものです。ありがとう! – Max

1

私は、IDとIDREFを使用してこれを行うメカニズムについて認識していません。設計上、IDとIDREFは文書内のすべてのタグを参照します。

しかし、これは何らかの方法で回避できます。おそらく、データ構造をどのようなプロセスで処理するかに関する検証ルールを使用することができます。例えば、Xpath式を使ってこれを行うのはかなり簡単でしょう。あなたは確かにSchematronアサーションを使ってこれを達成することができます。これの例がここにあります:​​

希望があります。

ケン私の以前の回答にさらに

0

ソリューションXSD 1.0を使用している場合、kennethmayが動作しないことがあります。たとえば、私はビジュアルスタジオ2015エディタを使用しており、bの要素(たとえば)がエラーとして識別されていないとします。私はこれがXSDバージョン1.1のためだけに動作すると思います。

関連する問題