0

this is my first stackoverflow question :

I have an XML file like this:

<ROOT>
 <COLUMN ID="AA1">
  <CAPTION>Some Text 1</CAPTION>
 </COLUMN>
 <COLUMN ID="AA1">
  <CAPTION>Some Text 2</CAPTION>
 </COLUMN>
 <COLUMN ID="AA1">
  <CAPTION>Some Text 3</CAPTION>
 </COLUMN>
 <COLUMN ID="AA2">
  <CAPTION>Some Text 4</CAPTION>
 </COLUMN>
 <COLUMN ID="AA2">
  <CAPTION>Some Text 5</CAPTION>
 </COLUMN>
</ROOT>

and I want tranform it to this :

Result should be that all column IDs are grouped and the CAPTION concated in each group

I do not know the IDs value and I dont care about them. Just group by them.

this should by the result, two strings in this case :

"Some Text 1 Some Text 2 Some Text 3"

"Some Text 4 Some Text 5"

all this, using Xpath2

I am realy not sure if its can be done only with xpath expession, but I hope to...

Has someone an idea ?

  • So, was any of the answers helpfull? Did you manage to solve this issue? Please update this SO item; maybe you can check 1 of the answers as 'correct'... – Reinder Wit May 26 '14 at 13:11

2 Answers2

0

Use

for $id in distinct-values(/ROOT/COLUMN/@ID)
return string-join(/ROOT/COLUMN[@ID = $id]/CAPTION, ' ')
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
0

For grouping, have a look at this answer. You can achieve this by using an XSLT like this one:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="yes"/>
    <xsl:key name="id" match="COLUMN" use="@ID" />

    <xsl:template match="ROOT">
        <OUTPUT>
            <xsl:apply-templates select="COLUMN[generate-id(.)=generate-id(key('id',@ID)[1])]" />
        </OUTPUT>
    </xsl:template>

    <xsl:template match="COLUMN">
        <xsl:element name="COLUMN">
            <xsl:attribute name="ID">
                <xsl:value-of select="@ID" />
            </xsl:attribute>
            <xsl:for-each select="key('id', @ID)">
                <xsl:value-of select="CAPTION" />
                <xsl:text> </xsl:text>  
            </xsl:for-each>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

It will output like this:

<OUTPUT>
    <COLUMN ID="AA1">Some Text 1 Some Text 2 Some Text 3 </COLUMN>
    <COLUMN ID="AA2">Some Text 4 Some Text 5</COLUMN>
</OUTPUT>
Community
  • 1
  • 1
Reinder Wit
  • 6,490
  • 1
  • 25
  • 36