XML模式验证,如何忽略元素内的树

时间:2015-05-11 17:53:20

标签: xml validation xsd

考虑这个xml:

<myName myClass="grandfather" name="grandfather_name" transaction="new">
<attributes>
    <name>test name</name>
    <description>some stuff for desc</description>
    <version>the current version</version>
</attributes>
<myName myClass="father" name="father_name" transaction="new">
    <attributes>
    <name>test name</name>
    <description>some stuff for desc</description>
    <version>the current version</version>
    <wifeName>name of wife</wifeName>
    </attributes>
    <myName myClass="child_1" name="child_1_name" transaction="new">
        <attributes>
            <name>test name</name>
            <description>some stuff for desc</description>
            <version>the current version</version>
            <GirlFriendName>name of GirlFriend</GirlFriendName>
        </attributes>
    </myName>
    <myName myClass="child_2" name="child_2_name" transaction="new">
        <attributes>
            <name>test name</name>
            <description>some stuff for desc</description>
            <version>the current version</version>
            <GirlFriendName>name of GirlFriend</GirlFriendName>
        </attributes>
    </myName>
</myName>

我想验证的是#34;爷爷&#34;及其属性,并忽略XML的其余部分。 出于这个原因,我在架构中添加了这一行: 类型=&#34; XS:anyType的&#34;

此行导致验证忽略&#34;父亲&#34;,但验证在&#34; child_1&#34;中失败。和&#34; child_2&#34;。

  

&#34;从元素&#39; GirlFriendName&#39;开始发现无效内容。没有   在这一点上预计会出现子元素&#34;

正在使用的XSD是:

<?xml version="1.0" encoding="UTF-8"?>

<xs:group name="AttributesGroup"> 
    <xs:all>
        <xs:element name="name" type="xs:string" minOccurs="1" maxOccurs="1" />
        <xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="1" />
        <xs:element name="version" type="xs:string" minOccurs="0" maxOccurs="1" />
    </xs:all>
</xs:group>

<!-- BASE COMPLEX TYPES DEFINITIONS -->

<xs:complexType name="MyComplexType">
    <xs:sequence>
        <xs:element name="attributes" minOccurs="1" maxOccurs="unbounded">
            <xs:complexType>
                    <xs:group ref="AttributesGroup"/>
            </xs:complexType>
        </xs:element>

        <xs:element name="myName" type="xs:anyType" minOccurs="0" maxOccurs="unbounded"/>

    </xs:sequence>

    <xs:attribute name="myClass" type="xs:string" use="required" />
    <xs:attribute name="name" type="xs:string" />
    <xs:attribute name="transaction" type="xs:string" use="required" />
</xs:complexType>


<!-- ROOT ELEMENT -->
<xs:element name="myName" type="MyComplexType" />

我怎样才能忽略以下所有内容&#34;父亲&#34; ?

1 个答案:

答案 0 :(得分:0)

您可以在xsd:any内的本地定义的myName中使用MyComplexType;以下XSD将成功验证您的XML:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="unqualified">
    <xs:group name="AttributesGroup"> 
        <xs:all>
            <xs:element name="name" type="xs:string" minOccurs="1" maxOccurs="1" />
            <xs:element name="description" type="xs:string"
                  minOccurs="0" maxOccurs="1" />
            <xs:element name="version" type="xs:string" minOccurs="0" maxOccurs="1" />
        </xs:all>
    </xs:group>
    <xs:complexType name="MyComplexType">
        <xs:sequence>
            <xs:element name="attributes" minOccurs="1" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:group ref="AttributesGroup"/>
                </xs:complexType>
            </xs:element>
            <xs:element name="myName" minOccurs="1" maxOccurs="unbounded">
                <xs:complexType>
          <xs:sequence>
            <xs:any processContents="skip"
                    minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
          <xs:anyAttribute processContents="skip"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="myClass" type="xs:string" use="required" />
        <xs:attribute name="name" type="xs:string" />
        <xs:attribute name="transaction" type="xs:string" use="required" />
    </xs:complexType>
    <xs:element name="myName" type="MyComplexType" />
</xs:schema>