Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression in XSD schema: mix and max size of two combined elements

Tags:

regex

xsd

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.

like image 251
Luis Andrés García Avatar asked Oct 06 '22 00:10

Luis Andrés García


1 Answers

^ 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>
like image 127
13ren Avatar answered Oct 11 '22 01:10

13ren