WebReference.com - Chapter 7 of XML for ASP.NET Developers, from Sams Publishing (7/13) | WebReference

WebReference.com - Chapter 7 of XML for ASP.NET Developers, from Sams Publishing (7/13)

To page 1To page 2To page 3To page 4To page 5To page 6current pageTo page 8To page 9To page 10To page 11To page 12To page 13
[previous] [next]

XML for ASP.NET Developers

Transforming XML into Another Form of XML Using XSLT Elements

HTML isn't the only structure that can be created by an XSLT document. Many cases may occur in which other formats, such as a comma-delimited, WML, or even another form of XML, need to be generated to integrate with an application. In this section we'll take a look at a simple example of transforming from XML to XML and show how a few of the XSLT elements can make this process easier. XML-to-XML transformations are useful when an XML document's structure needs to be changed before sending it off to a vendor or to another application that expects a different format.

Listing 7.5 shows the XML document that needs to be transformed, Listing 7.6 shows the result of the transformation, and Listing 7.7 shows the XSLT document used in processing the transformation.

Listing 7.5 The Source XML Document

1: <?xml version="1.0"?>
2: <root>
3:   <row>
4:     <id>1</id>
5:     <name>
6:       <fname>Dan</fname>
7:       <lname>Wahlin</lname>
8:     </name>
9:     <address type="home">
10:       <street>1234 Anywhere St.</street>
11:       <city>AnyTown</city>
12:       <zip>85789</zip>
13:     </address>
14:     <address type="business">
15:       <street>1234 LottaWork Ave.</street>
16:       <city>AnyTown</city>
17:       <zip>85786</zip>
18:     </address>
19:   </row>
20:   <row>
21:     <id>2</id>
22:     <name>
23:       <fname>Elaine</fname>
24:       <lname>Wahlin</lname>
25:     </name>
26:     <address type="home">
27:       <street>1234 Anywhere St.</street>
28:       <city>AnyTown</city>
29:       <zip>85789</zip>
30:     </address>
31:     <address type="business">
32:       <street>1233 Books Way</street>
33:       <city>AnyTown</city>
34:       <zip>85784</zip>
35:     </address>
36:   </row>
37: </root>

Listing 7.6 The Result of Transforming the XML Document in Listing 7.5

1: <?xml version="1.0"?>
2: <root>
3:   <row id="1" fname="Dan" lname="Wahlin">
4:     <address type="home">
5:       <street>1234 Anywhere St.</street>
6:       <city>AnyTown</city>
7:       <zip>85789</zip>
8:     </address>
9:     <address type="business">
10:       <street>1234 LottaWork Ave.</street>
11:       <city>AnyTown</city>
12:       <zip>85786</zip>
14:     </address>
15:   </row>
16:   <row id="2" fname="Elaine" lname="Wahlin">
17:     <address type="home">
18:       <street>1234 Anywhere St.</street>
19:       <city>AnyTown</city>
20:       <zip>85789</zip>
21:     </address>
22:     <address type="business">
23:       <street>1233 Books Way</street>
24:       <city>AnyTown</city>
25:       <zip>85784</zip>
26:     </address>
27:   </row>
28: </root>

Listing 7.7 The XSLT Document


 1: <?xml version="1.0" ?> 
2: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 3: version="1.0">
4: <xsl:output method="xml" indent="yes" encoding="utf-8"
5: omit-xml-declaration="no"/>
6: <xsl:template match="/">
7: <root>
8: <xsl:apply-templates/>
9: </root> 10: </xsl:template>
11: <xsl:template match="row">
12: <row> 13: <xsl:attribute name="id">
14: <xsl:value-of select="id"/> 15: </xsl:attribute>
16: <xsl:attribute name="fname">
17: <xsl:value-of select="name/fname"/>
18: </xsl:attribute> 19: <xsl:attribute name="lname">
20: <xsl:value-of select="name/lname"/> 21: </xsl:attribute>
22: <xsl:for-each select="address"> 23: <xsl:copy-of select="."/>
24: </xsl:for-each>
25: </row> 26: </xsl:template>
27: </xsl:stylesheet>

Breaking the XSLT document down into individual pieces reveals a few new things not seen in previous examples. First, Line 4 uses the xsl:output element to specify an output format of xml. It also specifies that the XML declaration should be included. This is done by setting the omit-xml-declaration attribute to no. Because this is the attribute's default value, it could have been left out altogether.

Lines 6–10 take care of setting the starting template (the one that matches the document node) needed in the XSLT document. This template simply adds a node named root to the result tree and then triggers the process of looking for other templates that match up with nodes in the source XML document.

The template matching the row element node writes a row element to the result tree. The bulk of the transformation process occurs in lines 13–24. To start, three different attributes are added to the row element by using the xsl:attribute element. The value of these attributes is obtained by using the xsl:value-of element to access the appropriate elements in the source XML document.

After the attributes are added, the xsl:for-each element is used to loop through all address elements. This element simply takes the name of the node-set to loop through as the value of the select attribute. Because the address elements (and their children) remain unchanged from the source to the result tree, the xsl:copy-of element is used to simply copy over the address element (and all its children) to the result tree. Had we wanted only to copy the address element itself and not the children, we could have used the xsl:copy element instead. However, utilizing the xsl:copy-of element prevents us from having to create each element (address, street, city, zip) dynamically by using the xsl:element or xsl:copy elements.

Now that you've had an opportunity to see some of the most common XSLT elements in action, let's take a look at a few more that can help make your XSLT documents more dynamic and flexible.


To page 1To page 2To page 3To page 4To page 5To page 6current pageTo page 8To page 9To page 10To page 11To page 12To page 13
[previous] [next]

© Copyright Pearson Education and Created: April 22, 2002
Revised: April 22, 2002

URL: http://webreference.com/authoring/languages/xml/aspnet/chap7/7.html