embedding xml in xml as string

S

shaun roe

I have an xml format for creating a database; lets say a fragment looks
like this:

<insertValue type="int">7</insert>
<insertValue type="string">Hello everyone</insert>


now I have a document I am transforming into this format using XSLT:

<nDwarves>7</nDwarves>
<snowWhiteSays>Hello everyone</snowWhiteSays>
<groupReply><possibleReplies replyTo="greeting">hello</possibleReplies>
<possibleReplies replyTo="workSignal">hi ho, hi ho</possibleReplies>
</groupReply>


My problem is that I want to put the entire contents of the <groupReply>
node into a string, escaping the "<" and ">", so that I have an
insertValue node containing the string which is essentially a bit of
escaped xml.

Unfortunately, <xsl:copy-of... > doesnt have an option to escape the xml
characters, so far as I can see. Is there another way?


background:
why do this?
I am using the Xerces2.3 parser to get the text content of the
<insertValue> nodes, then I upload it. If I have straight xml inside
this node, then getTextContent simply returns the collapsed text content
of all the child elements ­ with no element names etc, when what I want
is the actual xml fragment to insert in my database as a string. I hope
to get around this by escaping the xml, so it really looks like just
another string.
 
S

shaun roe

it probably bad form to answer your own question, but I think this will
do it:

<!-- serializing the embedded xml into a string -->
<xsl:template name="serialize">
<xsl:param name="nodeset"/>
<!-- openstarttag, openendtag, closetag markers, and close empty tag
marker -->
<xsl:variable name="ot" select="'&lt;'"/>
<xsl:variable name="oet" select="'&lt;/'"/>
<xsl:variable name="ct" select="'&gt;'"/>
<xsl:variable name="cet" select="'/&gt;'"/>
<xsl:for-each select="$nodeset">
<xsl:variable name="n" select="name()"/>
<xsl:variable name="value" select=".[not(./*)]"/>
<xsl:variable name="attributes">
<xsl:for-each select="./@*">
<xsl:call-template name="serializeAttribute"/>
</xsl:for-each>
</xsl:variable>
<xsl:choose>
<xsl:when test="empty(.)">
<xsl:value-of select="concat($ot,$n,$attributes,$cet,$LF)"/>
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="concat($ot,$n,$attributes,$ct,$value)"/>
<xsl:call-template name="serialize">
<xsl:with-param name="nodeset" select="./*"/>
</xsl:call-template>
<xsl:value-of select="concat($oet,$n,$ct,$LF)"/>
</xsl:eek:therwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<!--serialize one attribute -->
<xsl:template name="serializeAttribute">
<xsl:variable name="q" select="'&quot;'"/>
<xsl:variable name="an" select="name()"/>
<xsl:variable name="av" select="."/>
<xsl:value-of select="concat(' ',$an,'=',$q,$av,$q)"/>
</xsl:template>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top