[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] XML/XHTML fragment to text
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>
<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) ?
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|