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

Re: [xsl] grouping based on string but with child nodes


Subject: Re: [xsl] grouping based on string but with child nodes
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 13 Oct 2008 11:40:49 -0400

At 2008-10-13 16:17 +0100, Andrew Welch wrote:
Yes, use 2 passes, it will help you diagnose problems in the 2nd pass
much more easily.

The first pass is an idenity transform with:

<xsl:template match="title">
  <xsl:for-each select="tokenize(., '||')>
    <title><xsl:value-of select="."/></title>

I felt that wasn't an appropriate approach because it would lose the rich emphasis markup in the stringification done by tokenize().


At 2008-10-13 16:11 +0100, James Cummings wrote:
I have some old EAD XML that I'm trying to convert to a slightly
better form and what I want to take is something like this:
...
So desired output (in a variable
...
I've been assuming that this is a two-pass problem where I have to
replace the '||' with some empty XML element  and then group-by that
element?

Sounds good to me! I tried a number of other thoughts but I think yours is the best approach.


If that is the case what is the best way to replace these
for grouping?

<xsl:analyze-string/>


Or is it possible to do this all-in-one?

Absolutely.


XSLT2 solution obviously desired to avoid too much recursion. :-)

Yep!


I hope the code below helps.

. . . . . . . . Ken

T:\ftemp>type james.xml
<?xml version="1.0" encoding="utf-8"?>
<unittitle>
    <title>Calendarial rules. Prognostics. || Alcuin,
        <emph render="italic">Quaestiones in Genesim</emph>. ||
        Glossaries. || <emph render="italic">Ps</emph>.-Cicero,
        <emph render="italic">Synonyma ad Lucium Veturium</emph>, with
added Latin-Old English
        glossaries extracted from the Grammar and Glossary of AElfric.
    </title>
    <!-- other elements -->
</unittitle>

T:\ftemp>xslt2 james.xml james.xsl con
<?xml version="1.0" encoding="UTF-8"?>
<unittitle>
    <title>Calendarial rules. Prognostics. </title>
   <title> Alcuin,
        <emph render="italic">Quaestiones in Genesim</emph>. </title>
   <title>
        Glossaries. </title>
   <title>
      <emph render="italic">Ps</emph>.-Cicero,
        <emph render="italic">Synonyma ad Lucium Veturium</emph>, with
added Latin-Old English
        glossaries extracted from the Grammar and Glossary of AElfric.
    </title>
    <!-- other elements -->
</unittitle>
T:\ftemp>type james.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:dummy="urn:x-Dummy"
                exclude-result-prefixes="dummy"
                version="2.0">

<xsl:output indent="yes"/>

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

<xsl:template match="title">
  <xsl:variable name="separated-titles" as="node()*">
    <xsl:apply-templates mode="separate-titles"/>
  </xsl:variable>
  <xsl:variable name="titles" as="element(title)*">
    <xsl:for-each-group select="$separated-titles"
                        group-starting-with="dummy:dummy">
      <title>
        <xsl:copy-of select="current-group()[not(self::dummy:dummy)]"/>
      </title>
    </xsl:for-each-group>
  </xsl:variable>
  <xsl:copy-of select="$titles"/>
</xsl:template>

<xsl:template match="*" mode="separate-titles">
  <xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="text()" mode="separate-titles">
  <xsl:analyze-string select="." regex="\|\|">
    <xsl:matching-substring>
      <dummy:dummy/>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
      <xsl:value-of select="."/>
    </xsl:non-matching-substring>
  </xsl:analyze-string>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>

--
Upcoming XSLT/XSL-FO hands-on courses:      Wellington, NZ 2009-01
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video sample lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg
Video course overview:  http://www.youtube.com/watch?v=VTiodiij6gE
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


Current Thread
Keywords
xml