Using regular expressions for XML data validation

Source: Internet
Author: User
Tags data structures expression regular expression string version xmlns
xml| Data | regular  

An XML schema is a data definition file that defines an XML, with an. xsd as the extension of the file. It is also used to define a class of XML files.

Typically, data of a particular meaning cannot be clearly described by a system-preset structure (type).
The XML Schema specification declares that you can restrict (restriction) simple types by facet, resulting in some new atomic types (Atomic types).
Facet have pattern, enumeration, and so on;
One of the most useful things to say here is:
pattern+ Regular Expression language (regular expression language)
Combined with the powerful functions of regular expressions, we can describe some complex data structures.

Examples can be validated by XMLSpy, Xmlwrite, or Js/vbs, and the following examples of JS validation (need msxml4.0 support)


Information about defining an XML schema can be found in the first part of the XML Schema specification of the consortium . For information about the built-in data types and their available limitations, check the second part of the XML Schema specification . For an easy summary of the two parts of the XML Schema specification, see the Primer on XML Schema.

For regular expressions, you can go to http://www.regexlib.com/ to see


Examples:

/*** Examples.xml ***/
<?xml version= "1.0" encoding= "gb2312"?>
<root xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation= " Examples.xsd ">
<user>
<name>test</name>
<email>moonpiazza@hotmail.com</email>
<ip>127.0.0.1</ip>
<color> #000000 </color>
</user>
<user>
<name>guest</name>
<email>guest@371.net</email>
<ip>202.102.224.25</ip>
<color> #FFFFFF </color>
</user>
</root>


/*** examples.xsd ***/
<?xml version= "1.0" encoding= "gb2312"?>
<xsd:schema xmlns:xsd= "http://www.w3.org/2001/XMLSchema" >

<xsd:element name= "root" type= "root"/>

<xsd:complextype name= "Root" >
<xsd:sequence>
<xsd:element name= "user" type= "user" minoccurs= "0" maxoccurs= "unbounded"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complextype name= "User" >
<xsd:sequence>
<xsd:element name= "name" type= "xsd:string"/>
<xsd:element name= "Email" type= "email"/>
<xsd:element name= "IP" type= "IP"/>
<xsd:element name= "Color" type= "Color"/>
</xsd:sequence>
</xsd:complexType>

<xsd:simpletype name= "Email" >
<xsd:restriction base= "Xsd:string" >
<xsd:pattern value= "([A-za-z0-9_\-\.] +) @ (\[[0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\.) | (([a-za-z0-9\-]+\.) +)) ([a-za-z]{2,4}| [0-9] {1,3}) (\]?)" />
</xsd:restriction>
</xsd:simpleType>

<xsd:simpletype name= "IP" >
<xsd:restriction base= "Xsd:string" >
<xsd:pattern value= "(25[0-5]|2[0-4][0-9]|[ 0-1]{1}[0-9]{2}| [1-9] {1} [0-9] {1}| [1-9]) \. (25[0-5]|2[0-4][0-9]| [0-1] {1} [0-9] {2}| [1-9] {1} [0-9] {1}| [1-9]|0) \. (25[0-5]|2[0-4][0-9]| [0-1] {1} [0-9] {2}| [1-9] {1} [0-9] {1}| [1-9]|0) \. (25[0-5]|2[0-4][0-9]| [0-1] {1} [0-9] {2}| [1-9] {1} [0-9] {1}| [0-9]) " />
</xsd:restriction>
</xsd:simpleType>

<xsd:simpletype name= "Color" >
<xsd:restriction base= "Xsd:string" >
<xsd:pattern value= "#? ([a-f]| [a-f]| [0-9]) {3} ([a-f]| [a-f]| [0-9]) {3})? " />
</xsd:restriction>
</xsd:simpleType>

</xsd:schema>


/*** examples.htm ***/
<script language= "JavaScript" >
function Validate ()
{
var oXML;
var nparseerror;
var sreturnval;

oXML = new ActiveXObject ("MSXML2. Domdocument.4.0 ");
Oxml.async = false;
Oxml.validateonparse = true;

Oxml.load ("Examples.xml");

Nparseerror = OXML.parseError.errorCode;
Sreturnval = "";

if (0!= nparseerror)
{
Refer to the ParseError object properties in the book tutorial
Sreturnval = Sreturnval + "code:" + OXML.parseError.errorCode + "\ n";
Sreturnval = sreturnval + "Error Reason:" + OXML.parseError.Reason + "\ n";
Sreturnval = sreturnval + "error string:" + OXML.parseError.srcText + "\ n";
Sreturnval = sreturnval + "error line number" + OXML.parseError.line + "\ n";
Sreturnval = sreturnval + "error column number:" + OXML.parseError.linepos + "\ n";
}
Else
{
Sreturnval = Sreturnval + "Verify through!" "
}

alert (sreturnval);
}

function Window.onload ()
{
Validate ();
}
</SCRIPT>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.