Condition based on attribute value (XML Schema)

后端 未结 1 1121
梦谈多话
梦谈多话 2021-02-05 14:21

Is it possible to define in XML Schema an condition based on attribute value? For example, when test@attrib=\"one\", I want one-element to be allowed a

相关标签:
1条回答
  • 2021-02-05 14:58

    Not within the same type. You would need to define a different type for each of the different options.

    UPDATE

    To re-use type definitions in your schema:

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema elementFormDefault="qualified" 
               xmlns:xs="http://www.w3.org/2001/XMLSchema" 
               xmlns="http://My.Schema.Namespace" 
               targetNamespace="http://My.Schema.Namespace">
    
      <xs:element name="root">
        <xs:complexType>
          <xs:choice>
            <xs:element name="test1" type="test1Type" />
            <xs:element name="test2" type="test2Type" />
          </xs:choice>
        </xs:complexType>
      </xs:element>
    
      <!-- define the two root types -->
      <xs:complexType name="test1Type">
        <xs:all>
          <xs:element name="some-element-1" type="some-element-1Type" />
          <xs:element name="some-element-2" type="some-element-2Type" />
          <xs:element name="some-element-3" type="some-element-3Type" />
          <xs:element name="one-element" type="one-elementType" />
        </xs:all>
        <xs:attribute name="attrib" type="xs:string" fixed="one" />
      </xs:complexType>
    
      <xs:complexType name="test2Type">
        <xs:all>
          <xs:element name="some-element-1" type="some-element-1Type" />
          <xs:element name="some-element-2" type="some-element-2Type" />
          <xs:element name="some-element-3" type="some-element-3Type" />
          <xs:element name="two-element" type="two-elementType" />
        </xs:all>
        <xs:attribute name="attrib" type="xs:string" fixed="two" />
      </xs:complexType>
    
      <!-- Define re-usable types-->
      <xs:complexType mixed="true" name="some-element-1Type"/>
      <xs:complexType mixed="true" name="some-element-2Type"/>
      <xs:complexType mixed="true" name="some-element-3Type"/>
      <xs:complexType mixed="true" name="one-elementType"/>
      <xs:complexType mixed="true" name="two-elementType"/>
    
    </xs:schema>
    

    This will validate:

    <?xml version="1.0" encoding="utf-8" ?>
    <root xmlns="http://My.Schema.Namespace">
      <test1 attrib="one">
        <some-element-1>sadas</some-element-1>
        <some-element-2>sadas</some-element-2>
        <some-element-3>sadas</some-element-3>
        <one-element>sadas</one-element>
      </test1>
    </root>
    

    and

    <?xml version="1.0" encoding="utf-8" ?>
    <root xmlns="http://My.Schema.Namespace">
      <test2 attrib="two">
        <some-element-1>sadas</some-element-1>
        <some-element-2>sadas</some-element-2>
        <some-element-3>sadas</some-element-3>
        <two-element>sadas</two-element>
      </test2>
    </root>
    
    0 讨论(0)
提交回复
热议问题