我是靠谱客的博主 细腻飞机,这篇文章主要介绍使用正则表达式进行xml数据验证的代码实例详解,现在分享给大家,希望可以做个参考。

xml Schema是定义XML的数据定义文件,以.xsd作为文件的扩展名。它也以被用来定义一类XML文件。

通常,一些特殊含义的数据不能通过系统预设的数据结构(类型)清楚地描述。
XML Schema 规范中声明:可以通过facet来限制(restriction)简单类型,从而产生一些新的原子类型(Atomic types)。
Facet有pattern, enumeration,等等;
这里要说的是其中非常有用的一项是:
pattern+ 正则表达式语言(regular exPRession language)
结合正则表达式的强大功能,就可以进行一些复杂的数据结构的描述

Examples可以通过xmlspy, xmlwrite,或js/vbs 等进行验证,下面举出了js验证的例子(需要msxml4.0支持)


有关定义 XML Schema 的信息,可以在W3C 的 XML Schema 规范的第一部分中找到。有关内置数据类型及其可用的局限性方面的信息,请检 查 XML Schema 规范的第二部分。关于 这两部分 XML Schema 规范的简易摘要,请查看 W3C Primer on XML Schema。

有关正则表达式,可以去http://www.regexlib.com/看看

examples:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*** 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>
登录后复制

以上就是使用正则表达式进行xml数据验证的代码实例详解的内容,更多相关内容请关注靠谱客(www.uoften.com)!


最后

以上就是细腻飞机最近收集整理的关于使用正则表达式进行xml数据验证的代码实例详解的全部内容,更多相关使用正则表达式进行xml数据验证内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(119)

评论列表共有 0 条评论

立即
投稿
返回
顶部