user712027
user712027

Reputation: 572

xsd: how to check a number code

I have an xsd file and I need to introduce a new validation. I need to check a city code. The city code is 68, an integer that its always the same.

How can I check for that?

Thanks!

Here's my code:

 <xsd:element name="CodCity">
<xsd:annotation><xsd:documentation>City Code has to be 68.</xsd:documentation></xsd:annotation>
<xsd:simpleType>
<xsd:restriction base="xsd:int"></xsd:restriction>
</xsd:simpleType>
</xsd:element>

Upvotes: 1

Views: 249

Answers (2)

Rookie Programmer Aravind
Rookie Programmer Aravind

Reputation: 12154

Could have been more specific if you had posted input XML.

Assuming your different sample input XML I am posting answers:

Sample Input XML:

<?xml version="1.0" encoding="utf-8"?>
<testing>68</testing>

XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="testing" type="citycode" />
  <xs:simpleType name="citycode">
    <xs:restriction base="xs:int">
      <xs:pattern value="68"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

Sample Input XML:

<?xml version="1.0" encoding="utf-8"?>
<testing>Blah blah City code is: 68</testing>

XSD [using regex pattern]:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="testing" type="pattern" />
  <xs:simpleType name="pattern">
    <xs:restriction base="xs:string">
      <xs:pattern value=".*68"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

where .* is any character (except linefeed), 68 - number 68

one more sample Input XML:

<?xml version="1.0" encoding="utf-8"?>
<testing>Blah blah City code is: '68' and something else</testing>

XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="testing" type="pattern" />
  <xs:simpleType name="pattern">
    <xs:restriction base="xs:string">
      <xs:pattern value=".*68.*"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

Upvotes: 0

Ben
Ben

Reputation: 1927

Just add a single xsd:enumeration to your xsd:restriction: http://www.w3schools.com/schema/schema_facets.asp

<xsd:restriction base="xsd:int">
  <xsd:enumeration value="68"/>
</xsd:restriction>

Upvotes: 1

Related Questions