Malintha
Malintha

Reputation: 4766

XML schema validation : Check the excistance of special characters

I want to validate my XML file using XSD schema. In there, I want to check whether a specific attribute value contains some character (eg: $ character). How can I check this with my XSD. My XML is look like this.

<parent name="abc">
..........
..........
</parent>

<parent name="ef$">
........
........
</parent>

I want to check the excistance of "$" character inside the attribute value and parse an error. Could you suggest me a XSD schema for this

Upvotes: 0

Views: 3101

Answers (1)

user764357
user764357

Reputation:

With this you want your attribute to have a specific datatype. So you aren't checking what the value is, more declaring what it can be.

Here, we define a restricted datatype based on a string. Here, the regular expression in the xs:pattern is [^$]* which translates to "0 or more characters that aren't a '$'":

<xs:simpleType name="myAttribute">
    <xs:restriction base="xs:string">
        <xs:pattern value="[^$]*"/>
    </xs:restriction>
</xs:simpleType>

Then, on the declaration of the parent element, the attribute has that restricted datatype.

<xs:element name="parent">
    <xs:complexType>
        <xs:attribute name="name" type="myAttribute"/>
    </xs:complexType>
</xs:element>

Upvotes: 3

Related Questions