VB Script 如何使用XSD验证XML文档格式

Binhua Liu

 

XSD (XML Schema Definition) 文件用于定义XML文件的格式,通过XSD文件来验证XML文档格式是否符合定义中的规范,可以发现并指出XML文档中的错误。 我发现网络上介绍VB Script读取XML文档的文章很多,但是很难找到使用XSD文件验证XML Schema的文章。我这里介绍一个我写的例子。

 

有XML文件,定义了author.xml:

<?xml version="1.0" encoding="utf-8"?>
<Authors xmlns="ns-authors">
  <Author name="Bill" gender="male">
    <Book category="science">computer programming</Book>
    <Book category="science">C++ Programming</Book>
  </Author>
  <Author name="Lily" gender="female">
    <Book category="cooking">Sichuan Cuisine</Book>
    <Book category="travel">Travel in Britain</Book>
  </Author>
</Authors>

 

Schema文件author.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns="ns-authors"
    targetNamespace ="ns-authors"
    elementFormDefault="qualified">
  <xs:element name="Authors" >
    <xs:complexType>
      <xs:sequence minOccurs="1" maxOccurs="unbounded">
        <xs:element name="Author">
          <xs:complexType>
            <xs:sequence minOccurs="1" maxOccurs="unbounded">
              <xs:element name="Book">
                <xs:complexType>
                  <xs:simpleContent>
                    <xs:extension base="xs:string">
                      <xs:attribute name="category" type="xs:string" use ="optional"/>
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="name" type="xs:string" use ="required"/>
            <xs:attribute name="gender" use="optional">
              <xs:simpleType>
                <xs:restriction base="xs:string">
                  <xs:pattern value="male|female"/>
                </xs:restriction>
              </xs:simpleType>
            </xs:attribute>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

 

XMLValidation.vbs

xmlFile=WScript.Arguments(0)
xsdFile=WScript.Arguments(1)

Dim objDOM, objSchema, loadStatus, xmlParseErr
Set objDOM = CreateObject("MSXML2.DOMDocument.6.0")	
Set objSchema = CreateObject("MSXML2.XMLSchemaCache.6.0")

objSchema.add "ns-authors", xsdFile	
objDom.schemas = objSchema
objDOM.async = False
objDOM.validateOnParse = True
objDOM.resolveExternals = True

loadStatus = objDOM.Load(xmlFile)
Set xmlParseErr = objDOM.parseError
If xmlParseErr.errorCode <> 0 Then
	status="Fail"&":"&xmlParseErr.reason&"(LineNumber:"&XmlParseErr.line&")"
else
	status="Success"
End If
	WScript.Echo status

 

可以用下面的命令使用VS调试运行这个脚本:

CScript /nologo /X XMLReader.vbs Author.xml Author.xsd

 

简单分析一下脚本:

1, objDOM是实现IXMLDOMDocument3接口的对象

2, objSchema是实现IXMLDOMSchemaCollection2接口的对象

3, 第8行:objSchema.add "ns-authors", xsdFile. 第一个参数是XML使用的命名空间,即XML和XSD文件根节点中定义的xmlns="ns-authors"。如果不使用命名空间,在XML和XSD文件中将该命名空间删除,该参数需传递空字符串;第二个参数为XSD文件路径

4, 第14行loadStatus = objDOM.Load(xmlFile) 载入一个xmlFile文档到DOM对象,如果XML文档正确且符合Schema,则loadStatus=True,否则loadStatus=False

5, xmlParseErr为实现IXMLDOMParseError2接口的对象,其中最重要的3个属性值:

    errorCode:错误码

    reason:解析错误的原因

    line:错误所在行.

通过reason和line我们可以轻松定位XML验证的问题。

 

Binhua Liu原创,装载请注明出处http://www.cnblogs.com/Binhua-Liu

原文链接: https://www.cnblogs.com/Binhua-Liu/archive/2011/05/21/2052725.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    VB Script 如何使用XSD验证XML文档格式

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/25903

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午3:39
下一篇 2023年2月8日 上午3:40

相关推荐