Search code examples
javastringxsdxsd-validationxsd-1.1

I want to send <br> tag inside CDATA in XML. Which Does not get validated inside XSD


I want to send
tag inside CDATA in XML. Which Does not get validated inside XSD. uses sequence in XSD. My XML goes like this.

<hotelnotes>
    <hotelnote><![CDATA[This is <br> Hotel Note <br> End of hotel note]]></hotelnote>
</hotelnotes>

XSD

  <xs:element name="hotelnotes">
      <xs:complexType>
        <xs:sequence>
          <xs:element type="xs:string" name="hotelnote" minOccurs="0"/>
        </xs:sequence>
      </xs:complexType>
  </xs:element>

Solution

  • If you want to ensure that the <br> tag is inside of the text in hotel note you can use a simple type based on the string type with a pattern restriction.

    Here is an example of such a restriction:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="hotelnotes">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="hotelnote" minOccurs="0">
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:pattern value=".+&lt;br\s*&gt;.+" />
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    

    This file will validate against the XSD code above:

    <?xml version='1.0' encoding='utf-8'?>
    <hotelnotes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:noNamespaceSchemaLocation="../xsd/hotel_example.xsd">
        <hotelnote><![CDATA[This is <br> Hotel Note End of hotel note]]></hotelnote>
    </hotelnotes>
    

    whereas this one will not because it does not contain a <br> tag:

    <?xml version='1.0' encoding='utf-8'?>
    <hotelnotes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:noNamespaceSchemaLocation="../xsd/hotel_example.xsd">
        <hotelnote><![CDATA[This is Hotel Note End of hotel note]]></hotelnote>
    </hotelnotes>
    

    Update:

    If you need to accept a more generic string in CDATA you can use this XSD:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="hotelnotes">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="hotelnote" minOccurs="0" >
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:pattern value=".+" /><!-- Enter here whichever regular expression which imposes a limitation on the string in CDATA -->
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    

    The version above just requires at least one character in the CDATA block.