xsl-list
[Top] [All Lists]

RE: increment value - philosophy

2004-02-08 14:28:45
Hello Ken,

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. By the rule, the top level menu items can't be empty but all
top level menu items don't necessarily have sub menu items always. Thanks in
advance. Have a nice day.

Anoop

-----Original Message-----
From: G. Ken Holman [mailto:gkholman(_at_)CraneSoftwrights(_dot_)com]
Sent: Thursday, January 22, 2004 3:07 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] increment value - philosophy


At 2004-01-22 14:17 -0500, Govil, Anoop (Contractor) wrote:
Thanks for your note. That put things into the right perspective.

I don't think it did at all.

Yes, you are true that we are trying to get too much out of XSL.

FALSE!!! You are NOT trying to get too much out of XSLT ... you are looking 
at XSLT incorrectly.  XSLT is *amazingly* powerful with hierarchies.  When 
you are trying to use a hammer to affix a screw you are going to have 
problems and while the eventual solution may work it is the *wrong* way to 
do things!

My problem is due to two nested loops and the position() value is 
different in these two loops.

Your problem is trying to use position() ... I said there are many inherent 
facilities in XSLT for working with hierarchies.

I have started looking into recursive template implementation to find a
solution to my problem there.

Because you are a programmer and you are trying to solve a simple problem 
with ugly and difficult-to-maintain code.

If you have any sample for that, it will definitely be helpful to start
with.

I would NOT try to solve the problem using recursion.

Below is your problem as you stated it, and two very simple one-line 
solutions using XSLT that don't use position().  Both techniques work 
regardless of how the node is being processed (the position() function 
*depends* on how the node is being processed).  I hope one of them suits 
your needs.  And I hope you see you don't have to use recursion for this 
particular requirement.

Handing you a solution doesn't help teach you about the language ... I hope 
you can see that I've tried to help you understand why your approaches were 
totally wrong and wasting a tremendous amount of time by trying to solve a 
hierarchical problem with a programmer's classical approach.

I sincerely hope this helps this time around.

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

p.s. can I interest you or your colleagues in our publicly-subscribed 
hands-on training near DC in March? :{)}

At 2004-01-22 09:33 -0500, Govil, Anoop (Contractor) wrote:
I have the following XML and want to implement such that I can have a
linear
counter increment with each iteration of the parent for-each loop and also
the nested for-each loop.
<?xml version="1.0"?>
<MENU>
    <MenuItems>
        <MenuLabel>Menu 1</MenuLabel>
        <SubMenuItems>
            <SubMenuLabel>Sub Menu 1</SubMenuLabel>
        </SubMenuItems>
        <SubMenuItems>
            <SubMenuLabel>Sub Menu 11</SubMenuLabel>
        </SubMenuItems>
    </MenuItems>
    <MenuItems>
        <MenuLabel>Menu 2</MenuLabel>
        <SubMenuItems>
            <SubMenuLabel>Sub Menu 2</SubMenuLabel>
        </SubMenuItems>
    </MenuItems>
</MENU>
e.g. The counter should return as:
1 for Menu 1
2 for SubMenu 1
3 for SubMenu 11
4 for Menu 2
5 for SubMenu 2



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

T:\ftemp>type anoop.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">
     <xsl:value-of select="1 + count(preceding::MenuLabel) +
                           count(preceding::SubMenuLabel)"/>
     <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">
     <xsl:number level="any" count="MenuLabel | SubMenuLabel"/>
     <xsl:value-of select="concat(' ',.)"/><xsl:text>
</xsl:text>
   </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>saxon anoop.xml anoop.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: sign up for one or both soon to reserve your seat!
Each week:  Monday-Wednesday: XSLT/XPath;  Thursday-Friday: XSL-FO
Washington, DC: 2004-03-15           San Francisco, CA: 2004-03-22
Hong Kong, China: 2004-05-17           Bremen, Germany: 2004-05-24
World-wide on-site corporate, government & user group XML training

G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
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



<Prev in Thread] Current Thread [Next in Thread>