Copied to clipboard

Flag this post as spam?

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


  • Brad 94 posts 151 karma points
    Feb 01, 2010 @ 05:54
    Brad
    0

    Selecting nodes - problem

    I have a very simple site structure.


    The root node has 2 child node. One has the site navigation in it. The 2nd has a node called PanelItems that are to appear in a small box on the side of the site. PanelItems has the child nodes of Paneltem.

    My problem is I can't get my xslt to get the PanelItem values. I have written some xslt in Cooktop and tested it against the sites umbraco.config xml file. And this works perfectly.

    ( I have omited the standard XSLT guff around this and removed the HTML formatting for clarity.

    <xsl:template match="/">

    <xsl:for-each select=".//node">
            <xsl:value-of select="./data [@alias = 'panelText']"/>
    </xsl:for-each>

    </xsl:template>

    This works in Cooktop but not when I put it in a macro in umbraco.  

    Seems every xslt example I find for Umbraco is using CurrentPage but this macro does not need CurrentPage. It cares not what page it is on. This list needs to go on every page.

    What Umbraco specific thing am I missing here?

    Thanks

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Feb 01, 2010 @ 08:57
    Dirk De Grave
    1

    Brad,

    instead of using $currentPage, use:

    <xsl:variable name="myStartNode" select="umbraco.library:GetXmlNodeById(id)" />

    (id may be hardcoded, but could also pass this as a parameter from the macro that references the xslt)

     

    Hope this helps.

    Regards,

    /Dirk

  • Petr Snobelt 923 posts 1535 karma points
    Feb 01, 2010 @ 09:01
    Petr Snobelt
    0

    First of all you can try display your xml using textarea trick:

    <textarea>
     <xsl:copy-of select="." />
    </textarea>

    You also can try remove first dot in for-each select statement

    Petr

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Feb 01, 2010 @ 09:23
    Sebastiaan Janssen
    1

    You will not need the "./" before both selects, I suggest you do it a bit more like this:

    <xsl:template match="/">
     <xsl:for-each select="$currentPage/ancestor-or-self::node [@alias = 'panelText']">
       <xsl:value-of select="."/>
     </xsl:for-each>
    </xsl:template>

    So what this does is walk from the currentPage all the way up to the highest node (called node) then finds everything with the Alias of panelText. The select just shows that panelText ("." means: the current element's value).

  • Jesper Hauge 298 posts 487 karma points c-trib
    Feb 01, 2010 @ 09:41
    Jesper Hauge
    2

    I think what you're missing is a little primer on how xlst in umbraco works. I can see from your post that you've created some xslt in a xslt-editor, and tested your xslt directly on the umbraco.config file, which should work fine if the xslt files in umbraco was aused with this entire file when doing the transform.

    Problem is they aren't. When umbraco runs your xslt-file it doesn't have the usual access to the root of the umbraco.config file. The xml-file you're transforming has some extra xml content that is not present in umbraco.config, you may imagine an xml-document with the umbraco.config present in a subnode to the root, other subnodes contains stuff like macro parameters etc.

    That's why everybody uses the $currentPage parameter, which is a parameter pointing at the node of the currentpage in the umbraco.config file, if you need acces to the root of the umbraco.config and do a transformation from there you'll have to create a variable that point to the root first, which can be done like this:

    <xsl:variable name="umbRoot" select="$currentPage/ancestor::root" />

    This variable can then be used when you're doing transformations that contains code that starts out from the root of the umbraco.config file.

    I can recommend Douglas Robars excellent introduction to xslt in Umbraco.

    Regards
    Jesper Hauge

  • Brad 94 posts 151 karma points
    Feb 01, 2010 @ 12:10
    Brad
    0

    >>> I think what you're missing is a little primer on how xlst in umbraco works.

    I think you are right and until you wrote this message I had no reason to asssume that XSLT in Umbraco is somehow different. 

    Even that nice tutorial you directed me to (which I read earlier today) says this about currentPage

    "What page is the website visitor currently viewing? That is what the currentPage parameter is used for. Of all the pages in your site, currentPage will tell you which one is being viewed.

    The currentPage parameter is automatically populated by umbraco. It is referenced as $currentPage in your XSLT and you will use it a lot, as we'll see in future articles."

    Kind of plays down the importance.

    Thanks for the tips and explaination of this works. I'm off to give it a try.

     

  • Brad 94 posts 151 karma points
    Feb 02, 2010 @ 06:27
    Brad
    0

    I got this working using Dirks suggestion using GetXmlNodeById().Thanks Dirk. Here is the final code.

    <xsl:for-each select="umbraco.library:GetXmlNodeById(1062)/node">
    <xsl:sort select="@sortOrder" />
        <div class="news">
              <xsl:value-of select="data [@alias='panelText']" />
        </div>
        <xsl:if test="data [@alias='itemLink'] != ''">
            <a class="newsAnchor">
                <xsl:attribute name="href">
                                <xsl:value-of select="data [@alias='itemLink']"/>
                           </xsl:attribute>
                Read More >
            </a>
            <br></br>
        </xsl:if>
       
    </xsl:for-each>

    Thanks to all that responed.. Especially Jesper who gave some valuable info about how Unbraco works with XSLT (or visa versa). Info I could not find documented anywhere.

    One question though.. One would have to assume that the ID of a node, never changes for this to keep working. Right? I do hate hardcoding stuff like this but I got so held up (loosing money)  I was just happy to get it working.

    Brad

  • Jesper Hauge 298 posts 487 karma points c-trib
    Feb 02, 2010 @ 09:10
    Jesper Hauge
    0

    Hi Brad

    Glad you could use it.

    You're right, the id of a page doesn't change in its lifetime, but you should be aware, that deleting the page (node) and creating a new with identical property values, would create a node with a new id. This could happen if your customer deletes the panelitems node by accident.

    I situations like these I tend to create a document type to be used for holding panelitems alone, and then create a node of this type holding the panelitems in the root of the content tree, I then give this panelitems node the same nodename as the node holding the root of the website content:

    Root
        - mydomain.com (website)
            - content page 1 (textpage)
            - content page 2 (textpage)
            - ..
        - mydomain.com (panelitems)
            - panelitem 1 (panelitem)
            - panelitem 2 (panelitem)
            - ..

    Document type aliases in (). With this structure I can add more domains (like extra languages) and panelitems folders and still only use the following xslt to render panelitems for a website:

    <xsl:variable name="webRoot" select="$currentPage/ancestor-or-self::node[@nodeTypeAlias='website']" />
    <xsl:variable name="panelitemsRoot" select="$currentPage/ancestor::root/descendant::node[@nodeTypeAlias='panelitems' and @nodeName = $webRoot/@nodeName]" />
    
    <!-- xslt guff -->
    <xsl:for-each select="$panelitemsRoot/node">
        <!-- Your rendering code here -->
    </xsl:for-each>

    Regards
    Jesper Hauge

  • Brad 94 posts 151 karma points
    Feb 02, 2010 @ 11:53
    Brad
    0

    I'm happy to see that my site structure is exactly like yours. (maybe I did not expain it right) .. I have the site pages and the PanelItems in seperate nodes directly under root.

    And thanks for your code sample.. Consider it borrowed. :)

     

     

Please Sign in or register to post replies

Write your reply to:

Draft