We have a requirement to allow all the characters (including special chars) except #, $ and space to an XSD element.
I’ve tried the regex as [^$#\s]* but didn’t work. Can you please help with the resolution as I'm not able to figure out.
I tried your regex in a XSD and it works as expected.
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://Scratch.SO53903548" targetNamespace="http://Scratch.SO53903548" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="SpecialString2">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[^$#\s]*" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Will quite happily validate the below, but fail on $,# or space
<ns0:Root xmlns:ns0="http://Scratch.SO53903548">
<SpecialString2>thequickbrownfoxjumpedoverthelazydog#THEQUICKBROWNFOXJUMPEDOVERTHELAZYDOG!~`#%^&*()-_+=</SpecialString2>
</ns0:Root>
Related
I have the following XSD to validate this XML, but I don't know why it says that is valid. Notice that the pattern only checks for date, not date + time. Do you know how can I put a pattern to take only as valid the date + time? Thank you very much in advance.
XML:
<IN_PARAM>
<DATE_FROM>20/01/2018 10:35:00</DATE_FROM>
<DATE_TO>31/12/2019 18:40:00</DATE_TO>
</IN_PARAM>
XSD:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="IN_PARAM">
<xs:complexType>
<xs:sequence>
<xs:element name="DATE_FROM" minOccurs="1" maxOccurs="1" />
<xs:element name="DATE_TO" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="DATE_FROM">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{2}/[0-9]{2}/[0-9]{4}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="DATE_TO">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{2}/[0-9]{2}/[0-9]{4}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
I got it. You have to define a type, and that type must have the pattern do you want.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="IN_PARAM">
<xs:complexType>
<xs:sequence>
<xs:element name="DATE_FROM" type="DateTimeType" minOccurs="1" maxOccurs="1" />
<xs:element name="DATE_TO" type="DateTimeType" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="DateTimeType">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{2}/[0-9]{2}/[0-9]{4}"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
And of course, if you need it, here is the pattern for dd/MM/yyyy hh:mm:ss:
<xs:pattern value="(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/[0-9]{4} (0[0-9]|[1][0-9]|2[1-3]):([0-5][0-9]):([0-5][0-9])"/>
I need a sample XSD to support multiple email recipients in a new element. I require each recipient email address in a different element. Can anyone help me with explanation?
Example:
<EmailReceipts>
<address1></address1>
<address2></address2>
</EmailReceipts>
First off, I'd recommend not embedding an index number in the address elements:
<EmailReceipts>
<address>john#example.com</address>
<address>mary#example.org</address>
</EmailReceipts>
Then this XSD will validate the above XML (as well as other XML documents with additional address elements):
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="EmailReceipts">
<xs:complexType>
<xs:sequence>
<xs:element name="address" maxOccurs="unbounded" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The above XSD will allow any string contents for the address elements. If you've like to be more strict, you could use a regular expression to limit the values for address:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="EmailReceipts">
<xs:complexType>
<xs:sequence>
<xs:element name="address" maxOccurs="unbounded" type="EmailAddressType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="EmailAddressType">
<xs:restriction base="xs:string">
<xs:pattern value="([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*#([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Note that the above regular expression is one of many possible, each having various degrees of generality and specificity over a syntax that is more involved than you might imagine.
I would like to know if it is possible to validate attribute names by pattern using XML Schema. In other words, I would like to describe a set of acceptable attribute names for a given type, using a pattern (such as a regular expression).
Lets say I have the following XML data I would like to validate:
<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://mywebsite.com/myns">
<somename data-someattr1="value1"
data-someattr2="value2"/>
</root>
How can I describe that attributes of elements with name "somename" can only have attributes with name beginning by "data-"? Is this even possible?
Try something along the lines of:
<xs:simpleType name="somename">
<xs:restriction base="xs:string">
<xs:pattern value="^data-"/>
</xs:restriction>
</xs:simpleType>
The regex ^data- means "beginning with 'data-'", as you require.
EDIT:
I misunderstood the question, sorry... Here is a more relevant answer:
As I understand it, you cannot pattern match attribute names in an XSD - so there is no solution to your problem using an XSD alone. However, you may find one of the following XML schema elements helpful in constructing a solution:
XML choice (http://www.w3schools.com/schema/el_choice.asp) - so you could (possibly?) list all "data-" attribute names explicitly.
XML any (http://www.w3schools.com/schema/schema_complex_any.asp) - so you could then perform any additional validation steps via some other method.
Well, as it was said you can't validate attribute names, but you still can go different way and transform your xml to some kind of:
<root>
<element>
<data name='data-attr1' value='v1'/>
<data name='data-attr2' value='v2'/>
<data name='data-attr3' value='v3'/>
</element>
</root>
So now you can validate fake attribute name - data name='data-attr1' as well as values. Your schema might look like this:
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified' attributeFormDefault='unqualified'>
<xs:element name='root'>
<xs:complexType>
<xs:sequence>
<xs:element name='element'>
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs='10' name='data'>
<xs:complexType>
<xs:attribute name='name' use='required'>
<xs:simpleType>
<xs:restriction base='xs:string'>
<xs:pattern value='data-.*' />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name='value' use='required' />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I have an attribute that can be any string, but if it has starting and ending brackets "^[.*]$" - it must be only one of the following specific values:
"[EVENT]"
and
"[PROTOCOL]"
So, "[EVENT]", "[PROTOCOL]", "SomeString" - are correct, but "[SomeString]" - isn't.
How can I achieve this?
Use an xs:simpleType and regular expressions to restrict the base xs:string type. You can have more than one xs:pattern to keep the alternative patterns simple. The element has to match one of the patterns or validation will fail. Since the patterns are regular expressions, special characters like "[" and "]" have to be escaped when used as literals.
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="elem" type="myElemType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="myElemType">
<xs:attribute name="attrib" type="myAttribType"/>
</xs:complexType>
<xs:simpleType name="myAttribType">
<xs:restriction base="xs:string">
<xs:pattern value="\[EVENT\]"/><!-- "[EVENT]": okay -->
<xs:pattern value="\[PROTOCOL\]"/><!-- "[PROTOCOL]": okay -->
<xs:pattern value="[^\[].*"/><!-- Starts with anything but "[": okay -->
</xs:restriction>
</xs:simpleType>
</xs:schema>
XML:
<root>
<elem attrib="[EVENT]"/>
<elem attrib="[PROTOCOL]"/>
<elem attrib="SomeString"/>
<elem attrib="SomeString]"/>
<elem attrib=" [SomeString] "/>
<!-- All the above are okay; the ones below fail validation -->
<elem attrib="[SomeString]"/>
<elem attrib="[SomeString"/>
</root>
Modify the regular expressions to your heart's content, e.g., to fail the example with leading and/or trailing spaces.
Edited to reflect OP's comment that "[SomeString" should also be invalid.
I like #Burkart's use of separate xs:pattern elements better (+1), but I had this pending while awaiting clarification in comments ("[SomeString" without a closing bracket should be invalid), so I'll post it anyway in case anyone might find it useful. Read the regular expression as EVENT or PROTOCOL within brackets or any string that doesn't start or end with a bracket.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:attribute name="attr">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\[(EVENT|PROTOCOL)\]|[^\[].*[^\]]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
I am newbie in xml schema. Is there any possiblility to define that element starts with some characater or symbol. I mean to say, <xs:element minOccurs="1" maxOccurs="1" name="Header">
<xs:complexType>
<xs:sequence>
<xs:sequence>
<xs:element maxOccurs="1" minOccurs="1" name="NAME_STUDENTS">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
is there any possible way to define a pattern or tag in xml schema that name of student starts with 'P'??And schema should recognize the text as element NAME_STUDENTS only if the text starts with 'P'
I'm not sure if I fully understand your question but concerning the name element restriction you should look into Xml Schema Regular Expressions.
In your case this would look like this:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:element name="Header">
<xs:complexType>
<xs:sequence>
<xs:element name="NAME_STUDENTS" type="filtered-students" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="filtered-students">
<xs:restriction base="xs:string">
<xs:pattern value="^[P]?"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
... but I must confess I'm not really a regex hero so you may want to check the pattern with somebody more proficient.