I would like to create a restriction for an XSD type to only allow an element of size 0 to 64, a dot, and another element of size 0 to 64. I tried this, but without success.
<xs:simpleType name="myString_Type">
<xs:restriction base="xs:string">
<xs:pattern value="^([a-zA-Z\-]){0-64}.$([a-zA-Z\-]){0-64}"/>
</xs:restriction>
</xs:simpleType>
Thanks.
^
and $
aren't used for regex in XSD - it always matches from the start and end, as if they were there. Therefore, just omit them:
[a-zA-Z\-]{0,64}\.[a-zA-Z\-]{0,64}
And escape the .
(or use a character class as NullUserException said).
From the XML Schema part 2: datatypes spec:
Unlike some popular regular expression languages (including those defined by Perl and standard Unix utilities), the regular expression language defined here implicitly anchors all regular expressions at the head and tail, as the most common use of regular expressions in ·pattern· is to match entire literals.
Their example is to use A.*Z
not ^A.*Z$
Because ^
and $
aren't special characters, it will just try to match them in your xml document.
It's been said that unix is a collection of incompatible regular expression syntaxes, so by not following the unix standard, they are following the unix tradition.
You can test this example at: http://www.utilities-online.info/xsdvalidation/ (test instance from NullUserException)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="eg" type="myString_Type"/>
<xs:simpleType name="myString_Type">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z\-]{0,64}\.[a-zA-Z\-]{0,64}"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
<eg>something.something-else</eg>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With