Skip to content

Instantly share code, notes, and snippets.

@MohammedJabarullah
Created August 1, 2012 14:28
Show Gist options
  • Save MohammedJabarullah/3227318 to your computer and use it in GitHub Desktop.
Save MohammedJabarullah/3227318 to your computer and use it in GitHub Desktop.
XPath to check a XML Schema (XSD) for elements and types that don't have range / size restrictions
//xs:element[starts-with(@type, 'xs:')]/@name
| //xs:simpleType[
xs:restriction[not(xs:length) and
not(xs:minInclusive and xs:maxInclusive) and
not(xs:maxLength) and not(xs:enumeration) and
not(starts-with(@base, 'xs:dateTime')) and
not(xs:pattern[not(empty(@value))])]
]/@name
<?xml version="1.0" encoding="UTF-8"?>
<!-- Schema source: w3schools http://www.w3schools.com/schema/schema_example.asp -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="stringtype">
<xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="inttype">
<xs:restriction base="xs:positiveInteger"/>
</xs:simpleType>
<xs:simpleType name="dectype">
<xs:restriction base="xs:decimal"/>
</xs:simpleType>
<xs:simpleType name="orderidtype">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="shiptotype">
<xs:sequence>
<xs:element name="name" type="stringtype"/>
<xs:element name="address" type="stringtype"/>
<xs:element name="city" type="stringtype"/>
<xs:element name="country" type="stringtype"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="itemtype">
<xs:sequence>
<xs:element name="title" type="stringtype"/>
<xs:element name="note" type="stringtype" minOccurs="0"/>
<xs:element name="quantity" type="inttype"/>
<xs:element name="price" type="dectype"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="shipordertype">
<xs:sequence>
<xs:element name="orderperson" type="stringtype"/>
<xs:element name="shipto" type="shiptotype"/>
<xs:element name="item" maxOccurs="unbounded" type="itemtype"/>
</xs:sequence>
<xs:attribute name="orderid" type="orderidtype" use="required"/>
</xs:complexType>
<xs:element name="shiporder" type="shipordertype"/>
</xs:schema>
@MohammedJabarullah
Copy link
Author

This gist is helpful if you have to deal with large XSDs from external sources. Running check_xsd_xpath.xpath against shipping_sample_schema.xsd (or any of your XSD) using a XML tool like Altova XMLSpy will generate a list of elements and types that don't have size/type restrictions.

E.g. Output for shipping_sample_schema.xsd

  • name stringtype
  • name inttype
  • name dectype

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment