JBPM流程部署校验之javascript利用XSD校验XML

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文件;

校验类库

WFTH = {};
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 " );
}
};

客户端调用

//独立的xml和xsd
alert(xsdValidationManager.validationWithXSD( " a.xml " , " note.xsd " , "http://www.w3school.com.cn/" ));
//内联xsd
alert(xsdValidationManager.validationWithXSD(
" a.xml " ));

附xsd及xml实例
内联模式之指定了命名空间

//xsd文件
<? 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>

内联模式之无命名空间

//xml文件
<? xml version = " 1.0 " ?>
< 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文件

//xml
<? xml version = " 1.0 " ?>
< 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值