Re: Parsing copy-of result tree fragment

Subject: Re: Parsing copy-of result tree fragment
From: Mike Brown <mike@xxxxxxxx>
Date: Thu, 30 Nov 2000 17:44:43 -0700 (MST)
Mike Mahoney wrote:
> How does someone take a tree fragment from a copy-of and then turn
> around and use the results and parse out/replace certain characters.

You don't. Once you have used xsl:copy-of, you have put a copy of a branch
of the source tree into the result tree. You never had the opportunity to
change anything before it went into the result tree.

With pure XSLT 1.0, the solution involves using xsl:copy. xsl:copy works
like xsl:copy-of, but only copies the current node. The contents of the
xsl:copy element are used to generate the rest of the descendants of the
copied node.

> If the original xml were
> <object>
>   <data1>
>     <sub-data>
>       joe's data
>     </sub-data>
>   </data1>
> </object>
> and the resulting fragment from the copy-of was
> <sub-data>
>   joe's data
> </sub-data>
> I'd want the final output to be
> <sub-data>
>   joe\'s data
> </sub-data>

<xsl:template match="sub-data">
    <xsl:apply-templates select="node()|@*" mode="copying"/>

<xsl:template match="text()" mode="copying">
  <xsl:call-template name="SubStringReplace">
    <xsl:with-param name="stringIn" select="."/>
    <xsl:with-param name="substringIn">'</xsl:with-param>
    <xsl:with-param name="substringOut">\'</xsl:with-param>

<xsl:template name="SubstringReplace">
   <xsl:param name="stringIn"/>
   <xsl:param name="substringIn"/>
   <xsl:param name="substringOut"/>
      <xsl:when test="contains($stringIn,$substringIn)">
         <xsl:value-of select="concat(substring-before($stringIn,$substringIn),$substringOut)"/>
         <xsl:call-template name="SubstringReplace">
            <xsl:with-param name="stringIn" select="substring-after($stringIn,$substringIn)"/>
            <xsl:with-param name="substringIn" select="$substringIn"/>
            <xsl:with-param name="substringOut" select="$substringOut"/>
         <xsl:value-of select="$stringIn"/>

   - Mike
Mike J. Brown, software engineer at            My XML/XSL resources: in Denver, Colorado, USA    

 XSL-List info and archive:

Current Thread