Click to See Complete Forum and Search --> : Help with XSL, xsl:for-each ...


Sudhir5
11-02-2006, 12:52 PM
Hi

Below is a sample of an XML file that I have. In every <Entity>, 3 tags always appear exactly once, which are <Name>, <Date> and <Speed>.
The other two tags, <Setting> and <Label> are a pair and they may appear any number of times. For instance, an <Entity> could contain 3 <Setting> and <Label> and another may contain 5.

<Entity>
<Name> a </Name>
<Date> b </Date>
<Speed>
<Setting> c </Setting>
<Label> d </Label>

<Setting> e </Setting>
<Label> f </Label>

<Setting> g </Setting>
<Label> h </Label>

</Speed>
</Entity>

I am trying to produce a XSL for give output something like

Name Date Setting Label
a
b
c
d
a b e f
a b g h

Each time there is a <Setting> and <Label>, a new row gets output with the same <Name>, <Date> but with new <Setting> and <Label>.

Please can someone give me some sample code of a XSL to achieve this.

Many thanks

btm
11-27-2006, 10:08 AM
Here is one way of doing it, assuming you want html output this with give you a table with the desired results:

<xsl:template match="Entity">
<table>
<tr>
<th>Name</th>
<th>Date</th>
<th>Setting</th>
<th>Label</th>
</tr>
<xsl:apply-templates select="Speed">
<xsl:with-param name="name" select="Name"/>
<xsl:with-param name="date" select="Date"/>
</xsl:apply-templates>
</table>
</xsl:template>

<xsl:template match="Speed">
<xsl:param name="name"/>
<xsl:param name="date"/>
<xsl:for-each select="Setting">
<tr>
<td>
<xsl:value-of select="$name"/>
</td>
<td>
<xsl:value-of select="$date"/>
</td>
<td>
<xsl:value-of select="."/>
</td>
<td>
<!-- display the immeadiate sibling if it is a Label -->
<xsl:value-of select="following-sibling::*[1][self::Label]"/>
</td>
</tr>
</xsl:for-each>
</xsl:template>