RE: [xsl] How do I process a result-tree fragment?

Subject: RE: [xsl] How do I process a result-tree fragment?
From: "Andrew Welch" <awelch@xxxxxxxxxxxxxxx>
Date: Thu, 26 Sep 2002 11:18:28 +0100
Hi Greg,

Quite an interesting problem... What you can do here is include a
partial identity transform to copy through the RTF, with a modified
text() matching template that deals with replacing the apostophies.

You need to test if the text you are processing contains an apos, if it
does, call a recursive template that escapes it.  This is pretty much
where you were up to.

<xsl:variable name="apos">'</xsl:variable>

<xsl:template match="text()">
  <xsl:choose>
    <xsl:when test="contains(.,$apos)">
      <xsl:call-template name="escapeApos">
        <xsl:with-param name="string" select="."/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="."/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="escapeApos">
  <xsl:param name="string" select="''"/>
  <xsl:choose>
  <xsl:when test="contains($string,$apos)">
    <xsl:value-of select="substring-before($string, $apos)"/>
    <xsl:text>\'</xsl:text>
    <xsl:call-template name="escapeApos">
      <xsl:with-param name="string"
select="substring-after($string,$apos)"/>
    </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="$string"/>
  </xsl:otherwise>
  </xsl:choose>
</xsl:template> 


Now, to deal with RTF's, you need to perform an identity transform on
the elements and attributes (basically copy them through unchanged), but
apply the text() matching template you already have.

So, first make your RTF a node-set:

<xsl:variable name="MyNodeSet" select="exsl:node-set($my-rtf)"/>

Then when you want it copied through to your output, apply-templates on
the node-set, using a mode to do the identity transform:

<xsl:for-each select="$MyNodeSet">
  <xsl:apply-templates mode="RTF"/>
</xsl:for-each>

Finally, you need the moded template that copies through the elements
and attributes.  Note how it doesnt match text() like the normal
identity transform templates - we want the text() template you already
have to deal with that. 
 
<xsl:template match="@*|*" mode="RTF">
  <xsl:copy>
    <xsl:apply-templates mode="RTF"/>
  </xsl:copy>
</xsl:template>


cheers
andrew


> -----Original Message-----
> From: Greg Bender [mailto:greg@xxxxxxxxxxxxxxxxxx]
> Sent: 25 September 2002 23:16
> To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] How do I process a result-tree fragment?
> 
> 
> Currently I am using the following procedure (acquired from
> http://www.jenitennison.com) to escape apostrophe characters 
> (I need to do
> this in order to place the resulting information in javascript):
> 
> <xsl:template name="escape-apos">
>    <xsl:param name="string" />
>    <!-- create an $apos variable to make it easier to refer to -->
>    <xsl:variable name="apos" select='"&apos;"' />
>    <xsl:choose>
>       <!-- if the string contains an apostrophe... -->
>       <xsl:when test='contains($string, $apos)'>
>          <!-- ... give the value before the apostrophe... -->
>          <xsl:value-of select="substring-before($string, $apos)" />
>          <!-- ... the escaped apostrophe ... -->
>          <xsl:text>\'</xsl:text>
>          <!-- ... and the result of applying the template to 
> the string
> after the apostrophe -->
>          <xsl:call-template name="escape-apos">
>             <xsl:with-param name="string" 
> select="substring-after($string,
> $apos)" />
>          </xsl:call-template>
>       </xsl:when>
>       <!-- otherwise... -->
>       <xsl:otherwise>
>          <!-- ... just give the value of the string -->
>          <xsl:value-of select="$string" />
>       </xsl:otherwise>
>    </xsl:choose>
> </xsl:template>
> 
> 
> This method works great when I pass it a string. However, 
> when I pass it a
> result-tree fragment, I loose the mark-up language. For example:
> 
> If I pass "<div><b>these word's are bold.</b></div>"
> 
> I get "These word\'s are bold."
> 
> When what I want is "<div><b>these word\'s are bold.</b></div>"
> 
> 
> I've tried replacing the 'xsl:value-of' commands with 'xsl:copy-of'
> commands. This solution works if the result-tree fragment 
> doesn't contain
> any apostrophes (it just passes the variable through). 
> However, when the
> result-tree fragment contains apostrophes, I loose the 
> result-tree fragment.
> 
> I think the contains(), substring-before(), and/or substring-after()
> statements are converting the result-tree fragment to a string.
> 
> Any help is appreciated.
> 
> Regards,
> 
> Greg Bender
> 
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 
> 
> 
> ---
> Incoming mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.391 / Virus Database: 222 - Release Date: 19/09/2002
>  
> 

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.391 / Virus Database: 222 - Release Date: 19/09/2002
 

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


Current Thread