• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3305
  • Last Modified:

XSLT Position of Ancestor

Hello
I need to get the position of the ancestor when i am in a grand-child.
Say i am in <item2> within the <submenu>
I need to go a find the position of its <item1>
How do i do this?

<themenu>
      <item>
            <title>Home</title>
            <link>www.xtra.co.nz</link>
      </item>
      <item1>
            <title>Joana</title>
            <link>www.xtra.co.nz</link>
      </item1>
      <item1>
            <title>Craig</title>
            <link>www.xtra.co.nz</link>
      </item>
      <item1>
            <title subNo="Yes">Jim</title>
            <link>www.xtra.co.nz</link>
            <submenu>
                  <item2>
                        <title>Mic</title>
                        <link>www.xtra.co.nz</link>
                  </item2>
                  <item2>
                        <title>Grant</title>
                        <link>www.xtra.co.nz</link>
                  </item2>
                  <item2>
                        <title>Dave</title>
                        <link>www.xtra.co.nz</link>
                  </item2>
            </submenu>
      </item1>

<themenu>
0
Joana
Asked:
Joana
1 Solution
 
dualsoulCommented:
hm...do you mean count item1, if you are in context of item2?

you can do it for instance this way: count(ancestor::item1/preceding-sibling::item1)

check this XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
      
      <xsl:template match="/">
            <xsl:for-each select="//item2">
                  Item2=<xsl:value-of select="." /><br/>
                  Ancestor Position is <xsl:value-of select="count(ancestor::item1/preceding-sibling::item1)" />
            </xsl:for-each>
      </xsl:template>
</xsl:stylesheet>
0
 
uwebeCommented:
well done dualsoul

Gasper
0
 
JoanaAuthor Commented:
Sorry for taking so long, forgot i hadn't changed my email address fo new job.  Thanks heaps for the answer it was just what i was looking for.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now