本文出自:http://www.computerworld.com.cn 作者: 北京邮电大学 张剑 (2002-02-12 07:15:00) 1、制定Schema 考虑如下XML文档books.xml: <?xml version=“1.0”?> <booklist> <book> <title>Straight Talk About Computers </title> <author>Lars Peterson</author> </book> <book> <title>You Can Comabt Computer Stress</title> <author>Lars Peterson</author> <author>Carlos Diaz</author> </book> </booklist> 为上述XML文档制定Schema时,可以先为<title>和<author>元素制定规则,用<ElementType>定义它们为只能包含字符串的文本元素: <ElementType name=“title” content=“textOnly” dt:type=“string” /> <ElementType name=“author” content=“textOnly” dt:type=“string” /> 然后说明<book>只能包含子元素,且子元素出现的顺序是一定的。随后在内部用<element>定义<book>包括一个<title>和任意个<author>子元素: <ElementType name=“book” content=“eltOnly” order=“seq”> <element type=“title” /> <element type=“author” maxOccurs=“*” /> </ElementType> 最后用同样的方法说明根元素<booklist>: <ElementType name=“booklist” content=“eltOnly”> <element type=“book” minOccurs=“0” maxOccurs=“*” /> </ElementType> 下面的这个例子中定义了“isbn”类型的属性:数据类型是字符串,并规定如果元素中引用该属性,那么就必需赋值。 <AttributeType name=“isbn” dt:type=“string” required=“yes” /> <ElementType name=“book” content=“eltOnly”> <attribute type=“isbn” /> </ElementType> 2、XML的可扩展性 由于XML Schema是一种内容开放的模型,这种可扩展性就意味着用户可以在XML Schema中使用自己定义的元素和属性。例如: <ElementType name=“price” xmlns:myExt=“urn:myextensions”dt:type=“float”> <myExt:salestax>17.5</myExt:salestax> <myExt:bulkbuy>20</myExt:bulkbuy> <myExt:discount>5.0</myExt:discount> </ElementType> 其中本地的属性定义只声明了<price>元素为“float”类型,而通过名称空间又引用了其他3个标签来分别说明书籍的销售税、批量定额和折扣率。这种扩展性增强了Schema的灵活性。 我们还可以使用DOM技术来访问扩展的Schema: <book isbn=“9-001-122-01”> <title>Straight Talk About Computers</title> <price>19.99</price> </book> 假设上述XML数据就是根据刚才的Schema定义的,而且我们已经用DOM定位了<book>元素,那么,接下来就可以访问<price>节点,并利用它的definition属性从Schema中得到<ElementType>的定义: Set priceNode = bookNode.selectSingleNode(“price”) Set priceElementTypeNode = priceNode.definition salestax = priceElementTypeNode.childNodes(0).text bulkbuy = priceElementTypeNode.childNodes(1).text discount = priceElementTypeNode.childNodes(2).text 这样,就可以根据需要处理这些数据了。 3、在客户机/服务器环境中验证XML 在C/S(客户机/服务器)环境中,客户机和服务器都可以使用Schema来验证文档。在客户机端验证的对象可以是从服务器发来的文档,也可以是即将发送出去的文档。下面的这个例子就是用一个名为CustomerSchema.xml的Schema来验证docSubmit的xml文档,然后发送给服务器: Set rootnode = docSubmit.documentElement rootnode.setAttribute “xmlns”, “x-schema:http://server/CustomerSchema.xml” DOM首先取得根元素的访问权,然后把xmlns设为Schema的地址,使之开始验证。 相对客户机而言,在服务器上进行验证的意义更重大,在实际中也更为常见。由于连接到服务器上的客户数非常多,所以有必要在处理它们发送来的数据前,检查收到的文档是否与预期的结构一致。验证步骤如下: 1.将XML文档载入DOM树,开始验证: Set docReceived = CreateObject(“Microsoft.XMLDOM”) docReceived.validateOnParse = True docReceived.async = False docReceived.load Request 2.判断是否链接XML Schema(只需检验一下根元素中是否设置了xmlns属性): Set rootnode = docReceived.documentElement If rootnode.getAttribute(“xmlns”) = “x-schema:http://sever/CustomerSchema.xml” Then 〈!--有,开始验证!--〉 Else 〈!--没有…… --〉 End If 3.如果没有链接,服务器就要指定一个Schema进行链接,然后将更新的文档重新装入到另一个DOM树中: path = Server.mapPath(“CustomerSchema.xml”) attr = “x-schema:” & path rootnode.setAttribute “xmlns”, attr Set docTested = CreateObject(“Microsoft.XMLDOM”) docTested.validateOnParse = True docTested.async = False docTested.loadXML docReceived.xml 由于XML Schema具有许多优秀的特性,国际上许多著名公司和大企业纷纷开始向Schema倾斜,比如微软的IE 5.0就已经支持XML Schema。虽然Schema还需要在不断的实践中逐步完善,但XML Schema取代DTD已经是大势所趋,因此,笔者建议应学习并掌握这一技术。

评论