Saving a recordset to an XML file with ADO 2.5 produces an attribute-based XML file.

XML files can be element-based or attribute-based. The XML files produced by ADO 2.5 or higher are all attribute-based. XML files generated by ADO are self-describing objects that contain data and metadata (information about the data). If you take a look at the XML file generated by the example procedure (see Figure 17-26), you will notice that below the XML document's root tag there are two children nodes: <s:Schema> and <rs:data>. The schema node describes the structure of the recordset, while the data node holds the actual data. Inside the <s:Schema id="RowsetSchema"> and </s:Schema> tags, ADO places information about each column: field name, position, data type and length, nullability, and whether the column is writable. Each field is represented by the <s:AttributeType> element. Notice that the value of the name attribute is the field name. The <s:Attri-buteType> element also has a child element <s:datatype>, which holds information about its data type (integer, number, string, etc.) and the maximum field length. Below the schema definition, you can find the actual data. The ADO schema represents each record using the <z:row > tag. The fields in a record are expressed as attributes of the <z:row> element. Every XML attribute is assigned a value that is enclosed in a pair of single or double quotation marks; however, if the value of a field in a record is NULL, the attribute on the z:row is not created. Notice that each record is written out in the following format:

<z:row ProductID='1' ProductName='Chai' SupplierID='1' CategoryID='1' QuantityPerUnit='10 boxes x 20 bags' UnitPrice='18' UnitsInStock='39' Units0n0rder='0' ReorderLevel='10' Discontinued='False'/>

The above code fragment is an attribute-based XML document. However, you may want to have each record written out as follows:

<Product> <ProductID>1</ProductID> <ProductName>Chai</ProductName> <SupplierID>1</SupplierID> <CategoryID>1</CategoryID>

<QuantityPerUnit>10 boxes x 20 bags</QuantityPerUnit>






The above code fragment represents an element-based XML. Each record is wrapped in a <Product> tag, and each field is an element under the <Product> tag.

Changing the Type of an XML File

Because it is much easier to work with element-based XML files, you can write an XSL stylesheet to transform your attribute-based XML file to an element-based file. Figure 17-27 shows an example of such a stylesheet.

The following exercise demonstrates how to write a stylesheet to convert an XML document from attribute-based to element-based.

1. Open Notepad and type the code shown in Figure 17-27.

2. Save the stylesheet file as AttribToElem.xsl. We will use this stylesheet for the transformation in the next example procedure.

£ AtlfibToEleu. k$I - Notepad f i

FJe Ecft Format Hefc.

H«s 1: stylesheet versi o n="1.0" xm Ins:xsl="htlp://www.w3. a rg/19 9 9JXSUTransfa rrn" xrn ins: rs=" u rn; s c h e mas- ml c rosofl- com rowser> <KSl:ouipui method=')<mr' encoding="UTF-S" />


*xsl template match*"/"*

<b- root el e ment fa r ttie XM L output - > < Products xmlns^"*RowsetSchemcf>

<xsl io r- e a c h select=u/yml/rs:dataite:row/"> *Product> <Ksl:fbr-each seleet="@*"> *xsl :ete ment name="{ n a meQ}* *

<xsl:value-of setect=" 7> <tosl:element> <ixsl:for-each> ^/Product* «/ksl:fbr*each» «/Products*

<&si:tempiate> <J*>isl:sty1esheet=


This XSL stylesheet will convert an attribute-based XML document prepared by ADO to an element-based XML document.

Notice that in the above stylesheet example, the @* wild card matches all attribute nodes. Each time the <z:row> tag is encountered, an element named <Product> will be created. And for each attribute, the attribute name will be converted to the element name using the built-in XPath Name( ) function. Expressions in curly braces are evaluated and converted to strings. The select="." returns the current value of the attribute being read.

0 0

Post a comment