Re: [xsl] copying complete document, but sort specific childnodes

Subject: Re: [xsl] copying complete document, but sort specific childnodes
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Mon, 07 Sep 2009 12:46:50 +0200
Marc Harding wrote:

thanks for your fast reply, but as you already assumed, i need an xslt
1.0 solution.

i should have made it more clear that i'm limited to php5.2.x
xslt-capabilities (libxml/libxslt).

Here is an attempt to solve it with XSLT 1.0 plus exsl:node-set which I think libxslt supports:


<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:exsl="http://exslt.org/common";
  exclude-result-prefixes="exsl"
  version="1.0">

  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="group">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="*[1]" mode="group">
        <xsl:with-param name="grtf">
          <xsl:copy-of select="*[1]"/>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="group/*[@sort-order]" mode="group">
    <xsl:param name="grtf"/>
    <xsl:choose>
      <xsl:when test="following-sibling::*[1][@sort-order]">
        <xsl:apply-templates select="following-sibling::*[1]" mode="group">
          <xsl:with-param name="grtf">
            <xsl:copy-of select="$grtf"/>
            <xsl:copy-of select="following-sibling::*[1]"/>
          </xsl:with-param>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="exsl:node-set($grtf)/*">
          <xsl:sort select="@sort-order" data-type="number"/>
        </xsl:apply-templates>
        <xsl:apply-templates select="following-sibling::*[1]" mode="group">
          <xsl:with-param name="grtf">
            <xsl:copy-of select="following-sibling::*[1]"/>
          </xsl:with-param>
        </xsl:apply-templates>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="group/*[not(@sort-order)]" mode="group">
    <xsl:apply-templates select="."/>
    <xsl:apply-templates select="following-sibling::*[1]" mode="group">
      <xsl:with-param name="grtf">
        <xsl:copy-of select="following-sibling::*[1]"/>
      </xsl:with-param>
    </xsl:apply-templates>
  </xsl:template>

</xsl:stylesheet>




--


	Martin Honnen
	http://msmvps.com/blogs/martin_honnen/

Current Thread