Re: [xsl] Trouble digesting grouping

Subject: Re: [xsl] Trouble digesting grouping
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Fri, 06 Nov 2009 15:04:43 +0100
Flanders, Charles E (US SSA) wrote:
I'm struggling with a grouping problem. I've had some success with simpler structures, but this one has me stumped. This is an XML to XML transform.

I need to eliminate the parent <para > completely, output the <table> intact, but grab the <graphic> elements, create a single <figure>, then a <subfig> wrapper element for each <graphic>. Then output the structures interspersed with the tables. I can't seem to separate the tables and figures. What I'm ending up with is a single <figure> with all the <subfig>, then the tables.

This stylesheet


<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="2.0">

<xsl:output indent="yes"/>

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

  <xsl:template match="install/proc">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="install/para">
    <xsl:for-each-group select="*" group-adjacent="node-name(.)">
      <xsl:choose>
        <xsl:when test="current-grouping-key() eq QName('', 'graphic')">
          <figure>
            <title/>
            <xsl:apply-templates select="current-group()"/>
          </figure>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="current-group()"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </xsl:template>

  <xsl:template match="graphic">
    <subfig>
      <xsl:copy-of select="."/>
    </subfig>
  </xsl:template>

</xsl:stylesheet>


when applied to


<install>
   <proc>
     <step1>
        <para></para>
</step1>
</proc>
<para>
<table>
         All the table stuff
   </table>
   <graphic/>
   <graphic/>
   <graphic/>
   <table>
        All the table stuff
   </table>
   <graphic/>
   <graphic/>
   <graphic/>
</para>
</install>

outputs

<install>

     <step1>
        <para/>
   </step1>

   <table>
         All the table stuff
   </table>
   <figure>
      <title/>
      <subfig>
         <graphic/>
      </subfig>
      <subfig>
         <graphic/>
      </subfig>
      <subfig>
         <graphic/>
      </subfig>
   </figure>
   <table>
        All the table stuff
   </table>
   <figure>
      <title/>
      <subfig>
         <graphic/>
      </subfig>
      <subfig>
         <graphic/>
      </subfig>
      <subfig>
         <graphic/>
      </subfig>
   </figure>
</install>
--

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

Current Thread