[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

XML/XHTML fragment to text

Subject: XML/XHTML fragment to text
From: Alain <alainb06@xxxxxxx>
Date: Thu, 09 Aug 2007 22:44:07 +0200
 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>



<!-- XML to text templates --> <xsl:template match="fragment//element()"> <xsl:choose> <!-- for not empty elements --> <xsl:when test="*|text()"> <xsl:text>&lt;</xsl:text> <xsl:value-of select="name(.)"/> <xsl:apply-templates select="@*"/> <xsl:text>&gt;</xsl:text> <xsl:apply-templates select="*|text()"/> <xsl:text>&lt;/</xsl:text> <xsl:value-of select="name(.)"/> <xsl:text>&gt;</xsl:text> </xsl:when> <!-- for empty elements --> <xsl:otherwise> <xsl:text>&lt;</xsl:text> <xsl:value-of select="name(.)"/> <xsl:apply-templates select="@*"/> <xsl:text>/&gt;</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) ?


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.