RE: [xsl] increment value - philosophy

Subject: RE: [xsl] increment value - philosophy
From: "Govil, Anoop (Contractor)" <Anoop.Govil@xxxxxxxxxxxxxxxx>
Date: Mon, 9 Feb 2004 09:18:39 -0500
Hello Ken,

That works beautifully. Once again you are the man. Thanks for helping me
out.

Anoop

-----Original Message-----
From: G. Ken Holman [mailto:gkholman@xxxxxxxxxxxxxxxxxxxx]
Sent: Sunday, February 08, 2004 4:57 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] increment value - philosophy


At 2004-02-08 16:28 -0500, Govil, Anoop (Contractor) wrote:
>Thanks for your solution. I tested it in my application. I have a new issue
>with this solution, if one of the sub menu items is empty (i.e., the
>SubMenuLabel tag is empty), it still counts it and doesn't skip that if it
>is empty. I tried various things but still can't skip it. I will really
>appreciate if you think of how to fix that issue to skip a submenuitem node
>if it is empty.

Rather than think about what you are skipping, try to focus on what you are 
counting: you are counting those SubMenuLabel elements that are not 
empty.  When is an element not empty?  When it has node children.

So, only deal with SubMenuLabel[node()] elements.

Again, the idea when working with the hierarchy is to let XSLT do the heavy 
lifting by describing what it is you want done.

I hope this helps.

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

t:\ftemp>type snoop.xml
<?xml version="1.0"?>
<MENU>
      <MenuItems>
          <MenuLabel>Menu 1</MenuLabel>
          <SubMenuItems>
              <SubMenuLabel>Sub Menu 1</SubMenuLabel>
          </SubMenuItems>
          <SubMenuItems>
              <SubMenuLabel></SubMenuLabel>
          </SubMenuItems>
          <SubMenuItems>
              <SubMenuLabel>Sub Menu 11</SubMenuLabel>
          </SubMenuItems>
      </MenuItems>
      <MenuItems>
          <MenuLabel>Menu 2</MenuLabel>
          <SubMenuItems>
              <SubMenuLabel></SubMenuLabel>
          </SubMenuItems>
          <SubMenuItems>
              <SubMenuLabel>Sub Menu 2</SubMenuLabel>
          </SubMenuItems>
      </MenuItems>
</MENU>

t:\ftemp>type snoop.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                  version="1.0">

<xsl:output method="text"/>

<xsl:template match="/">
    <xsl:text>Method 1: using the count function:
</xsl:text>
    <xsl:for-each select="//MenuLabel | //SubMenuLabel[node()]">
      <xsl:value-of select="1 + count(preceding::MenuLabel) +
                            count(preceding::SubMenuLabel[node()])"/>
      <xsl:value-of select="concat(' ',.)"/><xsl:text>
</xsl:text>
    </xsl:for-each>
    <xsl:text>Method 2: using xsl:number:
</xsl:text>
    <xsl:for-each select="//MenuLabel | //SubMenuLabel[node()]">
      <xsl:number level="any" count="MenuLabel | SubMenuLabel[node()]"/>
      <xsl:value-of select="concat(' ',.)"/><xsl:text>
</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>


t:\ftemp>saxon snoop.xml snoop.xsl
Method 1: using the count function:
1 Menu 1
2 Sub Menu 1
3 Sub Menu 11
4 Menu 2
5 Sub Menu 2
Method 2: using xsl:number:
1 Menu 1
2 Sub Menu 1
3 Sub Menu 11
4 Menu 2
5 Sub Menu 2

t:\ftemp>


--
Public courses: upcoming world tour of hands-on XSL training events
Each week:    Monday-Wednesday: XSLT/XPath; Thursday-Friday: XSL-FO
Washington, DC: 2004-03-15            San Francisco, CA: 2004-03-22
Hong Kong: 2004-05-17    Germany: 2004-05-24    England: 2004-06-07
World-wide on-site corporate, government & user group XML 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


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

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


Current Thread