XSD: Define an element with any name

浪尽此生 提交于 2019-12-03 06:57:08
Filburt

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.

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