Re: [xsl] Re: Grouping by attribute

Subject: Re: [xsl] Re: Grouping by attribute
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Wed, 21 Oct 2009 15:04:08 +0200
Joo Lajos wrote:

<par class="Bullet 6" xml:lang="hu">
Valami szoveg <inline style="color: #ff0000;">
Sima inline
</inline>
szoveg <inline style="color: #ff0000;">
inline break elott.
<linebreak/> inline break utan
</inline>
szoveg break elott <linebreak/>
szoveg break utan <inline style="color: #ff0000;">
inline bold elott <bold>
inline bold break elott <linebreak/>
inline bold break utan
</bold> inline bold utan
</inline> Sima szoveg.
</par>
Indenting is not too pretty but it can be seen that there are linebreaks in <par> in <par><inline> and in <par><inline><bold>
I need to break up this into :


<par class="Bullet 6" xml:lang="hu">
Valami szoveg <inline style="color: #ff0000;">
Sima inline
</inline>
szoveg <inline style="color: #ff0000;">
inline break elott. </inline>
</par>
<par class="Bullet 6" xml:lang="hu">
<inline style="color: #ff0000;"> inline break utan
</inline> szoveg break elott </par>
<par class="Bullet 6" xml:lang="hu">
szoveg break utan <inline style="color: #ff0000;">
inline bold elott <bold>
inline bold break elott </bold>
</inline>
</par>
<par class="Bullet 6" xml:lang="hu">
<inline style="color: #ff0000;">
<bold>
inline bold break utan
</bold> inline bold utan
</inline> Sima szoveg.
</par>

Here is an XSLT 2.0 stylesheet that produces the described output:


<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="2.0"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema";
  exclude-result-prefixes="xsd">

<xsl:output indent="yes"/>

  <xsl:template match="@*">
    <xsl:copy/>
  </xsl:template>

  <xsl:template match="node()">
    <xsl:param name="cn"/>
    <xsl:if test="descendant-or-self::node() intersect $cn">
      <xsl:copy>
        <xsl:apply-templates select="@*, node()">
          <xsl:with-param name="cn" select="$cn"/>
        </xsl:apply-templates>
      </xsl:copy>
    </xsl:if>
  </xsl:template>

<xsl:template match="par">
<xsl:variable name="p" as="element()" select="."/>
<xsl:for-each-group select="descendant::node()" group-ending-with="linebreak">
<xsl:variable name="lb" as="element()?" select="current-group()[self::linebreak]"/>
<par>
<xsl:apply-templates select="$p/@*"/>
<xsl:apply-templates select="$p/node()[descendant-or-self::node() intersect (current-group() except $lb)]">
<xsl:with-param name="cn" select="current-group() except $lb"/>
</xsl:apply-templates>
</par>
</xsl:for-each-group>
</xsl:template>


</xsl:stylesheet>

--

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

Current Thread