embedding xml in xml as string

Discussion in 'XML' started by shaun roe, Nov 2, 2005.

  1. shaun roe

    shaun roe Guest

    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.
     
    shaun roe, Nov 2, 2005
    #1
    1. Advertising

  2. shaun roe

    shaun roe Guest

    In article
    <>,
    shaun roe <> wrote:

    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>
     
    shaun roe, Nov 2, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. terry
    Replies:
    0
    Views:
    390
    terry
    Jul 9, 2003
  2. Mark Van Orman

    embedding xml in xml as non-xml :)

    Mark Van Orman, Sep 14, 2004, in forum: XML
    Replies:
    5
    Views:
    481
    Patrick TJ McPhee
    Sep 15, 2004
  3. Elie B.
    Replies:
    1
    Views:
    275
    Paul Miller
    Feb 8, 2004
  4. Guest
    Replies:
    2
    Views:
    340
    Guest
    Dec 26, 2006
  5. Kee Nethery
    Replies:
    12
    Views:
    2,091
    Stefan Behnel
    Jun 27, 2009
Loading...

Share This Page