Re: [xsl] Using operators >> and <<

Subject: Re: [xsl] Using operators >> and <<
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 12 Jan 2009 20:16:26 -0500
At 2009-01-12 17:59 -0600, Ylvisaker, Steve wrote:
I need to take the following markup:
...
And turn it into:
It is easy enough to identify and format the <ulist/> based on the occurrence of the first <bullet/> but then isolating just the remaining contiguous bullets eludes me.

I approached it differently: finding the adjacent groups that are or are not made up entirely of bullets.


I have the feeling that I can use the << or >> operators to select all of the bullets until the node name changes in the document order.

I don't think so, because they'll look past the first of the groups.


However, I can only find very sketchy documentation regarding these constructs and when I try to use them I get syntax errors.

Remember you cannot have naked "<" characters in attribute specifications ... you need to escape them.


Any suggestions or pointers on where I could look for working examples (not shorthand) of how these constructs can be used or suggestions on a different approach would be very much appreciated.

How about a different approach, then ... I hope the code below helps. Note how I am calculating a value for every member of the population being grouped by adjacent equal values, and the value I'm calculating is either true or false for each member. Any value calculated for a member of the population can be used when grouping.


. . . . . . . Ken

t:\ftemp>type steve.xml
<steve>
<para>a para of data</para>
<bullet>first bullet data</bullet>
<bullet>second bullet data</bullet>
<bullet>third bullet data</bullet>
<para>more para data</para>
<bullet>another bullet list</bullet>
</steve>

t:\ftemp>call xslt2 steve.xml steve.xsl
<?xml version="1.0" encoding="UTF-8"?>
<para>a para of data</para>
<ulist>
   <listitem>
      <para>first bullet data</para>
   </listitem>
   <listitem>
      <para>second bullet data</para>
   </listitem>
   <listitem>
      <para>third bullet data</para>
   </listitem>
</ulist>
<para>more para data</para>
<ulist>
   <listitem>
      <para>another bullet list</para>
   </listitem>
</ulist>
t:\ftemp>type steve.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="steve">
  <!--find all adjacent groups that are or not made up of bullets-->
  <xsl:for-each-group select="*" group-adjacent="boolean(self::bullet)">
    <xsl:choose>
      <xsl:when test="self::bullet">
        <!--here is an adjacent group that is made up of bullets-->
        <ulist>
          <xsl:for-each select="current-group()">
            <listitem><para><xsl:apply-templates/></para></listitem>
          </xsl:for-each>
        </ulist>
      </xsl:when>
      <xsl:otherwise>
        <!--here is an adjacent group that is not made up of bullets-->
        <xsl:apply-templates select="current-group()"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each-group>
</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>

--
Upcoming XSLT/XSL-FO, UBL and code list hands-on training classes:
:  Sydney, AU 2009-01/02; Brussels, BE 2009-03; Prague, CZ 2009-03
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
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