【问题标题】:XSD: Define element with any type and any nameXSD:定义任何类型和任何名称的元素
【发布时间】:2013-04-27 17:20:05
【问题描述】:

我有以下XML 结构:

<?xml version="1.0" encoding="utf-8"?>
<DataSets>
  <DataSet>
    <Parameter id="1"/>
    <Parameter Description="My Description"/>
    <Parameter value="3.14"/>
  </DataSet>
  <DataSet>
    <Parameter id="2"/>
    <Parameter timeout="123"/>
  </DataSet>
</DataSets>

为了验证,我想创建一个 XSD 架构。最内部的元素Parameter 可以通过any 类型与any 名称。必须至少有一个这样的元素。

如何为这个内部元素定义XSD方案?

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

    您可以使用 xs:any 指定任何名称和类型。您的 XML 针对以下 XSD 进行验证:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    
        <xs:element name="DataSets" type="dataSets"/>
    
        <xs:complexType name="dataSets">
            <xs:sequence>
                <xs:element name="DataSet" type="dataSet" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    
        <xs:complexType name="dataSet">
            <xs:sequence>
                <xs:any minOccurs="1" maxOccurs="unbounded" namespace="##any" processContents="lax" />
            </xs:sequence>
        </xs:complexType>
    
    </xs:schema>
    

    【讨论】:

    • 该解决方案允许 Parameter 具有任何类型,但可以具有任何名称作为所要求的问题。要允许任何名称,您需要使用通配符:将 xs:element name="Parameter" 替换为 xs:any。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    相关资源
    最近更新 更多