XSD 1.1 xs:alternative / xs:assert

时间:2014-07-16 07:20:58

标签: xml xsd xsd-validation xsd-1.1

xsd 1.1有可能吗? 我想切换属性取决于"类型"如果它的" A"或" B"。 如何为这个简单的问题编写XSD 1.1语法?

  <?xml version="1.0"?>
  <node type="A" a1="asd" a2="d"/>

  <node type="B" b="4" />

2 个答案:

答案 0 :(得分:1)

是的,您可以使用node定义type的替代类型,具体取决于xs:alternative属性的值。在这种情况下,您不需要xs:assert

您的示例与规范中的示例非常相似,我不太确定您为什么要问这个问题。例如:

<xs:element name="message" type="messageType">
  <xs:alternative test="@kind='string'" type="messageTypeString"/>
  <xs:alternative test="@kind='base64'" type="messageTypeBase64"/>
  <xs:alternative test="@kind='binary'" type="messageTypeBase64"/>
  <xs:alternative test="@kind='xml'"    type="messageTypeXML"/>
  <xs:alternative test="@kind='XML'"    type="messageTypeXML"/> 
  <xs:alternative                       type="messageType"/>
</xs:element>

答案 1 :(得分:0)

以下作品可以为示例进行测试!:

<?xml version="1.0"?>

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="Test"  
    xsi:schemaLocation="Test SceneFile.xsd">

  <node mode="A" value="3"/>
  <node mode="B" name="asd"  a="s" asd="asd"/>


</root>

    

  <xs:element name="root" type="root"/>

  <xs:element name="node" type="node">
        <xs:alternative type="node-type-A" test="@mode = 'A'"/>  
        <xs:alternative type="node-type-B" test="@mode = 'B'"/>  
  </xs:element>


  <xs:complexType name="root">
    <xs:sequence>
      <xs:element ref="node" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <!-- Base type -->
  <xs:complexType name="node">
    <xs:attribute name="mode" type="allowed-node-types"/>
  </xs:complexType>

  <xs:simpleType name="allowed-node-types">
    <xs:restriction base="xs:string">
      <xs:enumeration value="A"/>
      <xs:enumeration value="B"/>
    </xs:restriction>
  </xs:simpleType>      

  <!-- Type A -->
  <xs:complexType name="node-type-A">
    <xs:complexContent>
        <xs:extension base="node">
            <xs:attribute name="value" type="xs:string" use="required"/>
            <xs:anyAttribute />
      </xs:extension>  
    </xs:complexContent>
  </xs:complexType>

  <!-- Type B -->
  <xs:complexType name="node-type-B">
    <xs:complexContent>
      <xs:extension base="node">
        <xs:attribute name="name" type="xs:string" use="required"/>
        <xs:anyAttribute processContents="skip"/>
      </xs:extension>  
    </xs:complexContent>
  </xs:complexType>  


</xs:schema>