Question

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?

Was it helpful?

Solution

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"

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top