search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Home Backend Development XML/RSS Tutorial Convert ADO recordset to XML using XSLT

Convert ADO recordset to XML using XSLT

Feb 27, 2017 pm 04:33 PM

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里不支持的字符,比如:"'< >&,象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中文网(m.sbmmt.com)!


Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

JSON vs. XML: Why RSS Chose XML JSON vs. XML: Why RSS Chose XML May 05, 2025 am 12:01 AM

RSS chose XML instead of JSON because: 1) XML's structure and verification capabilities are better than JSON, which is suitable for the needs of RSS complex data structures; 2) XML was supported extensively at that time; 3) Early versions of RSS were based on XML and have become a standard.

Understanding RSS Documents: A Comprehensive Guide Understanding RSS Documents: A Comprehensive Guide May 09, 2025 am 12:15 AM

RSS documents are a simple subscription mechanism to publish content updates through XML files. 1. The RSS document structure consists of and elements and contains multiple elements. 2. Use RSS readers to subscribe to the channel and extract information by parsing XML. 3. Advanced usage includes filtering and sorting using the feedparser library. 4. Common errors include XML parsing and encoding issues. XML format and encoding need to be verified during debugging. 5. Performance optimization suggestions include cache RSS documents and asynchronous parsing.

Building XML Applications with C  : Practical Examples Building XML Applications with C : Practical Examples May 03, 2025 am 12:16 AM

You can use the TinyXML, Pugixml, or libxml2 libraries to process XML data in C. 1) Parse XML files: Use DOM or SAX methods, DOM is suitable for small files, and SAX is suitable for large files. 2) Generate XML file: convert the data structure into XML format and write to the file. Through these steps, XML data can be effectively managed and manipulated.

RSS, XML and the Modern Web: A Content Syndication Deep Dive RSS, XML and the Modern Web: A Content Syndication Deep Dive May 08, 2025 am 12:14 AM

RSS and XML are still important in the modern web. 1.RSS is used to publish and distribute content, and users can subscribe and get updates through the RSS reader. 2. XML is a markup language and supports data storage and exchange, and RSS files are based on XML.

XML in C  : Handling Complex Data Structures XML in C : Handling Complex Data Structures May 02, 2025 am 12:04 AM

Working with XML data structures in C can use the TinyXML or pugixml library. 1) Use the pugixml library to parse and generate XML files. 2) Handle complex nested XML elements, such as book information. 3) Optimize XML processing code, and it is recommended to use efficient libraries and streaming parsing. Through these steps, XML data can be processed efficiently.

Beyond Basics: Advanced RSS Features Enabled by XML Beyond Basics: Advanced RSS Features Enabled by XML May 07, 2025 am 12:12 AM

RSS enables multimedia content embedding, conditional subscription, and performance and security optimization. 1) Embed multimedia content such as audio and video through tags. 2) Use XML namespace to implement conditional subscriptions, allowing subscribers to filter content based on specific conditions. 3) Optimize the performance and security of RSSFeed through CDATA section and XMLSchema to ensure stability and compliance with standards.

Inside the RSS Document: Essential XML Tags and Attributes Inside the RSS Document: Essential XML Tags and Attributes May 03, 2025 am 12:12 AM

The core structure of RSS documents includes XML tags and attributes. The specific parsing and generation steps are as follows: 1. Read XML files, process and tags. 2. Extract,,, etc. tag information. 3. Handle custom tags and attributes to ensure version compatibility. 4. Use cache and asynchronous processing to optimize performance to ensure code readability.

Decoding RSS: An XML Primer for Web Developers Decoding RSS: An XML Primer for Web Developers May 06, 2025 am 12:05 AM

RSS is an XML-based format used to publish frequently updated data. As a web developer, understanding RSS can improve content aggregation and automation update capabilities. By learning RSS structure, parsing and generation methods, you will be able to handle RSSfeeds confidently and optimize your web development skills.

Related articles