Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Thomas Kahn 602 posts 506 karma points
    Oct 16, 2009 @ 13:35
    Thomas Kahn
    1

    Which is the most effective way of traversing from root?

    Which is more effective:

    $currentPage/ancestor-or-self::root//node[INSERT CRITERIA HERE]

    or

    umbraco.library:GetXmlAll()/descendant-or-self::node[INSERT CRITERIA HERE]

    ?

    What I want to do is get all documents of a certain doctype that exist on the site and loop through them using for-each. To my eye it looks like they generate the same result, but one makes use of the Umbraco API?

    I'm interested in all info related to this since it's a very common thing (at least on my sites) and I want to make sure I do it the most effective way. Im guessing traversing the entire XML-structure can be rather costly, at least on a big site.

    Regards,
    Thomas Kahn

  • Comment author was deleted

    Oct 16, 2009 @ 13:46

    I would prefer the first option (so not using umbraco.library).But I doubt that there is a huge difference in performance between the 2

  • Thomas Kahn 602 posts 506 karma points
    Oct 16, 2009 @ 14:00
    Thomas Kahn
    0

    Me too - doing it the pure XSLT-way seems the best way to go.

    /Thomas

  • Ron Brouwer 273 posts 768 karma points
    Oct 16, 2009 @ 14:37
    Ron Brouwer
    3

    The default position of the iterator is the root.

    So the best way to start from there is:

    //node[INSERT CRITERIA HERE]

    If you have the iterator positioned somewhere else:

    ///node[INSERT CRITERIA HERE]

    The first / means start from root.

    Ron

  • dandrayne 1138 posts 2262 karma points
    Oct 16, 2009 @ 15:03
    dandrayne
    0

    @Ron - nice tip, thanks.

  • Thomas Kahn 602 posts 506 karma points
    Oct 16, 2009 @ 20:21
    Thomas Kahn
    0

    Excellent! I didn't know that! Thanks Ron!

    /Thomas K

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Oct 16, 2009 @ 23:35
    Chriztian Steinmeier
    2

    @Ron: Adding that extra slash won't cut it, I'm afraid. 

    Because of the way your data is handed to you in umbraco (by way of a parameter), I always add a variable after the currentPage one to grab the root:

    <xsl:variable name="root" select="$currentPage/ancestor-or-self::root" />

    That way, I can always grab stuff "from the root" by using something like:  

    <xsl:apply-templates select="$root//node[@nodeTypeAlias = 'MySuperDocumentType']" />

     

    /Chriztian

  • Ron Brouwer 273 posts 768 karma points
    Oct 17, 2009 @ 00:20
    Ron Brouwer
    0

    Hi Chriztian,

    I will try it later on however I don't know what you are trying to achief with the $currentPage parameter.
    You simply don't need that parameter at all. The only time you need to use that is when you need specific information about the current page.

    What the $currentPage parameter does is give you a iterator with points to the current page no more no less.

    Its an interesting issue tho.

    Ron

  • Ron Brouwer 273 posts 768 karma points
    Oct 17, 2009 @ 00:28
    Ron Brouwer
    0

    I gues that you are think the only way to reach your data is by using the $currentPage which is a commen misunderstanding.
    Correct me if I'me wrong

    Ron

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Oct 17, 2009 @ 01:47
    Chriztian Steinmeier
    0

    Hi Ron,

    We should probably take this discussion elsewhere, but in umbraco $currentPage is the way you get the site XML - the XML your XSLT gets (in the standard XML/XSLT way) is very light - amounts to a root element named "macro" and its associated parameters (if any). Your answer suggests to me you're not aware of this (?).

  • Ron Brouwer 273 posts 768 karma points
    Oct 17, 2009 @ 11:27
    Ron Brouwer
    0

    I'me wrong you are right ;)

    I was confused with some other senario.
    However, it would by nice to have the whole xml as default data tho.

    maybe for the next version.

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Oct 17, 2009 @ 14:57
    Sebastiaan Janssen
    0

    That's great, I had no idea the "::root" operator existed.

    For future reference, I'd like to point out something that you would run into when running multiple sites from one umbraco instance.

    You would have a content structure that looks like this:

    Content
     - sitename1.com
      -- Home
     - sitename2.org
      -- Home

    By using "::root", you go to the absolute root, which means that you start looking from Umbraco's default "Content" node for your criteria.

    <xsl:variable name="absoluteRoot" select="$currentPage/ancestor-or-self::root" />

    However, if you want to only find pages that are in your current site (let's say we're on sitename2.org), you will need to use "::node":

    <xsl:variable name="currentSiteRoot" select="$currentPage/ancestor-or-self::node" />

     

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Oct 17, 2009 @ 21:31
    Chriztian Steinmeier
    0

    Actually, the colons belong to the anscestor-or-self:: part - root is what umbraco decided to name the root element of the XML. So it's not a "::root" operator, but it's the XPath way of asking for elements named "root" that are either the current one, or one of its ancestors (parent, parent's parent and so on). Double colons are needed to distinguish from namespace prefixes which use a single colon. 

    I'm new to umbraco so I'd definitely run into said situation with multiple sites one day, so thanks for the heads-up :-)

    However, you will need to add some criteria to the currentSiteRoot variable, because a select statement will return all the elements matching (and there's probably more than one node element in the ancestor chain for many pages). The only reason the "absoluteRoot" variable works is because we know that there's only one element named root. So we'll need to do something like this instead:

    <xsl:variable name="currentSiteRoot" select="$currentPage/ancestor-or-self::node[@nodeName = 'sitename2.org']" />

    or if we already assigned the absoluteRoot variable and we know the site roots are children thereof:

    <xsl:variable name="currentSiteRoot" select="$absoluteRoot/node[@nodeName = 'sitename2.org']" />

     

    /Chriztian

     

     

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Oct 18, 2009 @ 10:50
    Sebastiaan Janssen
    0

    Thanks Chriztian, that actually made everything make much more sense! I love XSLT, but I don't understand some of it yet, obviously!

    However, if you're going to travel upwards from the $currentPage to find all the parent "node" element and upwards to find another "node" element, then you'll never end up at the "root" element, because it is not called "node". So in the end you'll still stay within the tree for the current website.

    So, luckily, you won't have to know the specific site name. This comes in very handy because you can keep your XSLT file generic for all of your sites.. they just operate on the nodes within the site you're visiting at that moment.

Please Sign in or register to post replies

Write your reply to:

Draft