2011-11-15 9 views
8

Załóżmy, że mamy następujący schemat:Ograniczanie referencje tożsamości do określonej grupy elementów

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

i tu jest plik xml próbki:

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

Więc c_elements mogą odwoływać a_elements i b_elements przez id. Czy w jakiś sposób można ograniczyć atrybut ref tylko do akceptowania odniesień do elementów z jednej grupy, np. A_elements?

Odpowiedz

6

nawiązaniu do moich wcześniejszych odpowiedzi, w teorii nie można ograniczyć za pomocą czysto ID/IDREF jednak jest to możliwe, aby dodać ograniczenie tożsamości, które spełniają swoje wymagania:

<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

To wydaje się być tym, czego szukałem. Dzięki! – Max

1

Nie jestem świadomy żadnego mechanizmu, aby to zrobić za pomocą ID i IDREF. Według ID projektu i IDREF odnoszą się do wszystkich znaczników w dokumencie.

Powiedziałeś, że możesz obejść to w jakiś sposób - być może z regułami sprawdzania poprawności w odniesieniu do dowolnych procesów w strukturze danych. Byłoby dość łatwo to zrobić, używając na przykład wyrażeń Xpath. Z pewnością można to osiągnąć za pomocą asercji Schematron. Oto przykład tego: http://zvon.org/xxl/SchematronTutorial/Examples/Example16/example.html

Mam nadzieję, że to pomoże.

Ken

0

rozwiązanie podane przez kennethmay może nie działać, jeśli używasz XSD 1.0. Na przykład używam edytora Visual Studio 2015 i wskazuję, powiedzmy, że element b (np.) Nie jest identyfikowany jako błąd. Domyślam się, że działa tylko dla XSD w wersji 1.1

Powiązane problemy