2
votes

I have to create a XSD file. A complex type of mine contains a SimplyType with base xs:string and I would like to give it the following pattern restriction:

[X|X/R|X/L]

So my intention is to allow either X, X/R or X/L.

When trying to verify a xml against this schema XMLSpy tells me that only X, R, L or / is allowed. But not the expected combination.

What am I doing wrong. According to my research a / does not need a escape character.

Thank you very much

1
Use X|X/R|X/L, without brackets. Right, no need to escape /, it is not a special char. - Wiktor Stribiżew
Please consider accepting the answer below (see How to accept SO answers). - Wiktor Stribiżew

1 Answers

2
votes

The reason your pattern is failing is that by using Character Classes or Character Sets ([ ]), you're specifying that any of the listed characters are acceptable -- not what you want.

You are right as for the forward slash: it is not a special regex metacharacter in any regex flavor, just sometimes it is used as a regex delimiter in flavors that allow regex literal notations (like /^abc$/).

So, all you need to use is

<xs:pattern value="X|X/R|X/L"/>

Or less verbose with [RL] (the character class matching either R or L) instead of repeating the last two values:

<xs:pattern value="X|X/[RL]"/>

Or even

<xs:pattern value="X(/[RL])?"/>

Here, X is matched, then /R or /L are matched 1 or 0 times, i.e. optionally.

XSD patterns are always anchored by default (=must match the whole string), so you should not add any ^ or $ at the start/end.


Here is a full supporting sample XSD:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" > 
  <xsd:element name="r">
    <xsd:simpleType>
      <xsd:restriction base="xsd:string"> 
        <xsd:pattern value="X|X/R|X/L"/> 
      </xsd:restriction>
    </xsd:simpleType>
  </xsd:element>
</xsd:schema>