由于某些系统的限制,我们需要使用格式化有点不方便的xml。我们需要转换成一种方便的形式。
问题是:如何在xsd模式中定义具有以下属性的元素:
没有孩子
没有任何属性
有任何名字(这就是导致问题的原因)

最佳答案

您可以将<xsd:any />元素与Xml Schema Instance type attribute一起使用。
图式

<?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>

测试XML实例
<?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>

结论
不能仅在xsd架构中强制使用简单类型。

07-24 09:48
查看更多