Special Characters Restriction Rule in XSD - xsd

restrict special characters in my XSD validation , i am able to handle , some characters with this pattern "([a-zA-Z0-9_.' !##$%^*()_+={}|/:;,>?/`~ ])+"
but not able to handle below :
"
&
'
<
\
®
™
any ideas ?
also not able to handle them with [^] pattern

You want define a type that extends string and add a restriction with a pattern
Something like
<xs:element name="your_element">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9_.' !##$%^*()_+={}|/:;,>?/`~ ]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Or you can add the escape character \ (backslash) before any special character you want to add to the pattern. For example:
<xs:pattern value="[a-zA-Z 0-9_.,()/=!\[\]"#%&*;<>&apos;+:?-]"/>
where you see the square brackets [ and ] included in the pattern.

I think you need to use character entities. & for the ampersand, for example, and < for the less. XML Schema is XML, and you have to live with XML rules. Expanding your question to actually show us the schema context would help.

Related

XSD Validation - Allow All Except String That Contains

Quite new at XSD validations.
Is there any way at all to validate string values - to allow all values a user may enter except certain values that contain a keyword.
For example, I have a system that users can submit their names.
I want to allow any name that comes through except for strings that contain 'Joh'
i.e. Allow Mary, Eric, Simon, etc and reject John, Johan, Bjohn, Benjoh, etc
So in my understanding (coming from PHP), 'Not Equal To' is '!=' but this wouldn't work here.
And wildcards wouldn't work as for any values that contain 'joh' as '%joh%'
<xs:element name="Names">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value!="%joh%"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Is there anyway around this?

Add pattern to anyURI data type in xsd

How can I guarantee that the url element starts with "http://"?
<xs:element name="url" type="xs:anyURI"/>
You can add a xs:restriction for a Regular Expression using xs:pattern:
<xs:element name="url">
<xs:simpleType>
<xs:restriction base="xs:anyURI">
<xs:pattern value="http://.+" />
</xs:restriction>
</xs:simpleType>
</xs:element>
This will match to anything that starts with http://. It will match:
http://www.stackoverflow.com
http://somethingsomethingsomething
http://123456789!!!!!
http://0
It will not match https URLs:
https://github.com
If you want to match https as well you can change the pattern to
https?://.+
which means that the s is allowed and is optional.
If you want to match only valid URLs then you need to improve it to check for characters, followed by a dot, more characters, a valid domain suffix, etc. If you search for URL validation via regex you will find several examples. You can also try this resource. And to experiment with Regex, Regex 101 is a great resource.
Pattern matching in XSD has some restrictions. Check this SO question/answer which discusses that.

How to generate a 'HTML select' from XSD definition

I try to generate HTML code from XSD and plan to generate a selection with defined values.
I could use a restriction in XSD but I need the values of the options too.
<select name="Pizza" size="5">
<option value="P101">Pizza Napoli</option>
<option value="P102">Pizza Funghi</option>
</select>
What would be the proper XSD to define a HTML select with options and values?
A standard xs:restriction will allow you to mark up the values, but for the descriptions I'd just use a custom namespace on top of the schema. Something like this would do it:
<xs:element name="Pizza" xmlns:pizza="http://my.custom.namespace/pizza">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="P101" pizza:description="Pizza Napoli"/>
<xs:enumeration value="P102" pizza:description="Pizza Funghi"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
XML is extensible and so therefore is XML schema.
This approach has the benefit that you can still validate data against the schema easily (a validator will simply ignore the custom namespace), but when converting to HTML, you can write out the options in the way you want.

Is there a way to restrict a string to be ASCII only in XSD?

Although the XML file being described by the XSD Schema may contain any unicode characters in general, there are some fields where only ASCII is allowed. (As these strings are going to be passed to another system which only accepts ASCII.)
Is there a way to specify that in XSD?
A regexp with all possible ASCII characters would be a possibility I suppose, but I feel there must be a better way.
You can try that :
<xs:simpleType name="basicLatin">
<xs:restriction base="xs:string">
<xs:pattern value="\p{IsBasicLatin}*"/>
</xs:restriction>
</xs:simpleType>
Unfortunately, for your requirement there isn't a way to restrict without using patterns.

How to force an element to contain only CDATA through XSD

My requirement is that any Xml file that will be validated against my schema should conform to following condition.
The OTHERWISE element can contain only CDATA section and nothing else.
Example
Valid XML: <OTHERWISE ContentURI=""><![CDATA[<html>Good-bye</html>]]></OTHERWISE>
Invalid XML: <OTHERWISE ContentURI="">ABC</OTHERWISE>
I am trying the following:
<xs:simpleContent>
<xs:restriction base="OtherwiseAtt">
<xs:pattern value="^<\!\[CDATA\[[a-zA-Z0-9]*\]\]>" />
</xs:restriction>
</xs:simpleContent>
Any thing can go inside the CDATA. I have put [a-zA-Z0-9]* just for testing purpose.
Please help me out.
Thanks
Sabri
The content between <![CDATA[ and ]]> is handled by the parser. Your XML file has been fully parsed by the time that it is validated. CDATA is basically another way to escape special characters. The validator will not have a way to determine if an element contains CDATA or not in the way that you wish.
The purpose of validation is to place controls on the structure of your documents. It is not and cannot enforce a particular method of escaping text.
Why would you need to require that the content is escaped by CDATA? This sounds like an attempt to handle a poor design choice at an earlier stage.

Resources