Merging element values

Here should go questions about transforming XML with XSLT and FOP.
DarkStar
Posts: 4
Joined: Wed Sep 27, 2006 3:13 pm

Merging element values

Post 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.
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post 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
DarkStar
Posts: 4
Joined: Wed Sep 27, 2006 3:13 pm

Post 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.
DarkStar
Posts: 4
Joined: Wed Sep 27, 2006 3:13 pm

Post 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

Code: Select all

<xsl:if test="not(field = '')">
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?
DarkStar
Posts: 4
Joined: Wed Sep 27, 2006 3:13 pm

Post 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.
Post Reply