need help with XSLT displaying multiple (duplicate) nodes

M

Mad Scientist Jr

I wrote an XSLT ("myquery1.xsl" below) that takes some XML data
("mydata.xml" below) and displays it in HTML table format.

"myquery1.xsl" returns:
C D E F G H I J K L
Red R 7 3 2 Hot Firetruck true true
Green G 50003 5 3 Warm Avocado true
Blue B 17 6 1 Cool

The problem is that the XML can contain multiple nodes for H and I and
the xsl is only displaying the first one for each node B.

So I modified the XSLT ("myquery2.xsl" below) using a nested template
so that it will return all nodes of H and I (separated by a + sign).
However, now it isn't returning anything for those nodes.

"myquery2.xsl" returns:
C D E F G H I J K L
Red R 7 3 2 true true
Green G 50003 5 3 true
Blue B 17 6 1

Can someone with more experience in XSLTs see what I am doing wrong
and explain how to get it to display the data correctly?

What I want it to return:
C D E F G H
I J K L
Red R 7 3 2 Hot+Warm Firetruck+Hydrant+Tomato+Apple
+Darth_Maul true true
Green G 50003 5 3 Warm+Cool Avocado+Lettuce
+Ralph_Nader true
Blue B 17 6 1
Cool

Any help would be most appreciated...

"mydata.xml"
<?xml version="1.0" encoding="UTF-8"?>
<A>
<B>
<C>Red</C>
<D>R</D>
<E>7</E>
<F>3</F>
<G>2</G>
<H>Hot</H>
<H>Warm</H>
<I>Firetruck</I>
<I>Hydrant</I>
<I>Tomato</I>
<I>Apple</I>
<I>Darth_Maul</I>
<J>true</J>
<K>true</K>
</B>
<B>
<C>Green</C>
<D>G</D>
<E>50003</E>
<F>5</F>
<G>3</G>
<H>Warm</H>
<H>Cool</H>
<I>Avocado</I>
<I>Lettuce</I>
<I>Ralph_Nader</I>
<L>true</L>
</B>
<B>
<C>Blue</C>
<D>B</D>
<E>17</E>
<F>6</F>
<G>1</G>
<H>Cool</H>
</B>
</A>



"myquery1.xsl"

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

<xsl:template match="/">
<table border="1">
<thead>
<th>C</th>
<th>D</th>
<th>E</th>
<th>F</th>
<th>G</th>
<th>H</th>
<th>I</th>
<th>J</th>
<th>K</th>
<th>L</th>
</thead>
<tbody>
<xsl:apply-templates select="A/B" />
</tbody>
</table>
</xsl:template>

<xsl:template match="A/B">
<tr>
<td>
<xsl:value-of select="C"/> 
</td>
<td>
<xsl:value-of select="D"/> 
</td>
<td>
<xsl:value-of select="E"/> 
</td>
<td>
<xsl:value-of select="F"/> 
</td>
<td>
<xsl:value-of select="G"/> 
</td>
<td>
<xsl:value-of select="H"/> 
</td>
<td>
<xsl:value-of select="I"/> 
</td>
<td>
<xsl:value-of select="J"/> 
</td>
<td>
<xsl:value-of select="K"/> 
</td>
<td>
<xsl:value-of select="L"/> 
</td>
</tr>
</xsl:template>

</xsl:stylesheet>




"myquery2.xsl"
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">
<xsl:eek:utput method="html"/>

<xsl:template match="/">
<table border="1">
<thead>
<th>C</th>
<th>D</th>
<th>E</th>
<th>F</th>
<th>G</th>
<th>H</th>
<th>I</th>
<th>J</th>
<th>K</th>
<th>L</th>
</thead>
<tbody>
<xsl:apply-templates select="A/B" />
</tbody>
</table>
</xsl:template>

<xsl:template match="A/B">
<tr>
<td>
<xsl:value-of select="C"/> 
</td>
<td>
<xsl:value-of select="D"/> 
</td>
<td>
<xsl:value-of select="E"/> 
</td>
<td>
<xsl:value-of select="F"/> 
</td>
<td>
<xsl:value-of select="G"/> 
</td>
<td>
<xsl:apply-templates select="A/B/H" /> 
</td>
<td>
<xsl:apply-templates select="A/B/I" /> 
</td>
<td>
<xsl:value-of select="J"/> 
</td>
<td>
<xsl:value-of select="K"/> 
</td>
<td>
<xsl:value-of select="L"/> 
</td>
</tr>
</xsl:template>

<xsl:template match="A/B/H">
<xsl:value-of select="H"/> + 
</xsl:template>

<xsl:template match="A/B/H">
<xsl:value-of select="H"/> + 
</xsl:template>

</xsl:stylesheet>


C D E F G H I J K L
Red R 7 3 2 Hot Firetruck true true
Green G 50003 5 3 Warm Avocado true
Blue B 17 6 1 Cool
 
R

roy axenov

Mad said:
I wrote an XSLT ("myquery1.xsl" below) that takes some XML
data ("mydata.xml" below) and displays it in HTML table
format.

The problem is that the XML can contain multiple nodes for
H and I and the xsl is only displaying the first one for
each node B.
[...]

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

<xsl:template match="A/B">

Interesting... So the context node in this template would be
a B element.
<xsl:value-of select="C"/> 
Nifty.

<xsl:apply-templates select="A/B/H" /> 

Instead of answering your question, I would like to ask you
one. Are there any B elements in your document that have A
element children?

HTH, HAND.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top