开发者

How to do the Validation of only particular/specific tags in an XML?

开发者 https://www.devze.com 2022-12-16 05:35 出处:网络
I have some code-generated XML. I have written an XSD to validate the XML. I have tags in them XML that do not need to be validated. Is there any way to validate particular tags and skip the others?

I have some code-generated XML. I have written an XSD to validate the XML. I have tags in them XML that do not need to be validated. Is there any way to validate particular tags and skip the others?

The example XML is:

<person>
<firstname>Name</firstname>
&开发者_如何学Golt;lastname>Name</lastname>
<tag1>data</tag1>
<tag2>data</tag2>
<tag3>data</tag3>
</person>

I need to validate only <firstname> and <lastname> and to skip the validation of all other elements.


You can't "ignore" elements in terms of having the parser just skip through some of the XML, but you can make your schema less strict by allowing any type of element as a child element.

XSD allows this by the use of the "any" element. Example:

<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
      <xs:any minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Now you can have any unknown but valid xml element show up as a child of a "person" element.

0

精彩评论

暂无评论...
验证码 换一张
取 消