Home  >  Article  >  Backend Development  >  Convert ADO recordset to XML using XSLT

Convert ADO recordset to XML using XSLT

黄舟
黄舟Original
2017-02-27 16:33:381329browse

Due to the true platform independence of XML (Extensible Markup Language: eXtensible Markup Language), it is gradually becoming the main medium for data transmission. XML is a self-describing language, and the data itself already contains metadata, that is, information about the data itself. For example: "Mencius Chapter E 1757281793923net_lover1807581793923" is a set of data. It is difficult to see what it means literally, and it is not clear how many data segments it consists of. However, if we use XML to describe it as follows, we can clearly see to the meaning of each data segment:

<PersonData>
  <Person>
   <姓名>孟子E章</姓名>
   <身高>175</身高>
   <体重>72</体重>
   <电话>81793923</电话>
  </Person>  
  <Person>
   <姓名>net_lover</姓名>
   <身高>180</身高>
   <体重>75</体重>
   <电话>81793923</电话>
  </Person>
 </PersonData>

From the above piece of XML, we can not only clearly see what each data segment means, but also know the meaning of the data. Split location. In our usual applications, the results we get may be in the form of arrays, collections or recordsets. How do we convert them into self-describing XML format data? From the data form point of view, XML is a simple text format of pure strings. Strings are very simple, fast and easy to transfer. Arrays are sometimes very slow to transfer by reference and are very troublesome to process. , and collections and record sets are both objects, which will cause a decrease in computer performance during processing, and these objects are associated with a specific platform, which requires the platform to have a built-in processing mechanism to handle object operations. XML is already a W3C standard and is platform-independent. The only requirement for our computers is to be able to process simple XML strings, that is, an XML parser. It can parse XML strings and can easily decompose data through an interface. into independent data segments so that we can access them. XML parsers are small, performant and can be found on every platform. Once we receive the XML data and parse it into the style of the above example, we can convert them into different representations through XSLT (eXstensible Stylesheet Language Transformations). Using XML data format for data transmission will make our work of writing application code simpler and easier, and has good scalability.
Next, let’s take a look at how to convert our data. Our example is written under Microsoft Windows 2000, IIS5, MSXML3 and ADO2.6. The sample data uses the Northwind sample database that comes with Microsoft SQL Server7.0. The reason why we use SQL Server7 instead of SQL Server2000 that supports XML is to consider the principle of universality. Our purpose is to process the record sets obtained from different types of data sources, not just to support XML output like SQL Server2000. data source. ADO is used because it has various forms and can handle different types of data sources; XML is used because it can be transmitted and parsed quickly. But the processing method in this example is also suitable for any environment with Microsoft XML parser, ADO2.5 or above version of Windows, IIS, SQL Server.
For the sake of simplicity, we only select products with unit price less than or equal to 20 US dollars, inventory greater than or equal to 20, and product names less than or equal to 6 characters:


<%
  Dim objRecordset
  Set objRecordset = Server.CreateObject("ADODB.Recordset")
  objRecordset.open _
      "SELECT PRoductName, UnitPrice, UnitsInStock " _
          & "FROM Products " _
          & "WHERE UnitPrice <= 20 " _
          & "AND UnitsInStock >= 20 " _
          & "AND LEN(ProductName) <= 6 " _
          & "ORDER BY ProductName", _
      "Provider=SQLOLEDB;" _
          & "Data Source=SomeSQLServer;" _
          & "Initial Catalog=Northwind;" _
          & "User ID=MyUserName;" _
          & "PassWord=MyPassword;"
  %>

Now, we Just use 3 ways to convert the record set we got into XML format.
First, we can traverse the entire record set, use XML DOM (Document Object Model), and build an XML node tree:

<%
  Dim objXMLDOM, objRootNode, objNode
  Set objXMLDOM = Server.CreateObject("MSXML2.DOMDocument")
  
  Set objRootNode = objXMLDOM.createElement("xml")
  objXMLDOM.documentElement = objRootNode
  
  Do While NOT objRecordset.EOF
      Set objRowNode = objXMLDOM.createElement("row")  
      Set objNode = objXMLDOM.createElement("ProductName")
      objNode.text = objRecordset.Fields.Item("ProductName").Value
      objRowNode.appendChild(objNode)
  
      Set objNode = objXMLDOM.createElement("UnitPrice")
      objNode.text = objRecordset.Fields.Item("UnitPrice").Value
      objRowNode.appendChild(objNode)
  
      Set objNode = objXMLDOM.createElement("UnitsInStock")
      objNode.text = objRecordset.Fields.Item("UnitsInStock").Value
      objRowNode.appendChild(objNode)
  
      objRootNode.appendChild(objRowNode)
  
      objRecordset.MoveNext
  Loop
  
  Set objNode = Nothing
  Set objRowNode = Nothing
  Set objRootNode = Nothing
  
  Set objRecordset = Nothing
  %>

