
Using Transformations to Restructure XML Data
The .xml file that you have used in this walkthrough is structured so that the properties of each element are expressed as attributes. In many cases, .xml files that you work with are structured differently. For example, values in an .xml file are often created as elements with inner text.
If you have an .xml file in which property values are expressed in a format other than attributes, you can create a transformation file (.xslt) that can dynamically reformat the .xml file so that it is compatible with the XmlDataSource control.
In this part of the walkthrough, you will work with an .xml file that contains the same data as the Bookstore.xml file that you used previously. However, the data will be structured differently than it was in the Bookstore.xml file, so you will use a transformation to dynamically reformat it.
To begin this section, you will create a second .xml file.
To create the second .xml file
In Solution Explorer, right-click the App_Data folder, and then click Add New Item.
Under Visual Studio installed templates, click XML file.
In the Name box, type Bookstore2.xml.
Click Add.
A new .xml file is created containing only the XML directive.
Copy the following XML data, and then paste it into the file, overwriting what is already in the file.
<?xml version="1.0" standalone="yes"?>
<bookstore>
<book ISBN="10-000000-001">
<title>The Iliad and The Odyssey</title>
<price>12.95</price>
<comments>
<userComment rating="4">
Best translation I've read.
</userComment>
<userComment rating="2">
I like other versions better.
</userComment>
</comments>
</book>
<book ISBN="10-000000-999">
<title>Anthology of World Literature</title>
<price>24.95</price>
<comments>
<userComment rating="3">
Needs more modern literature.
</userComment>
<userComment rating="4">
Excellent overview of world literature.
</userComment>
</comments>
</book>
<book ISBN="11-000000-002">
<title>Computer Dictionary</title>
<price>24.95</price>
<comments>
<userComment rating="3">
A valuable resource.
</userComment>
</comments>
</book>
<book ISBN="11-000000-003">
<title>Cooking on a Budget</title>
<price>23.95</price>
<comments>
<userComment rating="4">Delicious!</userComment>
</comments>
</book>
<book ISBN="11-000000-004">
<title>Great Works of Art</title>
<price>29.95</price>
</book>
</bookstore>
Save the Bookstore2.xml file, and then close it.
You now need a transformation file that will convert the data in the Bookstore2.xml file into the attribute-based format that is used by the XmlDataSource control.
To create the transformation file
In Solution Explorer, right-click the App_Data folder, and then click Add New Item.
Under Visual Studio installed templates, click Text File.
There is no template for a transform file, so you can create it as a text file with the correct extension.
In the Name box, type Bookstore2.xsl.
Note: |
|---|
Be sure to use the .xsl extension.
|
Click Add.
A new blank file is created.
Copy the following transformation code, and then paste it into the file.
<?xml version="1.0"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
>
<xsl:strip-space elements="*"/>
<xsl:output method="xml"
omit-xml-declaration="yes"
indent="yes"
standalone="yes" />
<xsl:template match="/">
<xsl:for-each select="bookstore">
<xsl:element name="bookstore">
<xsl:for-each select="book">
<xsl:element name="book">
<xsl:attribute name="ISBN">
<xsl:value-of select="@ISBN"/>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="title"/>
</xsl:attribute>
<xsl:attribute name="price">
<xsl:value-of select="price"/>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Save the Bookstore2.xsl file, and then close it.
From this point, working with the XML data is very similar to what you did earlier in this walkthrough, except that you can specify the transformation file when you configure the XmlDataSource control. For the last part of this walkthrough, you will create a new page, and then repeat some of the steps from the first part of this walkthrough. However, this time you will show the data from the Bookstore2.xml file.
To configure data access to the .xml file
In Solution Explorer, right-click the name of your Web site, and then click Add New Item.
Under Visual Studio installed templates, click Web Form.
In the Name box, type Bookstore2.aspx.
Click Add.
Switch to Design view.
In the Toolbox, from the Data group, drag an XmlDataSource control onto the page.
On the XmlDataSource Tasks menu, click Configure Data Source.
The Configure Data Source dialog boxappears.
In the Data file box, type ~/App_Data/Bookstore2.xml.
In the Transform file box, type ~/App_Data/Bookstore2.xsl.
Click OK.
In the Toolbox, from the Data group, drag a GridView control onto the page.
On the GridView Tasks menu, in the Choose Data Source list, click XmlDataSource1.
Press CTRL+F5 to run the page.
The page displays the XML data in a grid. The data will appear in the grid the same as it did in the first page, even though the format of the underlying .xml file is different this time.