2012-11-09 101 views
4

我不確定這是否可以使用XML/XSD,並且沒有太多運氣搜索它。指定XSD,使XML元素必須具有另一個XML元素的值

您可以指定一個XSD,以便在編寫XML時可以有一個元素必須引用另一個XML元素中包含的值之一?

例如,如果您有這個XML:

<car>Audi</car> 
<car>Ford</car> 
<car>Honda</car> 

<person> 
    <drives>Audi</drives> 
</person> 

你如何指定驅動XSD所以它必須是車開進值中的一個?

回答

4

使用XSD 1.1,您可以使用assertions

Assertion components constrain the existence and values of related elements and attributes.

您可以使用XPath表達式2.0中的斷言執行驗證/測試。

此示例架構具有用於確保/doc/person/drives值等於它的一個所述/doc元件的斷言真實/doc/car元素:

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.1" elementFormDefault="qualified"> 

    <xs:element name="car" type="xs:string" /> 
    <xs:element name="drives" type="xs:string" /> 

    <xs:element name="person"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="drives"/> 
      </xs:sequence>  
     </xs:complexType> 
    </xs:element> 

    <xs:element name="doc"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="car" maxOccurs="unbounded"/> 
       <xs:element ref="person"/> 
      </xs:sequence> 
      <xs:assert test="person/drives = car"/> 
     </xs:complexType> 
    </xs:element> 

</xs:schema> 
+0

感謝的Mads指定他們,看起來是我所追求的 –

1

這看起來像枚舉,你的例子碰巧與w3schools例子非常相似。

<xs:element name="car" type="carType"/> 

<xs:simpleType name="carType"> 
    <xs:restriction base="xs:string"> 
     <xs:enumeration value="Audi"/> 
     <xs:enumeration value="Golf"/> 
     <xs:enumeration value="BMW"/> 
    </xs:restriction> 
</xs:simpleType> 

<xs:element name="person"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element name="drives" type="carType"/> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 

如果不是你可能想Pangeas的答案。

+0

是的,我知道枚舉,但我想在XML,而不是XSD –

5

XSD 1.0會工作。理想情況下,您應該使用相同的模式類型和參照完整性的組合。

的XSD:

<?xml version="1.0" encoding="utf-8"?> 
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)--> 
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="sample"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element maxOccurs="unbounded" name="car" type="car"/> 
       <xsd:element name="person"> 
        <xsd:complexType> 
         <xsd:sequence> 
          <xsd:element name="drives" type="car"/> 
         </xsd:sequence> 
        </xsd:complexType> 
       </xsd:element> 
      </xsd:sequence> 
     </xsd:complexType> 
     <xsd:key name="PKCars"> 
      <xsd:selector xpath="car"/> 
      <xsd:field xpath="."/> 
     </xsd:key> 
     <xsd:keyref name="FKPersonCar" refer="PKCars"> 
      <xsd:selector xpath="person/drives"/> 
      <xsd:field xpath="."/> 
     </xsd:keyref> 
    </xsd:element> 
    <xsd:simpleType name="car"> 
     <xsd:restriction base="xsd:string"> 
      <xsd:enumeration value="Audi"/> 
      <xsd:enumeration value="Ford"/> 
      <xsd:enumeration value="Honda"/> 
     </xsd:restriction> 
    </xsd:simpleType> 
</xsd:schema> 

圖:

enter image description here

無效XML:

<sample> 
    <car>Audi</car> 
    <car>Ford</car> 
    <car>Honda</car> 

    <person> 
     <drives>Aud</drives> 
    </person> 
</sample> 

錯誤:

Error occurred while loading [], line 7 position 16 
The 'drives' element is invalid - The value 'Aud' is invalid according to its datatype 'car' - The Enumeration constraint failed. 
specify-xsd-such-that-an-xml-element-must-have-the-value-of-another-xml-element.xml is invalid. 

這個錯誤告訴你使用枚舉值會使key/keyref變得多餘 - 你不會觸發它。

但是,如果在XSD中不能包含枚舉值的列表,則應該至少強制該類型的最小長度,以避免創建空值的空值。當然,雖然建議分享這種類型,但您不必這樣做。

修改XSD:

<?xml version="1.0" encoding="utf-8"?> 
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)--> 
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="sample"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element maxOccurs="unbounded" name="car" type="car"/> 
       <xsd:element name="person"> 
        <xsd:complexType> 
         <xsd:sequence> 
          <xsd:element name="drives" type="car"/> 
         </xsd:sequence> 
        </xsd:complexType> 
       </xsd:element> 
      </xsd:sequence> 
     </xsd:complexType> 
     <xsd:key name="PKCars"> 
      <xsd:selector xpath="car"/> 
      <xsd:field xpath="."/> 
     </xsd:key> 
     <xsd:keyref name="FKPersonCar" refer="PKCars"> 
      <xsd:selector xpath="person/drives"/> 
      <xsd:field xpath="."/> 
     </xsd:keyref> 
    </xsd:element> 
    <xsd:simpleType name="car"> 
     <xsd:restriction base="xsd:normalizedString"> 
      <xsd:minLength value="1"/> 
     </xsd:restriction> 
    </xsd:simpleType> 
</xsd:schema> 

錯誤消息:

Error occurred while loading [], line 9 position 4 
The key sequence 'Aud' in Keyref fails to refer to some key. 
specify-xsd-such-that-an-xml-element-must-have-the-value-of-another-xml-element.xml is invalid. 
相關問題