Re: [xsl] Transform inline-block type elements to block-level elements

Subject: Re: [xsl] Transform inline-block type elements to block-level elements
From: "Christian Roth" <roth@xxxxxxxxxxxxxx>
Date: Thu, 30 Nov 2006 22:18:30 +0100
David Carlisle wrote:

>You'd be better with something like the following.
>[nice XSLT solution snipped]

Thanks David! Wanted to post this much earlier, but did not get the time.

I had created another solution about the time you posted yours, but
which I think takes a way more awkward route (though I see both
solutions use a temporary element wrapper for text nodes resp. non-
element nodes and we both make copies or references to the original
nodes to work on), with some benefits thrown in.

The difference to your solution seems to be that I am flattening the
tree completely, attaching nesting level information as attributes, and
then group on this, and that I am not using dedicated templates for the
elements, but use a single variable for storing the block-level elements
to work on. I have extended the XSLT in such a way that it can cope with
any (unknown in advance) inlines plus any set attributes, empty elements
and PIs in a generic way.

I think mixing both of our approaches could yield something usable. :-)

The sample XML, my XSLT code and the intended result is available from here:

<http://www.visualclick.de/misc/extract-inlineblocks.zip>

(I decided to put it online and not inline since some of the XPath
expressions are longer than can be swiftly handled within email's 80
char lien width limit.)



It also provides some first step at a solution for Andreas.M's question
you referred to (<http://www.biglist.com/lists/xsl-list/archives/200610/
msg00744.html>), just use a configuration in the XSLT similar to this one:

--snip--
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
  xmlns:cr="http://www.visualclick.de/namespace/temp";
  xmlns:xs="http://www.w3.org/2001/XMLSchema"; 
  version="2.0"
  xmlns:text="http://www.visualclick.de/namespace/text";
  xmlns:draw="http://www.visualclick.de/namespace/draw";
  xmlns:svg="http://www.visualclick.de/namespace/svg";
  xmlns:style="http://www.visualclick.de/namespace/style";
  xmlns:xlink="http://www.visualclick.de/namespace/xlink";
  exclude-result-prefixes="cr xs"
  >

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

  <xsl:param name="blocknames" select="(
    QName('http://www.visualclick.de/namespace/draw', 'draw:frame'),
    QName('http://www.visualclick.de/namespace/draw', 'draw:image'),

    QName('http://www.visualclick.de/namespace/text', 'text:h'),
    QName('http://www.visualclick.de/namespace/text', 'text:p')
    )" />

  <xsl:template match="root">
    <root
        xmlns:text="http://www.visualclick.de/namespace/text";
        xmlns:draw="http://www.visualclick.de/namespace/draw";
        xmlns:svg="http://www.visualclick.de/namespace/svg";
        xmlns:style="http://www.visualclick.de/namespace/style";
        xmlns:xlink="http://www.visualclick.de/namespace/xlink";>
      <xsl:apply-templates/>
    </root>
  </xsl:template>
  
  <xsl:template match="text:h|text:p">
    <xsl:call-template name="block-root">
      <xsl:with-param name="grouping-element" select="$grouping-element"
tunnel="yes"/>
    </xsl:call-template>
  </xsl:template>
  
  ...

--snip--

Well, if it isn't useful in any other way, then it's at least an example
of horrible XSLT code...

-Christian.

Current Thread