Merging element values
Here should go questions about transforming XML with XSLT and FOP.
Merging element values
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:
Wanted result:
The number of entries for each given 'number' is not known in advance.
Any help appreciated.
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>
Code: Select all
<results>
<row>
<number>1</number>
<field>A, B</field>
</row>
<row>
<number>2</number>
<field>C</field>
</row>
</results>
Any help appreciated.
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:
Best Regards,
George
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>
George
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.
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.
Half way there. UsedDarkStar wrote: I'm assuming some if statements will be needed - any suggestions/solutions you could add would again be appreciated.
Code: Select all
<xsl:if test="not(field = '')">
This means that something like
Code: Select all
<xsl:if test="not((field = '') or (IsItADuplicateCheck))">
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service