xsl-list
[Top] [All Lists]

RE: parse inside a cdtata

2003-06-03 05:24:29
Hi,

I d like to know if it's possible to parse a cdata content in XSL.
I have something like this:
<CONTENT TYPE="text/html"><![CDATA[<p align="left"><font 
size="12"></font><font size="12" face="tahoma" 
color="#000000">one</font></p><p align="left"><font 
size="12"></font></p>]]></CONTENT>

There are extension functions to do this in some XSLT engines, but other than 
that, you have to either preprocess the document or write an XML parser in XSLT.

Or may be is there a way to get the unformatted html text of a cdata? 
("one" in my example)

Preprocess the data, since XSLT works on trees, not character data that is an 
SGML document fragment. You can, of course, write an template like

  <xsl:template match="CONTENT" name="character-parser">
    <xsl:param name="text" select="."/>
    <xsl:choose>
      <xsl:when test="contains($text, '&lt;')">
        <xsl:value-of select="substring-before($text, '&lt;')"/>
        <xsl:call-template name="character-parser">
          <xsl:with-param name="text" 
select="substring-after(substring-after($text, '&lt;'), '>')"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

that will give "one" with the input above, I'm pretty sure it won't take long 
for you to get input that will not be handled by your parser stylesheet. Fix 
your input.

Cheers,

Jarno - Neurotic Fish: Wake Me Up

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>