2014-02-06 80 views
0

我想在我的XSD模式定義中嵌入用於如何使用元素的可選屬性的規則。考慮以下元素定義:在XSD模式中嵌入驗證

<xs:complexType name="sampleElement"> 
    <xs:attribute name="name" type="xs:string" use="required"/> 
    <xs:attribute name="description" type="xs:string" use="optional"/> 
    <xs:attribute name="optPrimary" type="xs:string" use="optional"/> 
    <xs:attribute name="optSecondary" type="xs:string" use="optional"/> 
</xs:complexType 

在此示例中,optPrimary和optSecondary都是可選的。 optPrimary屬性本身可以使用,但optSecondary必須與optPrimary一起使用。因此,我希望在XML驗證時嵌入在模式中的規則可以執行。

我在一個單獨的文件中發現了用於此的Schematron的示例,但是我還沒有找到如何將它作爲模式的一部分嵌入。

+0

XML模式不支持在元素之間創建約束的能力。你已經正確識別瞭解決這個差距的schematron。 –

回答

0

XSD 1.1支持此功能。這是表達它的一種方式:

<?xml version="1.0" encoding="utf-8" ?> 
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) --> 
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xerces="http://xerces.apache.org"> 
    <xsd:element name="sampleElement"> 
     <xsd:complexType> 
      <xsd:attribute name="name" type="xsd:string" use="required"/> 
      <xsd:attribute name="description" type="xsd:string" use="optional"/> 
      <xsd:attribute name="optPrimary" type="xsd:string" use="optional"/> 
      <xsd:attribute name="optSecondary" type="xsd:string" use="optional"/> 
      <xsd:assert test="@optPrimary or not(@optSecondary)" xerces:message="Secondary needs primary..."/> 
     </xsd:complexType>   
    </xsd:element> 
</xsd:schema>