Re: [xsl] is there a more elegant way to change page references?

Subject: Re: [xsl] is there a more elegant way to change page references?
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 28 May 2012 17:57:35 +0200
At 2012-05-28 11:42 -0400, Terry Ofner wrote:
I have an xml document with multiple page
references inside <ref> elements. I have to add
2 to each page reference that follows the text TG:

<ref><b>TG: </b>pp. 19, 20, 21, 29, 32, 40, 43,
44, 46, 56, 57, 58, 60, 61, 66, 67&#8211;68</ref>

As you can see above, the list can contain one
or more series, as in 67-68 (with an en-dash).

Here is my series of templates, one that captures...

I think this is best handled with <xsl:analyze-string>. I hope the example below helps.

. . . . . . . . . Ken

t:\ftemp>type terry.xml
<?xml version="1.0" encoding="UTF-8"?>
<ref><b>TG: </b>pp. 19, 20, 21, 29, 32, 40, 43, 44, 46, 56, 57, 58, 60, 61,
66,
67&#8211;68</ref>

t:\ftemp>xslt2 terry.xml terry.xsl
<?xml version="1.0" encoding="UTF-8"?><ref><b>TG:
</b>pp. 21, 22, 23, 31, 34, 42
, 45, 46, 48, 58, 59, 60, 62, 63, 68, 69GGt70</ref>
t:\ftemp>type terry.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="2.0">

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

<!--the following assumes that any given string of page numbers is not
    interrupted-->
<xsl:template match="ref/text()">
  <xsl:analyze-string select="." regex="(\d+)(&#8211;(\d+))?">
    <xsl:matching-substring>
      <xsl:value-of select="number(regex-group(1))+2"/>
      <xsl:if test="regex-group(2)">
        <xsl:text/>&#8211;<xsl:value-of select="number(regex-group(3))+2"/>
      </xsl:if>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
      <xsl:value-of select="."/>
    </xsl:non-matching-substring>
  </xsl:analyze-string>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>



--
Public XSLT, XSL-FO, UBL and code list classes in Europe -- Oct 2012
Contact us for world-wide XML consulting and instructor-led training
Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm
Crane Softwrights Ltd.            http://www.CraneSoftwrights.com/s/
G. Ken Holman                   mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal

Current Thread