Re: [xsl] group by tags

Subject: Re: [xsl] group by tags
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Mon, 23 Jun 2008 14:24:57 +0200
IZASKUN GUTIERREZ GUTIERREZ wrote:

I need group by tags this xml for example. The problem is that I dont know the name of the tags. Only I know the tag "Book" and the ID "boo2"

Here is an XSLT 2.0 solution:


<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
  version="2.0">

<xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Book[@rdf:ID = 'boo2']">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:for-each-group select="*" group-by="name()">
        <xsl:element name="{current-grouping-key()}">
          <xsl:copy-of select="@* | text()"/>
          <xsl:for-each-group select="current-group()/*" group-by="name()">
            <xsl:element name="{current-grouping-key()}">
              <xsl:copy-of select="current-group()/*"/>
            </xsl:element>
          </xsl:for-each-group>
        </xsl:element>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

For your provided XML sample it gives the result you describe.


--


	Martin Honnen
	http://JavaScript.FAQTs.com/

Current Thread