XSD regex pattern: it's or nothing

I am trying to define a schema 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 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. null characters):

 <PostedDate></PostedDate> <PostedDate /> 

which made me 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, is not allowed.

As is often the case in XSD, most formats cannot be easily represented by XSD, so I decided to try the regex rule:

 .{8} | "" 

which is 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 this did not 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 from the template that solves the problem corresponding - some specific template - be empty

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

+6
regex xsd xsd-validation
source share
2 answers

I can guess that the patterns should look like \d{8}| , which means eight digits or nothing, but not eight digits or two quotation marks. However, this does not explain why 20101111 not mapped. Are you sure that there are no spaces or other additional characters in the element value?
\d is said to match the numbers in the "F.1.1 Character Class Escapes" section

+11
source share

I am also in the same situation as an empty string, otherwise it should have 6 length numbers. Finally, I used the following. It works for me

 <xs:simpleType name="DateFormat"> <xs:restriction base="xs:string"> <xs:pattern value="|[0-9]{8}" /> </xs:restriction> </xs:simpleType> 
+3
source share

All Articles