【问题标题】:How to fully define an XML Schema for same element name but differing subtype如何为相同的元素名称但不同的子类型完全定义 XML Schema
【发布时间】:2014-07-11 17:51:57
【问题描述】:

再次向 SO 社区致敬,

收到另一个关于如何正确定义 XSD 架构的请求。

鉴于我有一个 XML 文件,例如:

<?xml version="1.0" encoding="UTF-8" ?>
<transformation xmlns="http://www.denktmit.de/pdi/ktr" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.w3.org/2001/XMLSchema-instance
./test.xsd">
    <info>
        <name>TrafoName</name>
        <description>TrafoDescription</description>
    </info>
    <changedby>LastUser</changedby>
    <step>
        <type>FileInput</type>
        <description>FileInput Step description</description>
        <FileInputParameterOne>FileInputParam</FileInputParameterOne>
        <another>1</another>
    </step>
    <step>
        <type>Select</type>
        <description>Select Step description</description>
        <SelectParameterOne>SelectParameterOne</SelectParameterOne>
        <SelectParameterTwo>SelectParameterTwo</SelectParameterTwo>
        <another>2</another>
    </step>
</transformation>

这类似于http://community.pentaho.com/projects/data-integration/ 生成的转换文件的样式

如您所见,每个步骤在顶部包含多个序列节点

<type>Select</type>
<description>Select Step description</description>

在底部:

<another>2</another>

在这两者之间,可能会出现任意复杂的 xml 子树。但是到底发生了什么是由“类型”中的值决定的。用例是存储具体 Java 类的特定配置,它们都实现了一个接口,为“类型”、“描述”和“另一个”定义 getter/setter,并自己定义附加对象结构。只要特定类所需的所有元素都以某种方式存在,忽略顺序也足够了。

我已经读过了:

我尝试了类似的东西

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.denktmit.de/pdi/ktr" elementFormDefault="qualified"
    attributeFormDefault="unqualified" xmlns="http://www.denktmit.de/pdi/ktr">
    <xs:element name="transformation">
        <xs:complexType>
            <xs:sequence>
                <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element name="step" type="fileInputStep"></xs:element>
                    <xs:element name="step" type="selectStep"></xs:element>
                </xs:choice>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="stepComplexType" abstract="true"></xs:complexType>

    <xs:complexType name="fileInputStep">
        <xs:complexContent>
            <xs:extension base="stepComplexType">
                <xs:sequence>
                    <xs:element name="type" minOccurs="1"
                        maxOccurs="1">
                        <xs:simpleType>
                            <xs:restriction base="xs:string"></xs:restriction>
                        </xs:simpleType>
                    </xs:element>
                    <xs:element name="description" type="xs:string"
                        minOccurs="1" maxOccurs="1"></xs:element>
                    <xs:element name="FileInputParameterOne" type="xs:string">
                    </xs:element>
                    <xs:element name="another" type="xs:int" minOccurs="1"
                        maxOccurs="1"></xs:element>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="selectStep">
        <xs:complexContent>
            <xs:extension base="stepComplexType">
                <xs:sequence>
                    <xs:element name="type" minOccurs="1"
                        maxOccurs="1">
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:pattern value="Select"></xs:pattern>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:element>
                    <xs:element name="description" type="xs:string"
                        minOccurs="1" maxOccurs="1"></xs:element>
                    <xs:element minOccurs="1" name="SelectParameterOne"
                        type="xs:string">
                    </xs:element>
                    <xs:element minOccurs="1" name="SelectParameterTwo"
                        type="xs:string">
                    </xs:element>
                    <xs:element name="another" type="xs:int" minOccurs="1"
                        maxOccurs="1"></xs:element>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

但它不起作用,告诉我:

在此行发现多个注释:针对此验证 模式,将为这两个粒子创建歧义。 - 元素的开始标签 - cos-element-consistent:类型“#AnonType_transformation”的错误。名称为“step”的多个元素,具有不同的类型,出现在 模型组。

有什么想法吗?

【问题讨论】:

    标签: xsd


    【解决方案1】:

    您可以使用xsd Group。这是一个示例供您参考

    XSD:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="transformation">
        <xs:complexType>
            <xs:sequence>
                <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element name="step" type="stepType"/>
                </xs:choice>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    
    <xs:complexType name="stepComplexType" abstract="true"></xs:complexType>
    
    <xs:group name="selectParamGroup">
        <xs:sequence>
            <xs:element name="SelectParameterOne" type="xs:string" minOccurs="1" maxOccurs="1">
            </xs:element>
            <xs:element name="SelectParameterTwo" type="xs:string" minOccurs="1" maxOccurs="1">
            </xs:element>
        </xs:sequence>
    </xs:group>
    
    <xs:group name="paramGroup">
        <xs:choice>
            <xs:element name="FileInputParameterOne" type="xs:string">
            </xs:element>
            <xs:group ref="selectParamGroup">
            </xs:group>
        </xs:choice>
    </xs:group>
    
    <xs:complexType name="stepType">
        <xs:complexContent>
            <xs:extension base="stepComplexType">
                <xs:sequence>
                    <xs:element name="type" minOccurs="1"
                        maxOccurs="1">
                        <xs:simpleType>
                            <xs:restriction base="xs:string"></xs:restriction>
                        </xs:simpleType>
                    </xs:element>
                    <xs:element name="description" type="xs:string"
                        minOccurs="1" maxOccurs="1"></xs:element>
                    <xs:group ref="paramGroup"></xs:group>
                    <xs:element name="another" type="xs:int" minOccurs="1"
                        maxOccurs="1"></xs:element>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    </xs:schema>
    

    XML:

    <?xml version="1.0" encoding="UTF-8" ?>
    <transformation>
    <step>
        <type>FileInput</type>
        <description>FileInput Step description</description>
        <FileInputParameterOne>FileInputParam</FileInputParameterOne>
        <another>1</another>
    </step>
    <step>
        <type>Select</type>
        <description>Select Step description</description>
        <SelectParameterOne>SelectParameterOne</SelectParameterOne>
        <SelectParameterTwo>SelectParameterTwo</SelectParameterTwo>
        <another>2</another>
    </step>
    </transformation>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      相关资源
      最近更新 更多