Use XSLT to convert the ADO record set to XML
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:
<
Persondata
>
<
Person
>
<
Name
>
Mencius E
Name
>
<
height
>
175
height
>
<
body weight
>
72
body weight
>
<
phone
>
81793923
phone
>
Person
>
<
Person
>
<
Name
>
NET_LOVER
Name
>
<
height
>
180
height
>
<
body weight
>
75
body weight
>
<
phone
>
81793923
phone
>
Person
>
Persondata
>
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 or equal to 20 mines, the stock 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, UnitsStock" _ & "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), 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 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 approach, traversal record set, directly generate the XML string itself:
<
% DIM
Strxml strXml
= "
ObjRecordset.movefirst Do While Not ObjRecordset.eof strXML
= STRXML
& " > "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%> 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 change our code to Meet the needs of different nodes. The following methods will become more common. Third ways: reusable methods. < % DIM Strxml strXml = " ObjRecordset.movefirst Do While Not ObjRecordset.eof strXML = STRXML & " > "For easy 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 efficient method, we can directly use the Save method in which the recordset is built, it can automatically convert the contents of the record set into XML format, we can release the Save method, we can release it immediately The record set object instance in memory. Save method has 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 the data into XML DOM objects (ADO Stream object) It can also be saved directly as an ASP RESPONSE object. For a versatile, we save it into XML DOM, the second parameter uses AdpersistXML ADO constants. 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 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: < 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 > > < 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 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": 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 > This XSLT has a reusable feature that applies to different query results, and how to use this xslt example: < % DIM StrcleanXML, Objxmldom_xslt set objxmldom_xslt = CreateObject ("msxml2.domdocument") Objxmldom_xslt.Load (Server.Mappath ("Dataclener.xsl")) StrcleanXML = Objxmldom.transformNode (objxmldom_xslt) Set Objxmldom = Nothing Set objxmldom_xslt = Nothing % > After the above treatment, STRCLAENXML is the XML string we want. < xml > < Row > < ProductName > Chai ProductName > < Unitprice > 18 Unitprice > < UnitsInstock > 39 UnitsInstock > Row > < Row > < ProductName > Konbu ProductName > < Unitprice > 6 Unitprice > < UnitsInstock > twenty four UnitsInstock > Row > xml > 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: 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 > The following is a result of a new style, which is much shorter than the length of the field indicating the field. Transmission speed will be faster: < xml > < Row ProductName = "Chai" Unitprice = "18" UnitsInstock = "39" /> < Row ProductName = "Konbu" Unitprice = "6" UnitsInstock = "24" /> xml > 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 pay attention, and some of the field values have characters that are not supported in XML, such as: "' <> &, Icon P & G Procter & Gamble's name, Chef Anton's Gumbo Mix product name, etc., to perform encoding processing when switching. 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. Transfer from: http://www.g22.net/Article/show.asp?id=640