I have to give a legacy application (non-XML/XHTML aware) a fragment of
XHTML.
That is done trough converting the fragment to text and storing it in a
text file. The legacy application reads the file, considers it's just a
bunch of bytes and hands it back as is to a web application when required.
XSL is awsome when you work with XML in and out, but trying to output
text... it's doesn't look so easy and natural !
So the problem is : converting an XHTML (in fact any XML) fragment to text.
We run Xalan-C 1.10 (then obviously XSLT1.0)
That's what I did :
XML fragment :
<fragment>
<div>Any XML/XHTML <span style="color:black">elements with attributes
also</span> or empty tags <br/> and empty tags with attributes <hr
style="color:white"/></div>
</fragment>
XSL templates :
<xsl:output method="text" encoding="utf-8"
omit-xml-declaration="yes" indent="no"/>
<xsl:template match="/">
<!-- Some code here to output other pieces of text -->
<!-- Here we Call the XML to Text Templates on our fragment -->
<xsl:variable name="xhtml"><xsl:apply-templates
select="fragment"/></xsl:variable>
<!-- And we output the text -->
<xsl:value-of select="$xhtml"/>
</xsl:template>
<!-- XML to text templates -->
<xsl:template match="fragment//element()">
<xsl:choose>
<!-- for not empty elements -->
<xsl:when test="*|text()">
<xsl:text><</xsl:text>
<xsl:value-of select="name(.)"/>
<xsl:apply-templates select="@*"/>
<xsl:text>></xsl:text>
<xsl:apply-templates select="*|text()"/>
<xsl:text></</xsl:text>
<xsl:value-of select="name(.)"/>
<xsl:text>></xsl:text>
</xsl:when>
<!-- for empty elements -->
<xsl:otherwise>
<xsl:text><</xsl:text>
<xsl:value-of select="name(.)"/>
<xsl:apply-templates select="@*"/>
<xsl:text>/></xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="fragment//text()">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="fragment//@*">
<xsl:text> </xsl:text>
<xsl:value-of select="name(.)"/>
<xsl:text>="</xsl:text>
<xsl:value-of select="."/>
<xsl:text>"</xsl:text>
</xsl:template>
<!-- We do not have a template for comments or P.I., we drop them -->
All this works fine but it's quite a hedache and will be a pain for
maintenance.
So my question is : do you think there is a simpler way of fulfilling
the same function (even if it means moving to XSLT2.0... I would then
have to push my enterprise to Saxon which I prefer) ?
--~------------------------------------------------------------------
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>
--~--