Now, we get an XML DOM object. The performance of this method is not ideal when the recordset is large, because the ADO recordset object and the XML DOM object must be stored in the system memory at the same time.
The second method is to traverse the record set and directly generate the XML string itself:

 <%
  Dim strXML
  strXML = "<xml>"
  objRecordset.MoveFirst
  Do While NOT objRecordset.EOF
      strXML = strXML & "<row>"
      strXML = strXML & "<ProductName>" _
          & objRecordset.Fields.Item("ProductName").Value _
          & "</ProductName>"
      strXML = strXML & "<UnitPrice>" _
          & objRecordset.Fields.Item("UnitPrice").Value _
          & "</UnitPrice>"
      strXML = strXML & "<UnitsInStock>" _
          & objRecordset.Fields.Item("UnitsInStock").Value _
          & "</UnitsInStock>"
      strXML = strXML & "</row>"
      objRecordset.MoveNext
  Loop
  strXML = strXML & "</xml>"
  Set objRecordset = Nothing
  %>


However, the biggest flaw of the above two methods is that they cannot reuse the code. We put the node The names are all written down. If we perform queries on different fields, we must also manually change our code to meet the needs of different nodes. Our approach below will become more general.
The third method: reusable method.

  <%
  Dim strXML
  strXML = "<xml>"
  objRecordset.MoveFirst
  Do While NOT objRecordset.EOF
      strXML = strXML & "<row>"
      For Each varItem In objRecordset.Fields
          strXML = strXML _
              & "<" & varItem.name & ">" _
              & varItem.value _
              & "</" & varItem.name & ">"
      Next
      strXML = strXML & "</row>"
      objRecordset.MoveNext
  Loop
  strXML = strXML & "</xml>"
  Set objRecordset = Nothing
  %>


A more effective method is to directly use the built-in save method of the record set, which can automatically convert the contents of the record set into XML format. We call After the save method, we can immediately release the recordset object instance in memory. The save method has two parameters: one is the place where the XML is to be saved, and the other is an indicator indicating the format in which the data is saved. We can save the data as an XML DOM object (ADO STREAM object), or directly save it as an asp RESPONSE object. For the sake of generality, we save it as XML DOM, and use the adPersistXML ADO constant for the second parameter. The method is as follows:

  <%
  Const adPersistXML = 1
  Dim objXMLDOM
  Set objXMLDOM = Server.CreateObject("MSXML2.DOMDocument.3.0")
  objRecordset.save objXMLDOM, adPersistXML
  Set objRecordset = Nothing
  %>


This method is convenient and quick, and not error-prone. There is no need to manually change the node name for different queries. However, the XML produced by this method is not concise enough. Take a look at the results it produces:

<xml    
xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"    
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"    
xmlns:rs="urn:schemas-microsoft-com:rowset"    
xmlns:z="#RowsetSchema">
<s:Schema id="RowsetSchema">
    <s:ElementType        
    name="row"        
    content="eltOnly"        
    rs:CommandTimeout="30">
        <s:AttributeType            
        name="ProductName"           
         rs:number="1"            
         rs:writeunknown="true">
            <s:datatype                
            dt:type="string"                
            dt:maxLength="40"                
            rs:maybenull="false"/>        
            </s:AttributeType>
        <s:AttributeType            
        name="UnitPrice"            
        rs:number="2"            
        rs:nullable="true"            
        rs:writeunknown="true">
            <s:datatype                
            dt:type="number"                
            rs:dbtype="currency"                
            dt:maxLength="8"                
            rs:precision="19"                
            rs:fixedlength="true"/>        
            </s:AttributeType>
        <s:AttributeType            
        name="UnitsInStock"            
        rs:number="3"            
        rs:nullable="true"            
        rs:writeunknown="true">
            <s:datatype                
            dt:type="i2"                
            dt:maxLength="2"                
            rs:precision="5"                
            rs:fixedlength="true"/>        
            </s:AttributeType>
        <s:extends type="rs:rowbase"/>
    </s:ElementType>
</s:Schema>
<rs:data>
    <z:row        ProductName="Chai"        UnitPrice="18"        UnitsInStock="39"/>
    <z:row        ProductName="Konbu"        UnitPrice="6"        UnitsInStock="24"/>
    <z:row        ProductName="Tofu"        UnitPrice="23.25"        UnitsInStock="35"/>
