Jo101
Jo101

Reputation: 23

Patter in XML for a string

I want to write a pattern for element photo that the name of the photo end with png or jpg I tried this code but it doesn't work properly

 <xs:simpleType name="Photo">

    <xs:restriction base="xs:string">
        <xs:pattern value="[a-z]([.png] | [.jpg])"/>
    </xs:restriction>

</xs:simpleType>

Upvotes: 0

Views: 156

Answers (2)

Yitzhak Khabinsky
Yitzhak Khabinsky

Reputation: 22187

Because you are using XSD 1.1, you could use an assertion to be slightly more explicit with the validation logic. Much easier to create, and much easier to maintain going forward.

Check it out.

XSD 1.1

<xs:simpleType name="Photo">
    <xs:restriction base="xs:string">
        <xs:assertion test="ends-with($value, '.jpg') or ends-with($value, '.png')"/>
    </xs:restriction>
</xs:simpleType>

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1501113

I haven't used regular expressions within XSD before, but assuming it's just a normal regular expression, you want:

[a-z]+\.(png|jpg)

That means:

  • At least one (but optionally many) a-z characters
  • Then a period
  • Then png or jpg

Note that you might want to allow other characters in the first part, e.g. digits and upper case letters. You should consider non-ASCII characters too.

Upvotes: 1

Related Questions