两个XML的通用XSD

时间:2016-12-23 08:36:55

标签: xml xsd xsd-validation

我有两个XML。在msgtype标记值的基础上包含一些共同的元素和相同的唯一元素。我需要为两者创建一个通用的XSD。 我选择了唯一标签,但没有工作。

实施例

- XML AAA -

<?xml version="1.0" encoding="UTF-8"?>
<sf:sf700 xmlns:sf="http://www.test.com/schema/sf100">
    <sf:sftMessage>
        <sf:msgType>AAA</sf:msgType>
        <sf:commonElement1>commonElement1</sf:commonElement1>
        <sf:commonElement2>commonElement2</sf:commonElement2>
        <sf:TypeAAA1>AAA7011</sf:TypeAAA1>
        <sf:TypeAAA2>AAA7012</sf:TypeAAA2>
    </sf:sftMessage>
</sf:sf700>

- XML BBB -

<?xml version="1.0" encoding="UTF-8"?>
<sf:sf710 xmlns:sf="http://www.test.com/schema/sf100">
    <sf:sftMessage>
        <sf:msgType>BBB</sf:msgType>
        <sf:commonElement1>commonElement1</sf:commonElement1>
        <sf:commonElement2>commonElement2</sf:commonElement2>
        <sf:TypeBBB1>BBB7011</sf:TypeBBB1>
        <sf:TypeBBB2>BBB7012</sf:TypeBBB2>
    </sf:sftMessage>
</sf:sf710>

- XSD -

<xs:schema attributeFormDefault="unqualified" 
elementFormDefault="qualified" 
targetNamespace="http://www.test.com/schema/sf100" 
xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="sf700">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="sftMessage">
          <xs:complexType>
            <xs:all>
              <xs:element type="xs:string" name="msgType"/>
              <xs:element type="xs:string" name="commonElement1"/>
              <xs:element type="xs:string" name="commonElement2"/>
              <xs:element type="xs:string" name="TypeAAA1"/>
              <xs:element type="xs:string" name="TypeAAA2"/>
            </xs:all>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

commonElement1,commonElement2

的公共元素msgType AAA and BBB相同

1 个答案:

答案 0 :(得分:2)

这将为您提供结构,但验证将不依赖于msgType值。这不能在XSD 1.0中完成,但可以使用XSD 1.1

完成

enter image description here

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML 2017 Developer Bundle Edition (Trial) 15.0.0.7015 (https://www.liquid-technologies.com)-->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.test.com/schema/sf100" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="sf700">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="sftMessage">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="msgType" type="xs:string" />
                            <xs:element name="commonElement1" type="xs:string" />
                            <xs:element name="commonElement2" type="xs:string" />
                            <xs:choice>
                                <xs:sequence>
                                    <xs:element name="TypeAAA1" type="xs:string" />
                                    <xs:element name="TypeAAA2" type="xs:string" />
                                </xs:sequence>
                                <xs:sequence>
                                    <xs:element name="TypeBBB1" type="xs:string" />
                                    <xs:element name="TypeBBB2" type="xs:string" />
                                </xs:sequence>
                            </xs:choice>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

如果您能够更改XML以使其看起来更像这样,那么您可以使用XSD 1.0架构完成所需的工作

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML 2017 Developer Bundle Edition (Trial) 15.0.0.7015 (https://www.liquid-technologies.com) -->
<sf700 xmlns="http://www.test.com/schema/sf100" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://www.test.com/schema/sf100 XSDFile2.xsd">
    <sftMessage xsi:type="msgTypeAAA" >
        <commonElement1></commonElement1>
        <commonElement2></commonElement2>
        <TypeAAA1></TypeAAA1>
        <TypeAAA2></TypeAAA2>
    </sftMessage>
</sf700>

然后,以下架构将正确验证它

enter image description here

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML 2017 Developer Bundle Edition (Trial) 15.0.0.7015 (https://www.liquid-technologies.com)-->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.test.com/schema/sf100" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="sf700">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="sftMessage" xmlns:q1="http://www.test.com/schema/sf100" type="q1:sftMessageType" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType abstract="true" name="sftMessageType">
        <xs:sequence>
            <xs:element name="commonElement1" type="xs:string" />
            <xs:element name="commonElement2" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="msgTypeAAA">
        <xs:complexContent>
            <xs:extension xmlns:q2="http://www.test.com/schema/sf100" base="q2:sftMessageType">
                <xs:sequence>
                    <xs:element name="TypeAAA1" type="xs:string" />
                    <xs:element name="TypeAAA2" type="xs:string" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="msgTypeBBB">
        <xs:complexContent>
            <xs:extension xmlns:q2="http://www.test.com/schema/sf100" base="q2:sftMessageType">
                <xs:sequence>
                    <xs:element name="TypeBBB1" type="xs:string" />
                    <xs:element name="TypeBBB2" type="xs:string" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>