BizTalk Utilities CV ,   Jobs ,   Code library  
 
 
Page 6 of 15

 

Previous Page Table Of Contents Next Page

XSL Transformations, cont.

Handling Multiple Selections with xsl:for-each

The select attribute selects only the first node that matches its selection _criterion. What if you have multiple nodes that could match? For example, say that you can have multiple <NAME> elements for each planet:

<PLANET>

    <NAME>Mercury</NAME>

    <NAME>Closest planet to the sun</NAME>

    <MASS UNITS="(Earth = 1)">.0553</MASS>

    <DAY UNITS="days">58.65</DAY>

    <RADIUS UNITS="miles">1516</RADIUS>

    <DENSITY UNITS="(Earth = 1)">.983</DENSITY>

    <DISTANCE UNITS="million miles">43.4</DISTANCE><!--At perihelion-->

</PLANET>

The <xsl:value-of> element's select attribute by itself will select only the first <NAME> element; to loop over all possible matches, you can use the <xsl:for-each> element like this:

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 

    <xsl:template match="PLANETS">

        <HTML>

            <xsl:apply-templates/>

        </HTML>

    </xsl:template>

 

<xsl:template match="PLANET">

    <xsl:for-each select="NAME">

        <P>

            <xsl:value-of select="."/>

        </P>

    </xsl:for-each>

</xsl:template>

 

</xsl:stylesheet>

This style sheet will catch all <NAME> elements, place their values in a <P> element, and add them to the output document, like this:

<HTML>

 

    <P>Mercury</P>

    <P>Closest planet to the sun</P>

 

    <P>Venus</P>

 

    <P>Earth</P>

 

</HTML>

We've seen now that you can use the match and select attributes to indicate what nodes you want to work with. The actual syntax that you can use with these attributes is fairly complex but worth knowing. I'll take a look at the match attribute in more detail first, and I'll examine the select attribute later in this chapter.

 

Page 6 of 15

 

Previous Page Table Of Contents Next Page
 

Recent Jobs

Sr. Software Engineer - Analytics
Immediate Mainframe openings for Ch
Immediate TANDEM-TAL openings for C
Immediate ASP.NET/C# Openings for C
Sr. Software Engineer

View all Jobs (Add yours)
View all CV (Add yours)



spfxmasks
water softener
Teleconference
Host Department NOLIMIT Web Hosting
MSN
sunglasses
conference calling


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP