This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Parsing copy-of result tree fragment


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:copy>
    <xsl:apply-templates select="node()|@*" mode="copying"/>
  </xsl:copy>
</xsl:template>

<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:call-template>
</xsl:template>

<xsl:template name="SubstringReplace">
   <xsl:param name="stringIn"/>
   <xsl:param name="substringIn"/>
   <xsl:param name="substringOut"/>
   <xsl:choose>
      <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:call-template>
      </xsl:when>
      <xsl:otherwise>
         <xsl:value-of select="$stringIn"/>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

   - Mike
____________________________________________________________________
Mike J. Brown, software engineer at            My XML/XSL resources: 
webb.net in Denver, Colorado, USA              http://skew.org/xml/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]