Home > Enterprise >  Using xsl group-starting-buy to group-by elements between two nodes
Using xsl group-starting-buy to group-by elements between two nodes

Time:12-07

So I have an XML that resembles to something of this type which I would like to convert into wikicode:

<result>
    <h3>Heading 1</h3>
    <doc>
    <filepath>Filepath 1</filepath>
    <filename>Filename 1</filename>
    <description>Desc 1</description>
    </doc>
    <doc>
    <filepath>Filepath 12</filepath>
    <filename>Filename 12</filename>
    <description>Desc 12</description>
    </doc>
    <h3>Heading 2</h3>
    <doc>
    <filepath>Filepath 21</filepath>
    <filename>Filename 21</filename>
    <description>Desc 21</description>
    </doc>
    <doc>
    <filepath>Filepath 22</filepath>
    <filename>Filename 22</filename>
    <description>Desc 22</description>
    </doc>
    <h3>Heading 3</h3>
    <doc>
    <filepath>Filepath 31</filepath>
    <filename>Filename 31</filename>
    <description>Desc 31</description>
    </doc>
    <doc>
    <filepath>Filepath 31</filepath>
    <filename>Filename 31</filename>
    <description>Desc 31</description>
    </doc>
 <result>

  

What I would like to do is to group by all the doc elements by the h3 above them. To have something like this :

*Heading 1
**Filename1 : Desc1
**Filename12 : Desc12
*Heading 2
**Filename 21 : Desc21
**Filename 22 : Desc22

Here is a snippet of my xsl :

<xml>
  <xsl:for-each-group select=".//result" group-starting-with="h3">
     *<xsl:value-of select="current()"/>
      <xsl:for-each select="current-group()">
        **<xsl:value-of select="./doc/filename"/> : <xsl:value-of select="./doc/description"/> 
      </xsl:for-each>
    </xsl:for-each-group>
</xml>

However, this doesn't work and the value of current() sends me the value of all the sub-nodes which is not what I want. My basic understanding of XSL is slightly limited. Any help would be appreciated. Thank You.

CodePudding user response:

Use xsl:for-each-group select=".//result/*" group-starting-with="h3" if the input is as shown.

And inside I think you want to process <xsl:for-each select="current-group()[position() ge 1]"> or <xsl:for-each select="current-group()[not(self::h3)]"> to process the items following the h3 and the value-ofs then are relative to them and use select="filename" and select="description".

  • Related