xsl-list
[Top] [All Lists]

Re: Apply Template based on position or count

2004-02-06 14:59:19
Yes, I noticed that I was also getting an extra step in that for-each.  I
couldn't figure out why so I just check for the existance of the node like
this:
              <xsl:for-each select="xml/rs:data/z:row">
                    <xsl:if test="position() mod 2 = 1">
                    <xsl:if test=".">

By the way, I did get this to work:
    <xsl:apply-templates select="following::node()[1]" mode="session_info"/>

Which I guess is the same as your:
    <xsl:value-of select="following-sibling::*[1]/@prop"/>

Karl



----- Original Message -----
From: "Josh Canfield" <Josh(_dot_)Canfield(_at_)plumtree(_dot_)com>
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Friday, February 06, 2004 2:10 PM
Subject: RE: [xsl] Apply Template based on position or count


position() + 1 will return a number, in this case 4. I think what he wants
the next node, which can be retrieved using:

following-sibling::*[1]

I have to agree that the <tr/> looks like a typo, why do you want an empty
tr element? Changing the match to AAA/BBB, and using the supplied XML I get
the following output:

<table width="200" border
   <tr></tr>
   <td valign="top"></td>
   <td valign="top"></td>
   <td valign="top"></td>
   <td valign="top"></td>
   <tr></tr>
   <tr></tr>
   <td valign="top"></td>
   <td valign="top"></td>
   <td valign="top"></td>
   <td valign="top"></td>
   <tr></tr>
   <tr></tr>
   <td valign="top"></td>
   <td valign="top"></td>
</table>

Try this template:

<xsl:template match="/">
<table width="200" border="1">
<xsl:for-each select="AAA/BBB">
  <xsl:if test="position() mod 2 = 1">
    <tr>
      <td valign="top"><xsl:value-of select="@prop"/></td>
      <td valign="top"><xsl:value-of
select="following-sibling::*[1]/@prop"/></td>
    </tr>
  </xsl:if>
</xsl:for-each>
</table>
</xsl:template>

output is:

<table width="200" border="1">
   <tr>
      <td valign="top">1</td>
      <td valign="top">2</td>
   </tr>
   <tr>
      <td valign="top">3</td>
      <td valign="top">4</td>
   </tr>
   <tr>
      <td valign="top">5</td>
      <td valign="top">6</td>
   </tr>
</table>

Enjoy,
Josh

-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com]On Behalf Of 
Kenny Akridge
Sent: Friday, February 06, 2004 6:41 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Apply Template based on position or count


<xsl:value-of select="position() + 1"/>

And my tr/ wasn't a type.  This is how I handle two columns.  I'm still
not exactly sure what you are trying to do, but the code above will
reference all BBB with even numbered prop values... mod 2 != 1
essentially.


-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf Of 
Karl J.
Stubsjoen
Sent: Thursday, February 05, 2004 11:59 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] Apply Template based on position or count

I think there might also be a <tr/> typo.
But now let me ask, given the sample XML:

<AAA>
  <BBB prop="1"></BBB>
  <BBB prop="2"></BBB>
  <BBB prop="3"></BBB>
  <BBB prop="4"></BBB>
  <BBB prop="5"></BBB>
</AAA>

Within a for-each... if I have the following for each loop and if
construct,
and for sake of discussion we are currently at:
BBB prop 3:

  <xsl:for-each select="//BBB">
        <xsl:if test="position() mod 2 = 1">

                [[[ HOW TO IMPLICITLY REFERENCE "BBB prop 4" ]]]

        </xsl:if>
  </xsl:for-each>


This would clean up your suggestion (i think - if this works).

Karl



----- Original Message -----
From: "Kenny Akridge" <kakridge(_at_)bellsouth(_dot_)net>
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Thursday, February 05, 2004 8:21 PM
Subject: RE: [xsl] Apply Template based on position or count


Sorry, missed a closing <if> there.

-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf 
Of Kenny
Akridge
Sent: Thursday, February 05, 2004 10:03 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Apply Template based on position or count

I'm not exactly certain what you are trying to do, but if you want two
columns, you'll need to do something like this:

 <table width="200" border="1">
  <xsl:for-each select="//z:row">
        <xsl:if test="position() mod 2 = 1">
     <tr/>
         <td valign="top"><xsl:apply-templates select="."
mode="session_info"/></td>
            <td valign="top"><xsl:apply-templates select="."
mode="session_info"/></td>
        </xsl:if>
        <xsl:if test="position() mod 2 = 0">

         <td valign="top"><xsl:apply-templates select="."
mode="session_info"/></td>
            <td valign="top"><xsl:apply-templates select="."
mode="session_info"/></td>
<tr/>
        </xsl:for-each>

    </table>

Notic:  the second select is "."  How do i get the next z:row?

The next z:row will be given to you in the for-each loop.
Maybe some sample xml and a simple drawing will help.

Karl


----- Original Message -----
From: "Karl J. Stubsjoen" <karl(_at_)azprogolf(_dot_)com>
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Thursday, February 05, 2004 6:25 PM
Subject: [xsl] Apply Template based on position or count


Hi there,

I have a node fragment with (lets say) 20 items in it.  I would like
to
apply a template rule which matches the first 10 nodes, and then
later
the
2nd ten.  Ultimately I am trying to create two columns in my html
output.
Now, lets say there are 25 items, so the first set would contain 13
items
and the 2nd set 12 items - but somehow in XSLT no that we need to
match 13
rows on the left so add 1 more row.

So, can you apply-templates and specify some sort of [1-10] match?

Karl



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




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


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


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




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


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


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




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