开发者

XSD: Define an element with any name

开发者 https://www.devze.com 2022-12-16 22:05 出处:网络
Because of limitations of certain systems, we need to us开发者_如何学Ce XMLs that are formatted a bit inconveniently. Those we need to transform into a convenient form.

Because of limitations of certain systems, we need to us开发者_如何学Ce XMLs that are formatted a bit inconveniently. Those we need to transform into a convenient form.

The question: how do I define in an XSD schema an element that has the following properties:

  • Does not have any children
  • Does not have any attributes
  • Has any name (that is what's causing problems)


You can use the <xsd:any /> element together with the Xml Schema Instance type attribute.

Schema

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="root">
        <xsd:complexType>
            <xsd:sequence maxOccurs="unbounded">
                <xsd:any processContents="strict" namespace="##local"></xsd:any>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:simpleType name="st">
        <xsd:restriction base="xsd:string" />
    </xsd:simpleType>
</xsd:schema>

Test Xml instance

<?xml version="1.0" encoding="utf-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <!-- valid -->
    <one xsi:type="st">value one</one>
    <emptyone xsi:type="st"/>

    <!-- invalid -->
    <two name="myname" xsi:type="st">value two</two>

    <!-- invalid -->
    <three xsi:type="st">
        <four xsi:type="st">value four</four>
    </three>
</root>

Conclusion

You cannot enforce a simple type in the xsd schema alone.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号