Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD regular expression pattern: this or nothing

i'm trying to define an scheme rule in XSD, for which a string is 8 characters long:

<PostedDate>42183296</PostedDate>

and space-filling is also allowed:

<PostedDate>        </PostedDate>

which led me to the XSD:

<xs:simpleType name="DateFormat">
   <xs:restriction base="xs:string">
      <xs:length value="8" />            //exactly 8 characters long
</xs:simpleType>

but the value can also be empty (i.e. zero characters long):

<PostedDate></PostedDate>
<PostedDate />

which led me to naively try:

<xs:simpleType name="DateFormat">
   <xs:restriction base="xs:string">
      <xs:length value="8" />            //exactly 8 characters long
      <xs:length value="0" />            //exactly 0 characters long
</xs:simpleType>

Which of course isn't allowed.

As is often the case in XSD, most formats cannot be represented easily with XSD, so i opted to try a regular expression rule:

.{8} | ""

which trying to convert to XSD i type:

<xs:simpleType name="DateFormat">
    <xs:restriction base="xs:string">
        <xs:pattern value=".{8}|''" />
    </xs:restriction>
</xs:simpleType>

But it didn't work:

''20101111' is not facet-valid with respect to pattern '.{8}|''' for type 'DateFormat'

i also tried

  • <xs:pattern value="[0-9]{8}|''" />
  • <xs:pattern value="([0-9]{8})|('')" />
  • <xs:pattern value="(\d{8})|('')" />

Can anyone else thing of a pattern that solves the issue matching either - some specific pattern - empty

Bonus: can anyone point to the place in the XSD documentation that says that \d matches digits? Or what the other special pattern codes are?

like image 457
Ian Boyd Avatar asked Nov 19 '10 16:11

Ian Boyd


2 Answers

I may guess that patters should look like \d{8}| that means "eight digits or nothing", but not eight digits or two quotes. However this doesn't explain, why 20101111 is not matched. Are you sure that there are no whitespaces or other additional symbols in the element value?
\d is said to match digits in the section "F.1.1 Character Class Escapes"

like image 178
alpha-mouse Avatar answered Sep 20 '22 05:09

alpha-mouse


I also in the same situation like empty string is allowed otherwise it must 6 length numbers. At last I used like the following. This works for me

<xs:simpleType name="DateFormat">
    <xs:restriction base="xs:string">
        <xs:pattern value="|[0-9]{8}" />
    </xs:restriction>
</xs:simpleType>
like image 20
Akhil Raj Avatar answered Sep 22 '22 05:09

Akhil Raj