Page 1 of 1
Merging element values
Posted: Wed Sep 27, 2006 3:26 pm
by DarkStar
I'm looking for an XSLT 1.0 solution to merge element values for similar items into comma separated list in the following way:
XML:
Code: Select all
<results>
<row>
<number>1</number>
<field>A</field>
</row>
<row>
<number>1</number>
<field>B</field>
</row>
<row>
<number>2</number>
<field>C</field>
</row>
</results>
Wanted result:
Code: Select all
<results>
<row>
<number>1</number>
<field>A, B</field>
</row>
<row>
<number>2</number>
<field>C</field>
</row>
</results>
The number of entries for each given 'number' is not known in advance.
Any help appreciated.
Posted: Wed Sep 27, 2006 3:48 pm
by george
Hi,
This is a grouping problem. A possible solution is to define a key to match all the rows based in the number. Then you can match only on the first row element that has a specific number and generate the new row there. For the field content iterate all the rows with the same number ang generate the field content from them:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:key name="rowsByNumber" match="row" use="number"/>
<xsl:template match="results">
<results><xsl:apply-templates/></results>
</xsl:template>
<xsl:template match="row[generate-id(.)=generate-id(key('rowsByNumber', number)[1])]">
<row>
<xsl:copy-of select="number"/>
<field>
<xsl:for-each select="key('rowsByNumber', number)">
<xsl:value-of select="field"/>
<xsl:if test="position()!=last()">, </xsl:if>
</xsl:for-each>
</field>
</row>
</xsl:template>
<xsl:template match="*"/>
</xsl:stylesheet>
Best Regards,
George
Posted: Thu Sep 28, 2006 2:00 am
by DarkStar
Got that working. Thanks George!
Never done any grouping with keys before (pretty new to all this), so I wouldn't have figured that out by myself.
As an extension to the problem, I've found that sometimes there are empty values for 'field' for a given 'number' and sometimes there are duplicate values of 'field', so now I need to apply the above but only when the next value of 'field' is not empty and not a duplicate value for the current 'number'.
I'm assuming some if statements will be needed - any suggestions/solutions you could add would again be appreciated.
Posted: Thu Sep 28, 2006 2:28 am
by DarkStar
DarkStar wrote:
I'm assuming some if statements will be needed - any suggestions/solutions you could add would again be appreciated.
Half way there. Used
around the two lines inside the for-each. That makes it ignore empty values of 'field'.
This means that something like
Code: Select all
<xsl:if test="not((field = '') or (IsItADuplicateCheck))">
would finish off my problem. Just need to figure out how to do the IsItADuplicateCheck. Can't do a string compare() as it's not 2 strings. What do I need to see if value is already present within the current set of selected 'number's?
Posted: Thu Sep 28, 2006 5:03 am
by DarkStar
DarkStar wrote: Just need to figure out how to do the IsItADuplicateCheck.
OK, sorted it out with
Code: Select all
field != preceding::row[number = current()/number]/field
as the check for duplicates. Works nicely.