[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Applying Templates to a Substring


Subject: Re: [xsl] Applying Templates to a Substring
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 22 Oct 2004 00:32:44 +0300

At 2004-10-21 14:20 -0400, Joe Heidenreich wrote:
If I have a <li> node like this:

<ol>
     <li>1) This is the <b>first</b> item.</li>
     <li>2) This is the 2<sup>nd</sup> item.</li>
</ol>

and I want the output to be:

<ol>
     <li>This is the <b>first</b> item.</li>
     <li>This is the 2<sup>nd</sup> item.</li>
</ol>

However I'm not aware of how you can apply-templates to a substring.

You cannot.


The only way I can get the proper substring I need that I know of is:

<xsl:value-of select="substring-after(.,')')"/>

But you didn't think to do it at the text node level.


Attached below are two examples, joe1.xsl and joe2.xsl ... use joe1.xsl if the first parenthesis pattern in the list item is never going to be, itself, in markup (typical). Use the more elaborate joe2.xsl if the first parenthesis pattern in the list item can be at any level of the subtree under the <li> (which it shouldn't be from your sample, but this technique I've used is useful for things like drop-initial-caps that might be in markup; I've added border cases to the joe.xml sample).

I hope this helps.

.................. Ken

T:\ftemp>type joe.xml
<ol>
     <li>1) This is the <b>first</b> item.</li>
     <li>2) This is the 2<sup>nd</sup> item.</li>
     <li>1) This is the <b>1) first</b>1) item.</li>
     <li>2) This is the 2<sup>nd</sup> item.</li>
     <li><b>3) </b>Third</li>
</ol>
T:\ftemp>type joe1.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:template match="li//text()[1]">
  <xsl:choose>
    <xsl:when test="contains(.,') ')">
      <xsl:value-of select="substring-after(.,') ')"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="."/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>saxon joe.xml joe1.xsl
<?xml version="1.0" encoding="utf-8"?><ol>
     <li>This is the <b>first</b> item.</li>
     <li>This is the 2<sup>nd</sup> item.</li>
     <li>This is the <b>first</b>1) item.</li>
     <li>This is the 2<sup>nd</sup> item.</li>
     <li><b/>Third</li>
</ol>
T:\ftemp>type joe2.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:template match="li//text()[1]">
  <xsl:choose>
    <xsl:when test="ancestor-or-self::node()[not(descendant-or-self::li)]/
                    preceding-sibling::node() or
                    not(contains(.,') '))">
      <xsl:value-of select="."/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="substring-after(.,') ')"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>saxon joe.xml joe2.xsl
<?xml version="1.0" encoding="utf-8"?><ol>
     <li>This is the <b>first</b> item.</li>
     <li>This is the 2<sup>nd</sup> item.</li>
     <li>This is the <b>1) first</b>1) item.</li>
     <li>This is the 2<sup>nd</sup> item.</li>
     <li><b/>Third</li>
</ol>
T:\ftemp>


-- World-wide on-site corporate, govt. & user group XML/XSL training. G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Breast Cancer Awareness http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal


Current Thread