Ben Ashton
Ben Ashton

Reputation: 1405

How to validate an XSD string at specified position

I was wondering if it's possible to perform XSD validation against the first character of a string? I have a feeling not, and I couldn't find any related search results for this question.

I have a gateway service which accepts several XML data strings, which are all matched against my XSD file. In this case, I have a particular data string where the first character can only contain the characters "S" and "N", where the rest of the string can be Alpha-numeric.

Currently I just define it as a string, with a 46 character limit:

<xs:element name="navcode">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:maxLength value="46"/>
      </xs:restriction>
    </xs:simpleType>
</xs:element>

Does anyone know if this is possible or not? Thanks for all help!

Upvotes: 1

Views: 842

Answers (1)

Petru Gardea
Petru Gardea

Reputation: 21658

Replace your maxLength facet with a pattern facet:

    <xs:pattern value="[NS].{0,45}"/>

It should hold for what you need. The . means anything except new line. For more info on regular expressions supported by the XSD spec, take a look here.

Upvotes: 2

Related Questions