XML Schema Recipes

Don’t underestimate the power of validating XML with a schema. I’ve often overlooked much of what an XML schema is capable of, generally doing the minimum needed to create one. But investing a little more love with a schema can save time and headache (especially when working in situations involving multiple systems and interfaces).

Creating custom types with regular expressions is an extremely powerful tool:

The GUID type:

	<xsd:simpleType name="GUIDType">
		<xsd:restriction base="xsd:string">
			<xsd:pattern value="\{[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}\}"/>
		</xsd:restriction>
	</xsd:simpleType>

Phone Number:

	<xsd:simpleType name="PhoneNumberType">
		<xsd:restriction base="xsd:string">
			<xsd:pattern value="[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]"/>
		</xsd:restriction>
	</xsd:simpleType>

Postal Code:

	<xsd:simpleType name="PostalCodeType">
		<xsd:restriction base="xsd:string">
			<xsd:pattern value="[A-CEGHJ-NPR-TVXY]\d[A-Z] \d[A-Z]\d"/>
		</xsd:restriction>
	</xsd:simpleType>

SIN:

	<xsd:simpleType name="SINType">
		<xsd:restriction base="xsd:string">
			<xsd:pattern value="\d{3} \d{3} \d{3}"/>
		</xsd:restriction>
	</xsd:simpleType>
You can leave a response, or trackback from your own site.
blog comments powered by Disqus