Home > Article > Backend Development > Detailed explanation of code examples using regular expressions for xml data validation
xml Schema is a data definition file that defines XML, with .xsd as the file extension. It can also be used to define a class of XML files.
Usually, some data with special meaning cannot be clearly described by the system's preset data structure (type).
The XML Schema specification states that simple types can be restricted through facets, thereby generating some new atomic types (Atomic types).
Facet has pattern, enumeration, etc.;
What I want to say here is that one of the very useful ones is:
pattern+ regular expression language (regular exPRession language)
Combined with the power of regular expressions function, you can describe some complex data structures
Examples can be verified through xmlspy, xmlwrite, or js/vbs, etc. The following is an example of js verification (requires msxml4.0 support)
Information about defining XML Schema can be found in Part 1 of the W3C's XML Schema specification. For information about built-in data types and the limitations of their availability, check Part 2 of the XML Schema specification. For a brief summary of these two parts of the XML Schema specification, see W3C 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) { //参看书籍教程中parseError对象属性 sReturnVal = sReturnVal + "代码:" + oXML.parseError.errorCode + "\n" ; sReturnVal = sReturnVal + "错误原因:" + oXML.parseError.Reason + "\n" ; sReturnVal = sReturnVal + "错误字符串:" + oXML.parseError.srcText + "\n" ; sReturnVal = sReturnVal + "错误行号" + oXML.parseError.line + "\n" ; sReturnVal = sReturnVal + "错误列数:" + oXML.parseError.linepos + "\n" ; } else { sReturnVal = sReturnVal + "验证通过!" } alert(sReturnVal); } function window.onload() { validate(); } </SCRIPT>
The above is the use of regular expressions Detailed explanation of the code example for expression xml data verification. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!