Use XSLT to convert the ADO record set to XML

xiaoxiao2021-04-09  327

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 The meaning representative of each data segment:

Menciic E Chapter 175 72 81793923 NET_LOVER 180 75 81793923

From the above XML, we can not only see what the representatives of each data are meaningful, but also you can know the split position of the data. 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 20 mines, the stock is greater than or equal to 20, the product name is less than or equal to 6 characters:

<% Dim objRecordsetSet 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 use three ways to convert our record set into XML format. First, we can traverse the entire recordset, using XML DOM (Document Object Model), build XML node tree: <% Dim objxmldom, objrootnode, objNodeEt ObjXmldom = Server.createObject ('msxml2.domdocument')

Set objRootnode = objxmldom.createElement ('xml') objxmldom.documentelement = Objrootnode

Do While NOT objRecordset.EOFSet objRowNode = objXMLDOM.createElement ( 'row') Set objNode = objXMLDOM.createElement ( 'ProductName') objNode.text = objRecordset.Fields.Item ( 'ProductName'). ValueobjRowNode.appendChild (objNode)

Set objNode = objxmldom.createElement ('unitprice') objNode.text = ObjRecordset.fields.Item ('unitprice'). Valueobjrownode.AppendChild (ObjNode)

Set objNode = objxmldom.createElement ('unitsinstock') objNode.text = ObjRecordset.fields.Item ('unitsSinstock'). Valueobjrownode.AppendChild (ObjNode)

Objrootnode.AppendChild (Objrownode)

ObjRecordset.MovenextLoop

Set objNode = NothingSet Objrownode = NothingSet Objrootnode = 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 way to traverse the record set, the string itself directly generate XML: <% Dim strXMLstrXML = '' objRecordset.MoveFirstDo While NOT objRecordset.EOFstrXML = 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.MoveNextLoopstrXML = strXML & ' 'Set objRecordset = Nothing%>

However, the above two methods are not able to reuse the code, we write the names of the nodes, if we do different fields, we must also change our code to meet the needs of different nodes. The following methods will become more common. Third method: a reusable method.

<% Dim strXMLstrXML = '' objRecordset.MoveFirstDo While NOT objRecordset.EOFstrXML = strXML & '' For Each varItem In objRecordset.FieldsstrXML = strXML _ & '<' & varItem.name & '>' _ & varItem. Value _ & '' nextstrxml = strXml & '' ObjRecordSet.MovenextLoopstrxml = strXml & '' set objRecordset = Nothing%>

A more effective way, we can directly use the Save method built into the record set, which automatically converts the contents of the record set into XML format, after calling the Save method, we can immediately release the record set object instance in memory. . 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. The method is as follows: <% const adpersistXML = 1dim objxmldomset Objxmldom = server.createObject ('msxml2.domdocument.3.0') ObjRecordSet.save objXmldom, AdpersistXmlset ObjRecordset = Nothing%>

This method is convenient and fast, and it is not easy to make mistakes. For different queries, you don't have to manually change the node name. However, the XML produced in this method is not simple enough to see the results it produced:

ADO automatically generated XML contains Schema information, which describes what nodes and properties that are allowed in this XML and what data types are used, and the data nodes also add name space. 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 for 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 = NothingSet Objxmldom_xslt = nothing%>

After the above treatment, STRCLAENXML is the XML string we want.

18 39 konbu 6 < / Unitprice> 24

The above format XML string is the style of the set of nodes we often see, if you don't want to process the fields into nodes, and handle it into a property node, then we only need to change the Datacleaber.xsl:

The following is the result of the new style, which is much shorter than the length of the field indicating the field. Transmission speed will be faster:

So far, we introduce several ways to get XML format data from the ADO record set, and also get the most simplified string. But there are several questions that you still need to note that some field values ​​also have characters that are not supported in XML, such as: '<> &, like P & g Mix, the name, Chef Anton's Gumbo Mix product name, when doing conversion Code processing. In the SDK in Microsoft Ado 2.6, there is a question when using the Save method: 1. Save method only works on the Open Recordset; 2. SAVW; 3, Two restrictions in saving the grade record set: You cannot save the parameterization and a recordset containing unresolved updates. To further improve performance, you can put the conversion work into the COM / COM component, and the ASP code only performs the final performance of the data. Separate the business layer, data layer, and performance layer, and the ASP only needs to call the data component. The data component calls the database stored procedure, convert the result into XML, and finally only the simple XML character ring back to the ASP program, ASP can Use XSLT to convert XML to send the result to the browser.

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

New Post(0)