JBPM流程定义校验之javascript利用XSD校验XML
上篇我们简单的介绍了一下XSD的相关知识,今天我们来学习一下在javascript中怎样利用XSD来验证xml;现在各大浏览器厂商中,好像就微软公布了相应的接口,其他的厂商需要我们自己来实现类库;所以今天我们讲的是在IE下利用XSD来验证xml。
我们先了解一下MSXML个版本对XML验证的支持情况
Version | Support |
---|---|
MSXML | 仅支持 DTD 验证,不支持xsd方式. |
MSXML 2.0 through 4.0 | 支持 XML-Data Reduced (XDR) 验证. |
MSXML 4.0 and later | 支持xsd验证方式. |
MSXML 5.0 for Microsoft Office Applications | 支持内联xsd验证. |
MSXML 6.0 | 移除 XML-Data Reduced (XDR) 验证方式. |
从图中我们可以看到从MSXML4以后的版本就都支持直接利用XSD来验证xml了,试了一下,高版本的抛出的错误信息更精确和详细。
MSXML支持两种方式的验证,一种是将xsd直接嵌入到xml文件中(内联xsd),一种是验证是动态的指定XSD文件;
校验类库
var xsdValidationManager = WFTH.XSDValidationManager = {
validationWithXSD: function (xmlFilePath, xsdFilePath, nameSpace) {
var xd = new ActiveXObject( " MSXML2.DOMDocument.6.0 " );
// 直接指定使用的xsd文件,否则需要内联xsd文件
if (arguments.length == 3 && xsdFilePath != null && xsdFilePath != undefined) {
var xs = new ActiveXObject( " MSXML2.XMLSchemaCache.6.0 " );
xs.add(nameSpace, xsdFilePath);
xd.schemas = xs;
} ;
xd.async = false ;
xd.validateOnParse = true ;
xd.resolveExternals = true ;
xd.load(xmlFilePath);
if (xd.parseError.errorCode != 0 ) {
return ( " Validation failed on " + xmlFilePath + " \n===================== " + " \nReason: " + xd.parseError.reason + " \nSource: " + xd.parseError.srcText + " \nLine: " + xd.parseError.line + " \n " );
}
else
return ( " Validation succeeded for " + xmlFilePath + " \n======================\n " + xd.xml + " \n " );
}
};
客户端调用
alert(xsdValidationManager.validationWithXSD( " a.xml " ));
附xsd及xml实例
内联模式之指定了命名空间
<? xml version = " 1.0 " ?>
< note xmlns = " http://www.w3school.com.cn " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation = " http://www.w3school.com.cn note.xsd " >
< to > George < / to>
< from > John < / from>
< heading > Reminder < / heading>
< body > Don ' t forget the meeting!</body>
<name></name>
</note>
//xsd文件
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3school.com.cn" xmlns="http://www.w3school.com.cn" elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
内联模式之无命名空间
< note xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:noNamespaceSchemaLocation = " note.xsd " >
< to > George < / to>
< from > John < / from>
< heading > Reminder < / heading>
< body > Don ' t forget the meeting!</body>
<name></name>
</note>
//xsd文件
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3school.com.cn" elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
单独的xsd和xml文件
< note xmlns = " http://www.w3school.com.cn " >
< to > George < / to>
< from > John < / from>
< heading > Reminder < / heading>
< body > Don ' t forget the meeting!</body>
<name></name>
</note>
//xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3school.com.cn" xmlns="http://www.w3school.com.cn" elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>