</rs:data>
</xml>

 ADO 自动产生的XML包含了schema信息,它描述这个XML里允许有什么节点和属性以及采用何种数据类型,而且数据节点也增加了名称空间。schema信 息在需要数据验证的地方或进行更复杂的处理或许很有用,但是,大多数情况下,我们使用的是瘦客户机,我们不需要schema信息。我们可以利用XSLT来 分离出我们想要的信息,去掉多余的信息。因此,我们编写下面的“ DataCleaner.xsl”:

 <?xml version="1.0"?>
  <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
      xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
      xmlns:rs="urn:schemas-microsoft-com:rowset"
      xmlns:z="#RowsetSchema">
  
  <xsl:output omit-xml-declaration="yes"/>
  <xsl:template match="/">
      <xsl:element name="xml">
          <xsl:for-each select="/xml/rs:data/z:row">
              <xsl:element name="row">
                  <xsl:for-each select="@*">
                      <xsl:element name="{name()}">
                          <xsl:value-of select="."/>
                      </xsl:element>
                  </xsl:for-each>
              </xsl:element>
          </xsl:for-each>
      </xsl:element>
  </xsl:template>
  
  </xsl:stylesheet>

  
 这个XSLT具有可重用的特性,对于不同的查询结果都适用,下面就是如何使用这个XSLT的例子:
 

  <%
  Dim strCleanXML, objXMLDOM_XSLT
  
  Set objXMLDOM_XSLT = CreateObject("MSXML2.DOMDocument")
  objXMLDOM_XSLT.load(Server.MapPath("DataCleaner.xsl"))
  strCleanXML = objXMLDOM.transformNode(objXMLDOM_XSLT)
  
  Set objXMLDOM = Nothing
  Set objXMLDOM_XSLT = Nothing
  %>

  
 经过上面的处理以后,strClaenXML就是我们所想要的XML字符串了。
  
 

 <xml>
      <row>
          <ProductName>Chai</ProductName>
          <UnitPrice>18</UnitPrice>
          <UnitsInStock>39</UnitsInStock>
      </row>
      <row>
          <ProductName>Konbu</ProductName>
          <UnitPrice>6</UnitPrice>
          <UnitsInStock>24</UnitsInStock>
      </row>
  </xml>

  
 上面这种格式的XML字符串是我们经常见到的节点集的样式,如果您不想把字段处理成节点,而把它处理成属性节点,那么我们只需对DataCleaber.xsl稍加改动即可:

<?xml version="1.0"?>
  <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
      xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
      xmlns:rs="urn:schemas-microsoft-com:rowset"
      xmlns:z="#RowsetSchema">
  
  <xsl:output omit-xml-declaration="yes"/>
  
  <xsl:template match="/">
      <xsl:element name="xml">
          <xsl:for-each select="/xml/rs:data/z:row">
              <xsl:element name="row">
                  <xsl:for-each select="@*">
                      <xsl:attribute name="{name()}">
                          <xsl:value-of select="."/>
                      </xsl:attribute>
                  </xsl:for-each>
              </xsl:element>
          </xsl:for-each>
      </xsl:element>
  </xsl:template>
  
  </xsl:stylesheet>


 以下是采用了新样式的结果,它比用节点表示字段的长度要短的多了。传输起来速度会更快:
 
 

<xml>
   <row ProductName="Chai" UnitPrice="18" UnitsInStock="39"/>
   <row ProductName="Konbu" UnitPrice="6" UnitsInStock="24"/>
  </xml>

  
  到此为止,我们介绍了从ADO 记录集得到XML格式数据的几种办法,也得到了最简化的字符串。但是有几个问题你仍然需要注意,有些字段值还有XML里不支持的字符,比如:"'6d267e5fab17ea8bc578f9e7e5e1570b&,象P&G宝洁公司的名称,Chef Anton's Gumbo Mix产品名字等,在做转换时要进行编码处理。在Microsoft ADO 2.6的SDK里有使用save方法时要注意的问题:1,save方法只对open Recordset起作用;2,不支持带有adVariant,adIDispatch,adIUnknown类型的字段的记录集的savw;3,当保存 分级的记录集( data shapes)有两个限制:不能保存参数化和含有未解决的更新的记录集。
 为了更进一步提高性能,你可以把转换工作放 到COM/COM+组件中, ASP代码只进行数据的最终表现即可。把业务层、数据层和表现层分开,ASP只需要调用数据组件,数据组件调用数据库的存储过程,把结果转换成XML,最 后只把简单的XML字符环串回到ASP程序里,ASP就可以用XSLT把XML进行转换,把结果送到浏览器。


 以上就是利用XSLT把ADO记录集转换成XML的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn