Use XSLT to convert the ADO record set to XML

zhaozj2021-02-11  200

Use XSLT to convert the ADO record set to XML

Mengxian

Since XML (Scalable Markup Language: Extensible Markup Language) is unrelated, it is gradually become the main media of data transmission. XML is a self-description language that has already included metadata, which is information about the data itself. For example: "Menciic E-chapter 1757281793923NET_LOVER1807581793923" This group of data, it is difficult to see what it means from the literal, and it does not know how many data segments are composed. However, if you use XML to do the following description, we can clearly look clear To each data segment, the meaning of each data segment: Menciic E Chapter 175 72 81793923 NET_LOVER 180 75 81793923 From above In a paragraph XML, we can not only see what it means to see every data representative, but also know the data segmentation position. In our usual applications, the results we get may be an array, a collection, or a collection of records, how do we convert them into data from the XML format? From the data form, XML is a simple string text format. The string is very simple, fast and easy, and the array is sometimes very slow when passing the reference, and it is very troublesome. And the collection and recording sets are objects, which will result in a decline in computer performance during processing, and these objects are associated with a particular platform, which requires the platform to have built-in processing mechanisms to process objects. Operation. XML is already the standard of W3C. It is a platform-independent. The only requirement of our computer is to handle simple XML strings, the XML parser, which can parse the XML string, which can be exploded by an interface. It is a separate data segment so that we can access. The XML parser is very small, the performance is also very good, can be found on each platform. Once we receive XML data and analyze it to the above example, we can convert them into different expressions through XSLT (Exstensible Stylesheet Language Transformations). Data transfer by using XML data format will make our application code easier and have good scalability. Below, let's take a look at how to convert our data. Our example is written in Microsoft Windows 2000, IIS5, MSXML3, and ADO2.6, which uses the Northwind sample database that comes with Microsoft SQL Server 7.0. The reason why SQL Server7 does not use SQL Server2000 that supports XML, is a principle of versatility, our purpose is to handle different types of data sources, not just support XML output like SQL Server2000. Data source. Using ADO because it is diverse, you can handle different types of data sources; use XML because it can transmit and resolve.

However, the processing method of this example is also suitable in any environment with a MicrSoft XML parser, ADO2.5 or above, of Windows, IIS, SQL Server. For the sake of simplicity, we only choose a unit price of less than or equal to 20 mines. The inventory is greater than or equal to 20. The product name is less than or equal to 6 characters: <% DIM ObjRecordset set objRecordset = Server.createObject ("adodb.recordset") ObjRecordset.open _ " SELECT ProductName, Unitprice, UnitsSinstock "_" "_ &" fer "_ &" and unitsinstock> = 20 "_ &" and lentname) <= 6 "_ &" order by productname " , _ "Provider = SQLOLEDB;" _ & "data source = somesqlserver;" _ "initial catalog = northwind;" _ "user id = myusername;" _ & "password = mypassword;"%> now, we use Three ways to convert our record set into XML format. First, we can traverse the entire recordset, using XML DOM (Document Object Model), establish 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 objRecordset = Nothing%>

Now, we get an XML DOM object. This approach is not ideal for the record set very much, as the ADO record set object and the XML DOM object are saved in the system memory. The second approach, traverses the record set, directly generate XML string itself: <% DIM strXml strXml = "" ObjRecordset.movefirst Do WHILE NOT OBJRECORDSET.EOF strXml = strxml & "" strXml = strxml & " "_ & objRecordset.fields.item (" ProductName "). Value_ &" "strXml = strxml &" "_ & objRecordset.fields.Item (" Unitprice). Value _ & "" strXml = strXml & "" _ & objRecordset.fields.Item ("UnitsInstock"). Value_ & "" strXml = strxml & "" ObjRecordset. MoveNext loop strxml = strXml & "" SET OBJRECORDSET = NOTHING%> But the maximum drawback of the above two methods is not able to reuse the code, we write the name of the node, if we do different fields We must also manually change our code to meet the needs of different nodes. The following methods will become more common. Third method: a reusable method.

<% Dim strXML strXML = "" objRecordset.MoveFirst Do While NOT objRecordset.EOF strXML = strXML & "" For Each varItem In objRecordset.Fields strXML = strXML _ & "<" & varItem.name & " > "_ & Varitem.value _ &" Next strXml = strxml & "" set objRecordset = Nothing %> A more effective way, we can directly utilize the SAVE method in which the recordset is built, it can automatically convert the contents of the record set into XML format, we can immediately release the record set in memory immediately after calling the Save method. Object instance. There are two parameters: one is where XML wants to save, one is an indicator, indicating that the data is saved in the format. We can save data into XML DOM objects (ADO Stream object), or directly saved as an ASP RESPONSE object, and we save into XML DOM, and the second parameter uses AdpersistXML ADO constants. 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 less error prone, for Different queries, do not manually change the node name. However, the XML produced in this method is not simple enough to see the results it produce:

ADO automatically generated XML contains SCHEMA information, which describes what nodes and properties allowed in this XML and what data types are used, and the data nodes also add namespaces. SCHEMA information is very useful in places requiring data verification or more complex processing, but in most cases, we use thin clients, we don't need SCHEMA information. We can use XSLT to separate the information we want and remove excess information. Therefore, we write the following "Dataclener.xsl":

This XSLT has a reusable feature, which is applicable to different query results. How to use this XSLT example: <% DIM strcleanxml, objxmldom_xslt set objxmldom_xslt = creteObject ("msxml2.domdocument") objxmldom_xslt.load (Server.MAppath "Dataclener.xsl")) StrcleanXML = objxmldom.TransFormNode (objxmldom_xslt) set objxmldom_xslt = Nothing%> After the above processing, StrclaenXML is the XML string we want.

18 39 konbu 6 < / Unitprice> 24 The above-mentioned XML string is the style of our current node set, if you don't want to process the field to node, and put it Processing a property node, then we only need to change the DataCleaber.xsl:

转载请注明原文地址:https://www.9cbs.com/read-5430.html

New Post(0)