Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: XSL problem

From: Ben Edgington <usenet@-------.--->
To: NULL
Date: 5/5/2004 4:25:00 PM
Hi Andy,

ajfish@b... (Andy Fish) writes:
> I'm stuck with an XSL problem - can anyone give me any hints?
> 
> I have some XML with nested formatting tags like this:
> 
> <text>
>   this is plain
>   <bold>
>     this is bold
>     <italic>
>       this is bold-italic
>     </italic>
>   </bold>
>   this is plain
> </text>
> 
> which I need to 'flatten out' into something like this:
> 
> <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>
> 
> It doesn't have to work with any arbitrary tags - there are only a few
> possible ones - but I'm not sure how to "remember" the outer level
> formatting nodes when processing the text inside. It seems to be
> crying out for some kind of state variable

Here's a brute force version that uses XPath to look at the ancestor
axis.  Perhaps not as elegant as Martin's solution, but shorter and
easier to extend (Martin's script will grow as the factorial of the
number of options, I think, whereas this is only linear).

This transformation:

<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()"/>
  </doc>
</xsl:template>

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

</xsl:stylesheet>


with this input
<text>
  this is plain
  <bold>
    this is bold
    <italic>
      this is bold-italic
    </italic>
  </bold>
  this is plain
</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>
</doc>

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
Digg
delicious
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