Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: XSL problem

From: Ben Edgington <usenet@-------.--->
To: NULL
Date: 5/6/2004 7:48:00 PM
"Andy Fish" <ajfish@b...> writes:
> although there are "only a few" formatting elements, I certainly don't fancy
> having to enumerate every possible combination of them.
> 
> Fortunately performance will not be an issue so I can use your idea.

You don't need to choose!

Whilst watching television with my two-year-old this morning I
realised that, of course, recursion is the proper way to maintain
state-information in XSLT. (The challenge presented by the Fimbles is
limited, you understand.)

This solution combines the best of Martin's and mine: it maintains
state information without recalculating it from scratch every time,
and its size is only linear in the number of options considered.

This XSLT,

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

<xsl:output indent="yes"/>

<xsl:template match="/text">
  <doc>
    <xsl:apply-templates select="text()|node()">
      <xsl:with-param name="italic" select="0"/>
      <xsl:with-param name="bold" select="0"/>
    </xsl:apply-templates>
  </doc>
</xsl:template>

<xsl:template match="text()">
  <xsl:param name="italic"/>
  <xsl:param name="bold"/>
  <xsl:if test="normalize-space(.)">
    <text>
      <xsl:if test="$bold">
        <xsl:attribute name="bold">true</xsl:attribute>
      </xsl:if>
      <xsl:if test="$italic">
        <xsl:attribute name="italic">true</xsl:attribute>
      </xsl:if>
      <xsl:value-of select="normalize-space(.)"/>
    </text>
  </xsl:if>
</xsl:template>

<xsl:template match="bold">
  <xsl:param name="italic"/>
  <xsl:apply-templates select="text()|node()">
    <xsl:with-param name="italic" select="$italic"/>
    <xsl:with-param name="bold" select="1"/>
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="italic">
  <xsl:param name="bold"/>
  <xsl:apply-templates select="text()|node()">
    <xsl:with-param name="italic" select="1"/>
    <xsl:with-param name="bold" select="$bold"/>
  </xsl:apply-templates>
</xsl:template>

</xsl:stylesheet>


with this XML


<text>
  this is plain
  <bold>
    this is bold
    <italic>
      this is bold-italic
    </italic>
  </bold>
  this is plain
  <italic>this is italic
    <bold>this is italic-bold</bold>
  </italic>
</text>


gives this output
<?xml version="1.0"?>
<doc>
  <text>this is plain</text>
  <text bold="true">this is bold</text>
  <text bold="true" italic="true">this is bold-italic</text>
  <text>this is plain</text>
  <text italic="true">this is italic</text>
  <text bold="true" italic="true">this is italic-bold</text>
</doc>


Hope you enjoy it.

Ben

-- 
Ben Edgington
  Mail to the address above is discarded.
  Mail to ben at that address might be read.
http://www.edginet.org/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent