[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Transform inline-block type elements to block-leve

Subject: Re: Transform inline-block type elements to block-level elements
From: David Carlisle <davidc@xxxxxxxxx>
Date: Sun, 26 Nov 2006 01:57:56 GMT
Re:  Transform inline-block type elements to block-leve
> How will this work with my example given? 

Not very well....

You'd be better with something like the following.

(Excepting of course some people reading the currently active parallel
thread on this mailing list may wish to comment adversely on my choice
of variable and mode names..)

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
<xsl:strip-space elements="*"/>
  <xsl:output indent="no"/>

<xsl:template match="/">
  <xsl:variable name="a">
    <xsl:apply-templates mode="a"/>
  </xsl:variable>
  <xsl:apply-templates mode="b" select="$a"/>
</xsl:template>

<xsl:template mode="a" match="root|p|ul|li">
  <xsl:copy>
    <xsl:apply-templates mode="a"/>
  </xsl:copy>
</xsl:template>


<xsl:template mode="a" match="em|strong|code">
  <xsl:param name="inlines" tunnel="yes"/>
  <xsl:apply-templates mode="a" >
    <xsl:with-param name="inlines" tunnel="yes"
  select="$inlines,name()"/>
  </xsl:apply-templates>
</xsl:template>

<xsl:template mode="a" match="text()">
  <xsl:param name="inlines" tunnel="yes"/>
  <text inlines="{$inlines}">
    <xsl:value-of select="normalize-space(.)"/>
  </text>
</xsl:template>

<xsl:template mode="b" match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates mode="b"/>
  </xsl:copy>
</xsl:template>

<xsl:template mode="b" match="p">
<xsl:text>&#10;</xsl:text>
  <xsl:for-each-group select="*" group-adjacent="boolean(self::ul)">
    <xsl:choose>
      <xsl:when test="current-grouping-key()">
      <xsl:apply-templates mode="b" select="current-group()"/>
      </xsl:when>
      <xsl:otherwise>
<xsl:text>&#10;</xsl:text>
	<p>
	  <xsl:apply-templates mode="b" select="current-group()"/>
	  </p>
<xsl:text>&#10;</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each-group>
<xsl:text>&#10;</xsl:text>
</xsl:template>

<xsl:template mode="b" match="li">
<xsl:text>&#10;</xsl:text>
  <li>
    <xsl:for-each-group select="*" group-adjacent="boolean(self::ul)">
      <xsl:choose>
      <xsl:when test="current-grouping-key()">
        <xsl:apply-templates mode="b" select="current-group()"/>
	</xsl:when>
	<xsl:otherwise>
<xsl:text>&#10;</xsl:text>
	  <p>
	      <xsl:apply-templates mode="b" select="current-group()"/>
	        </p>
<xsl:text>&#10;</xsl:text>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </li>
<xsl:text>&#10;</xsl:text>
</xsl:template>


<xsl:template mode="b" match="text">
  <xsl:param name="s" select="tokenize(@inlines,'\s+')[.!='']"/>
  <xsl:choose>
    <xsl:when test="empty($s)">
      <xsl:value-of select="."/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:element name="{$s[1]}">
      <xsl:apply-templates mode="b" select=".">
        <xsl:with-param name="s" select="$s[position()!=1]"/>
	</xsl:apply-templates>
      </xsl:element>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>

$ saxon8 inline.xml  inline.xsl 
<?xml version="1.0" encoding="UTF-8"?><root>

<p>one<em>two</em></p>
<ul>
<li>
<p><em>three</em></p>
</li>

<li>
<p><em><strong>four</strong></em></p>
<ul>
<li>
<p><em><strong><code>five</code></strong></em></p>
</li>
</ul>
<p><em><strong><code>six</code></strong></em></p>
</li>
</ul>
</root>

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.