简体   繁体   中英

Validating XML aginst Schema to Restrict Special Characters inside XML Elements

I'm new to XML Validation. Pardon my ignorance :(
I need to validate the xml using DocumentBuilderFactory.
Sample Input XML

<FirstElement>

          <ChildElement></ChildElement>FirstElementValue

</FirstElement>

Expected Result:

Exception should be thrown stating that child element is not allowed inside
<FirstElement> </FirstElement>

Actual Result:
Value inside the <FirstElement> </FirstElement> is getting parsed as below.

<FirstElement>

              &lt; ChildElement &gt; &lt; / ChildElement &gt; FirstElementValue

</FirstElement>

As the value is getting parsed as "&lt;" instead of "<" , XML validation is not getting failed while validating against the Schema.. I'm struggling with this issue from past few days.. Please suggest me with sample code. Thanks

Please provide schema and code.The provided information is not enough to help. I have anyway tried to provide some direction so as to help.

The interpretation of data as string should still give an error is the XSD is written properly.

eg For the below schema

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="FirstElement">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="ChildElement"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="ChildElement" type="xs:string"/>
</xs:schema>

The XML is valid against the above xsd:

<FirstElement>
          <ChildElement>FirstElementValue</ChildElement>
</FirstElement>

But the below is invalid

<FirstElement>
          <ChildElement></ChildElement>FirstElementValue
</FirstElement>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM