Re: [xsl] Getting the value of ancestor node in a for-each loop

Subject: Re: [xsl] Getting the value of ancestor node in a for-each loop
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Wed, 12 Nov 2003 02:37:24 -0700
If this is your source...

<get-MaxDirList>
 <get-MaxDirList-recordrow>
  <get-MaxDirList-cstrx>1</get-MaxDirList-cstrx>
  <get-MaxDirListz>
   <get-MaxDirListz-recordrow>
    <get-MaxDirListz-z>0</get-MaxDirListz-z>
   </get-MaxDirListz-recordrow>
   <get-MaxDirListz-recordrow>
    <get-MaxDirListz-z>1</get-MaxDirListz-z>
   </get-MaxDirListz-recordrow>
  </get-MaxDirListz>
 </get-MaxDirList-recordrow>
 <get-MaxDirList-recordrow>
  <get-MaxDirList-cstrx>1</get-MaxDirList-cstrx>
  <get-MaxDirListz>
   <get-MaxDirListz-recordrow>
    <get-MaxDirListz-z>0</get-MaxDirListz-z>
   </get-MaxDirListz-recordrow>
   <get-MaxDirListz-recordrow>
    <get-MaxDirListz-z>1</get-MaxDirListz-z>
   </get-MaxDirListz-recordrow>
  </get-MaxDirListz>
 </get-MaxDirList-recordrow>
  </get-MaxDirList>

And this is your template...

<?xml version="1.0"?>
<xsl:stylesheet
 version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns="http://www.w3.org/TR/REC-html40";>

 <xsl:template match="/">
  <xsl:for-each select="get-MaxDirList/get-MaxDirList-recordrow">
   <xsl:for-each select="get-MaxDirListz/get-MaxDirListz-recordrow">
    <xsl:value-of select="../preceding-sibling::get-MaxDirList-cstrx"/>
   </xsl:for-each>
  </xsl:for-each>
 </xsl:template>

</xsl:stylesheet>

You will get the value of the get-MaxDirList-cstrx as desired...

Keep in mind the XML you sent was not valid and the steps you described for
your for each loop would have returned no nodes in the first place.  I would
check your code and make sure you are referencing things correctly.  Also,
if you only want the value of get-MaxDirList-cstrx printed once within your
loop you need to wrap the xsl:value as so...
<xsl:if test="position() = 1">
    <xsl:value-of select="../preceding-sibling::get-MaxDirList-cstrx"/>
</xsl:if>

but be careful...  position() is based on the current context which in this
case is the node set containing all the get-MaxDirListz-recordrow children
of the previous context.  If your context changes the position() count will
change accordingly.

Hope this helps!

Best of luck...

M.




----- Original Message ----- 
From: "asim" <qazi@xxxxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Wednesday, November 12, 2003 1:49 AM
Subject: [xsl] Getting the value of ancestor node in a for-each loop


> Hi
> This is my XML Packet
>
> <get-MaxDirList>
>  <get-MaxDirList-recordrow>
>   <get-MaxDirList-cstrx>1</get-MaxDirList-cstrx>
>   <get-MaxDirListz>
>    <get-MaxDirListz-recordrow>
>     <get-MaxDirListz-z>0</get-MaxDirListz-z>
>    </get-MaxDirListz-recordrow>
>    <get-MaxDirListz-recordrow>
>     <get-MaxDirListz-z>1</get-MaxDirListz-z>
>    </get-MaxDirListz-recordrow>
>   <get-MaxDirListz>
>  </get-MaxDirList-recordrow>
>  <get-MaxDirList-recordrow>
>   <get-MaxDirList-cstrx>1</get-MaxDirList-cstrx>
>   <get-MaxDirListz>
>    <get-MaxDirListz-recordrow>
>     <get-MaxDirListz-z>0</get-MaxDirListz-z>
>    </get-MaxDirListz-recordrow>
>    <get-MaxDirListz-recordrow>
>     <get-MaxDirListz-z>1</get-MaxDirListz-z>
>    </get-MaxDirListz-recordrow>
>   <get-MaxDirListz>
>  </get-MaxDirList-recordrow>
>   </get-MaxDirList>
>
> I have two For-Each Loops nested in one another.
>
> The Outer Loop is looping on 'get-MaxDirList/get-MaxDirList-recordrow'
> and the internal loop i m looping on
> 'get-MaxDirListz-recordrow/get-MaxDirListz-z'
> now while in the internal loop i want to Print the value of
> 'get-MaxDirList-cstrx' node
> but i m unable to get it ..
>
> plz help me find solution to this problem.
>
> Qazi Asim
>
>
>  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