XML Schema: Child element type based on parent attribute 'type'

ⅰ亾dé卋堺 提交于 2020-01-04 05:45:10

问题


I have an XML document like below:

<operation name="func">
    <param name="a" type="string">up</param>
    <param name="b" type="integer">5</param>
    <param name="c" type="CustomList">
        <list>
            <idx>0</idx>
            <name>ZZZZ</name>
            <value>0.5464</value>
        </list>
        <list>
            <idx>1</idx>
            <name>YYYY</name>
            <value>0.464</value>
        </list>
        ....
    </param>
</operation>

and I wrote an XSD to restrict it:

<xs:complexType name="CustomListNode">
    <xs:sequence>
        <xs:element name="idx" type="xs:integer" />
        <xs:element name="name" type="xs:string" />
        <xs:element name="value" type="xs:decimal" />
    </xs:sequence>
</xs:complexType>

<xs:complexType name="CustomList">
    <xs:sequence>
        <xs:element name="list" type="CustomListNode" maxOccurs="unbounded"/>
    </xs:sequence>
</xs:complexType>

<xs:element name="operation">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="param" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:simpleContent>
                        <xs:extension base="xs:NOTATION">
                            <xs:attribute name="name" type="xs:string" use="required" />
                            <xs:attribute name="type" type="xs:integer" use="required" />
                            <xs:attribute name="type" type="xs:string" use="required" />
                            <xs:attribute name="type" type="CustomList" use="required"/>
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="name" type="xs:string" use="required" />
    </xs:complexType>
</xs:element>

Of course, this doesn't work. Someone suggested me to use xs:alternative in XSD 1.1. But I only have libxml2 now. It doesn't support XSD 1.1.

How can I do in XSD 1.0? Or it cannot be implemented in XSD 1.0 absolutely?


回答1:


Conditional Type Assignment (xs:alternative) requires XSD 1.1.

You cannot do Conditional Type Assignment in XSD 1.0, but if you're open to adjusting your XML design, you could use xsi:type to achieve a similar effect:

XML

<?xml version="1.0" encoding="UTF-8"?>
<operation xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           name="func">
  <a xsi:type="xs:string">up</a>
  <b xsi:type="xs:integer">5</b>
  <c xsi:type="CustomList">
    <list>
      <idx>0</idx>
      <name>ZZZZ</name>
      <value>0.5464</value>
    </list>
    <list>
      <idx>1</idx>
      <name>YYYY</name>
      <value>0.464</value>
    </list>
  </c>
</operation>

XSD 1.0

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="operation">
    <xs:complexType>
      <xs:sequence>
        <xs:any maxOccurs="unbounded" processContents="lax"/>
      </xs:sequence>
      <xs:attribute name="name" type="xs:string" use="required"/>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="CustomList">
    <xs:sequence>
      <xs:element name="list" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="idx" type="xs:integer"/>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="value" type="xs:decimal"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

</xs:schema>



回答2:


You can use this schema:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="operation">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="param">
          <xs:complexType mixed="true">
            <xs:sequence minOccurs="0">
              <xs:element maxOccurs="unbounded" name="list">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="idx" type="xs:integer" />
                    <xs:element name="name" type="xs:string" />
                    <xs:element name="value" type="xs:decimal" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="name" type="xs:string" use="required" />
            <xs:attribute name="type" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="name" type="xs:string" use="required" />
    </xs:complexType>
  </xs:element>
</xs:schema>


来源:https://stackoverflow.com/questions/33667606/xml-schema-child-element-type-based-on-parent-attribute-type

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