xsl-list
[Top] [All Lists]

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

2006-11-25 18:58:12

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>



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--