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

XSLT question ancestor-or-self::


I am trying some XSLT code. O don't get to seem the ancestor-or-self:: working. Can anyone tell me why this is not working. I also have a similar XSLT in Umbraco but the the param currentPage is filled as a param and I am filling it with a XML file.

<?xml version="1.0" encoding="UTF-8"?>

  <xsl:output method="xml" omit-xml-declaration="yes" />

  <!--<xsl:param name="currentPage"/> -->
  <!-- Input the documenttype you want here -->
  <xsl:variable name="level" select="1"/>

  <xsl:template match="/">    
    <xsl:variable name="currentPage" select="/"/>

    <!-- The fun starts here -->
    <ul id="topnavigation">
      <xsl:for-each select="$currentPage/ancestor-or-self::node[@level=$level]/node[string(data [@alias='umbracoNaviHide']) != '1']">
          <a href="#">
            <xsl:value-of select="@nodeName"/>



XML file is  attached

Thanks in advance
  • 8
  • 6
1 Solution
Geert BormansInformation ArchitectCommented:
it seems to work for me
of course ancestor is looking up the tree,
if you make $currentPage select="/", there is not much looking up

- I suggest that you pass in the id of the node as a parameter, not the node itself
<xsl:param name="cur-page-id"/>

I checked whether it was working with this
        <xsl:variable name="currentPage" select="//node[@id='1050']"/>
and it does
passing in the id it would be something like this
        <xsl:variable name="currentPage" select="//node[@id=$cur-page-id]"/>

If your XML becomes really big, I would use a key instead of the //node

Please note that you are looking up the tree, using ancestor-or-self::node, but from there you go back down,
so you are not necessarily building a full navigation trace
you are listing all the level 2 nodes that are child of the ancestor level 1 node (level being a parameter as well)
and that are not indicated as "hidden"
<xsl:for-each select="$currentPage/ancestor-or-self::node[@level=$level]/node[not(data[@alias='umbracoNaviHide'] = '1')]">

so if that is what you want to do, it seems to work just fine, given the parameter binding is correct
that can be assured by passing the id instead of the node itself

vriendelijke groeten,
waaromiknietAuthor Commented:
Hello Geert,

Thanks for the input. I still don't exactly get why

 <xsl:variable name="currentPage" select="/"/>

Is not working for me. When writing the param to screen as is comes from umbraco gives me the same XML als my attached file. So it seems to be working from Umbraco but not with a xml file.
waaromiknietAuthor Commented:

I used descendant-or-self::node
Na dno it is working for me. Thanks.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Geert BormansInformation ArchitectCommented:
well, if you really want it to work from top down,
you need descendant-or-self::*
short form would be "$currentPage//*"
by the way

Could have spotted that; I really thought you needed a bottom up approach
waaromiknietAuthor Commented:
With bottom up I first need to select a node lower in the tree right?
Why would I choose a bottom up approach? Or am I looking at it in a wrong way?
Geert BormansInformation ArchitectCommented:
I think I misjudged the goal of your question.
Since you were going bottom up, I guessed you were building a breadcrump.
That is when you would use a bottom up, to list the navigation path up to this particular point.

Now I see that you are just making a navigation list, from the point you are at, deeper down the hierarchy.
If that is the case, you should navigate downwards

I was put on the wrong foot by your usage of the ancestor axis
waaromiknietAuthor Commented:
If you start in the first template:

  <xsl:template match="/">    

You always start at the top right? If you want to start from the bottom yopu have to select the last node? and then work your way up?
Geert BormansInformation ArchitectCommented:
logically XSLT should always start working form the top, that is a natural way of processing a document

Most of the stylesheets start with a <xsl:template match="/">    (in XSLT1 they actually all do)
and so start from top of document

I have a feeling that I really confused you.

I thought you needed a breadcrump, given the context of a specific node.
That is something you would need if you were making a site from this one page, only showing one specific node from the XML in a page
If that were the case, you would have a starting point, right in the middle of your document, making all the implicit navigation explicit
waaromiknietAuthor Commented:
Thanks for your help
Geert BormansInformation ArchitectCommented:
Geert BormansInformation ArchitectCommented:
I think you have been assisted enough, no?
Geert BormansInformation ArchitectCommented:
I think you have been assisted enough, no?
Geert BormansInformation ArchitectCommented:
The original question was asking
"don't get to seem the ancestor-or-self:: working"

http:#a28430008 explains the ancestor-or-self behaviour
from that explanation the questioner realises he needed a different approach
that particular exposee seems valuable enough for a PAQ
so I suggest excepting http:#a28430008 as the answer

waaromiknietAuthor Commented:

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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