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

xml fragment to string

Subject: xml fragment to string
From: Steven Hentschel <steven.hentschel@xxxxxxxxx>
Date: Wed, 30 Jun 2010 11:04:35 +0100
 xml fragment to string
I'm trying to find a way of converting an xml fragment into a string. The 
reason for wanting to do this is so that the fragment can be stored  in an 
RDBMS as a string and I need to escape any apostrophes for the sql statement.

My method that seems to work with the content that I have is to do the 
following:

<xsl:variable name="xmlAsString" as="xs:string+">
 <xsl:apply-templates>
</xsl:variable>

<!-- template to  turn xml fragment into strings -->
<xsl:template match="*">
  <xsl:value-of select="concat('&lt;', name())"/> 
  <xsl:for-each select="@*">
    <xsl:value-of select="concat(' ', name(), '=&quot;', .,'&quot;')"/>
  </xsl:for-each>
  <xsl:value-of select="'&gt;'"/>
  <xsl:value-of select="text()"/> 
  <xsl:apply-templates select="*"/>
  <xsl:value-of select="concat('&lt;/', name(), '&gt;')"/>
</xsl:template>

then to output the text (using replace() to escape apostrophes

replace(string-join($xmlAsString, ''), '''', '''''')

Are there are other better approaches? Is there a way of dealing with mixed 
content?

Steven Hentschel

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.