XSD 1.1 xs:alternative/xs:assert

半世苍凉 提交于 2019-11-28 02:11:22

问题


Is that possible with xsd 1.1? I want to switch the attributes depending on the "type" if its "A" or "B". How can I write an XSD 1.1 syntax for this simple problem?

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

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

回答1:


Yes, you can define alternative types for node depending on the value of the type attribute using xs:alternative. You don't need xs:assert in this case.

Your example seems so similar to the ones in the spec that I'm not quite sure why you are asking the question. For example:

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



回答2:


The following works and can be tested for the example!:

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


来源:https://stackoverflow.com/questions/24774554/xsd-1-1-xsalternative-xsassert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!