PDA

View Full Version : How to concatenate similar element values in XSL


manasnair
07-28-2009, 11:16 AM
Hi, I am using an XML and the following is a small snippet from it:

<Field name="summary">
<Text highlighted="0">A</Text >
<Text highlighted="1">B</Text >
<Text highlighted="0">C</Text >
<Cut />
<Text highlighted="0">D</Text >
<Text highlighted="1">E</Text >
<Text highlighted="0">F</Text >
<Cut />
</Field>


I am using an XSL to convert it into another xml.

I need the strcuture as:

<Result>
<set></set>
</Result>

There should be as many <set> elements in the result as there are <Cut> elements in the source. ie.

The result should look like:

<Result>
<set>ABC</set>
<set>DEF</set>
</Result>

The <Cut> element should act as the separator. The <Text> elements before and after the <Cut> element should be seggragated and shown.

Here is the snippet of the xsl that i used...

<xsl:if test="@name='summary'">
<S>
<xsl:for-each select="*">
<xsl:choose>
<xsl:when test="local-name()='Text'">
<xsl:if test="@highlighted='0'">
<xsl:value-of select="."/>
</xsl:if>
<xsl:if test="@highlighted='1'">
<xsl:text disable-output-escaping="no">&lt;b&gt;</xsl:text>
<xsl:value-of select="."/>
<xsl:text disable-output-escaping="no">&lt;/b&gt;</xsl:text>
</xsl:if>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</S>
</xsl:if>


Please help.
Thanks a lot in advance.

Manas

RossMcCaughrain
07-29-2009, 01:51 PM
Youd be better changing your xml structure to make it clearer what your trying to achieve

<Field name="summary">
<set>
<Text highlighted="0">A</Text >
<Text highlighted="1">B</Text >
<Text highlighted="0">C</Text >
</set>
<set>
<Text highlighted="0">D</Text >
<Text highlighted="1">E</Text >
<Text highlighted="0">F</Text >
</set>
</Field>

Then using:

<xsl:for-each select="/Field/set">
<xsl:for-each select="./text">
</xsl:value-of select=".">
</xsl:for-each>
</xsl:for-each>

manasnair
07-30-2009, 02:04 PM
I am actually having the same xml structure as is given in my code. i.i

<Field name="summary">
<Text highlighted="0">A</Text >
<Text highlighted="1">B</Text >
<Text highlighted="0">C</Text >
<Cut />
<Text highlighted="0">D</Text >
<Text highlighted="1">E</Text >
<Text highlighted="0">F</Text >
<Cut />
</Field>


This xml is returned from the server and I have to transform it as i said. I cannot change the inherent structure.
It is not as you suggested


<Field name="summary">
<set>
<Text highlighted="0">A</Text >
<Text highlighted="1">B</Text >
<Text highlighted="0">C</Text >
</set>
<set>
<Text highlighted="0">D</Text >
<Text highlighted="1">E</Text >
<Text highlighted="0">F</Text >
</set>
</Field>


I hope it's clear now.