Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT Output Question

Status
Not open for further replies.

jba6511

Programmer
Jun 15, 2007
23
US
I have an xml document like the one below:

<room name='Green'>
<equipmentList>
</equipmentList>
</room>
<room name='White'>
<equipmentList>
<equipment>Projector</equipment>
<equipment>PC</equipment>
<equipment>Mac</equipment>
</equipmentList>
</room>
<room name='Blue'>
<equipmentList>
<equipment>Projector</equipment>
<equipment>PC</equipment>
</equipmentList>
</room>

I am trying to output all of the equipment in each room. If I use the following:
<xsl:template match="rooms">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="room">
<p>
<b><u><xsl:value-of select="@name"/></u></b>
<xsl:text> </xsl:text>
<xsl:for-each select="equipmentList">
<xsl:value-of select="equipment"/>
</xsl:for-each>
</p>
</xsl:template>
</xsl:stylesheet>

I can get the list of rooms just fine but it will only list projector for each room when there is more equipment. When I just use value-of select equipmentList, I get a list of all the equipment in each room, but it is run together like this
ProjectorPCMac

How can I get it to list each piece of equipment so that is is not all run together?
 
How about another <xsl:for-each select="equipment"> to iterate through the node set of <equipment> elements?

Tom Morrison
 
so something like:

<xsl:for-each select="equipmentList">
<xsl:for-each select="equipment">
<xsl:value-of select="equipment"/>
</xsl:for-each>
</xsl:for-each>

Is that correct?
 
Almost correct.
Code:
<xsl:for-each select="equipmentList">
        <xsl:for-each select="equipment">     
                <xsl:value-of select="[b][COLOR=white red].[/color][/b]"/>
                [b][COLOR=white blue]<xsl:text> </xsl:text>[/color][/b]
        </xsl:for-each>
</xsl:for-each>
The [COLOR=white red]red[/color] correction is necessary because the inner for-each changes the context to the <equipment> node. The [COLOR=white blue]blue[/color] is merely a suggestion, indicating that you probably want to do something in the output between the values of the <equipment> elements, or it will still be run together (as in your original post).

Tom Morrison
 
thanks. That worked like a charm. I will be trying to work some more with xpath later so I am sure I will have some more questions for you guys. thanks for all of the help so far.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top