溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用正則表達(dá)式進(jìn)行xml數(shù)據(jù)驗(yàn)證

發(fā)布時(shí)間:2021-03-12 11:24:23 來源:億速云 閱讀:296 作者:小新 欄目:編程語言

這篇文章主要介紹如何使用正則表達(dá)式進(jìn)行xml數(shù)據(jù)驗(yàn)證,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

xml Schema是定義XML的數(shù)據(jù)定義文件,以.xsd作為文件的擴(kuò)展名。它也以被用來定義一類XML文件。

通常,一些特殊含義的數(shù)據(jù)不能通過系統(tǒng)預(yù)設(shè)的數(shù)據(jù)結(jié)構(gòu)(類型)清楚地描述。
XML Schema 規(guī)范中聲明:可以通過facet來限制(restriction)簡(jiǎn)單類型,從而產(chǎn)生一些新的原子類型(Atomic types)。
Facet有pattern, enumeration,等等;
這里要說的是其中非常有用的一項(xiàng)是:
pattern+ 正則表達(dá)式語言(regular exPRession language)
結(jié)合正則表達(dá)式的強(qiáng)大功能,就可以進(jìn)行一些復(fù)雜的數(shù)據(jù)結(jié)構(gòu)的描述

Examples可以通過xmlspy, xmlwrite,或js/vbs 等進(jìn)行驗(yàn)證,下面舉出了js驗(yàn)證的例子(需要msxml4.0支持)


有關(guān)定義 XML Schema 的信息,可以在W3C 的 XML Schema 規(guī)范的第一部分中找到。有關(guān)內(nèi)置數(shù)據(jù)類型及其可用的局限性方面的信息,請(qǐng)檢 查 XML Schema 規(guī)范的第二部分。關(guān)于 這兩部分 XML Schema 規(guī)范的簡(jiǎn)易摘要,請(qǐng)查看 W3C Primer on XML Schema。

有關(guān)正則表達(dá)式,可以去http://www.regexlib.com/看看

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對(duì)象屬性
  sReturnVal = sReturnVal + "代碼:" + oXML.parseError.errorCode + "\n" ;
  sReturnVal = sReturnVal + "錯(cuò)誤原因:" + oXML.parseError.Reason + "\n" ;
  sReturnVal = sReturnVal + "錯(cuò)誤字符串:" + oXML.parseError.srcText + "\n" ;
  sReturnVal = sReturnVal + "錯(cuò)誤行號(hào)" + oXML.parseError.line + "\n" ;
  sReturnVal = sReturnVal + "錯(cuò)誤列數(shù):" + oXML.parseError.linepos + "\n" ;
 }
 else
 {
  sReturnVal = sReturnVal + "驗(yàn)證通過!"
 }

  alert(sReturnVal);
}

function window.onload()
{
 validate();
}
</SCRIPT>

以上是“如何使用正則表達(dá)式進(jìn)行xml數(shù)據(jù)驗(yàn)證”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI