XML Schema: Child element type based on parent att

2019-07-11 06:26发布

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?

标签: xml xsd xsd-1.0
2条回答
唯我独甜
2楼-- · 2019-07-11 06:33

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>
查看更多
一夜七次
3楼-- · 2019-07-11 06:48

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>
查看更多
登录 后发表回答