Copied to clipboard

Flag this post as spam?

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


  • Steve 472 posts 1216 karma points
    Oct 30, 2012 @ 13:23
    Steve
    0

    Selecting nodes by node name

    I am new to Umbraco and find refrencing content nodes difficult in Umbraco as it seams you have to go back to the doctype of the node to select it in xpath. Is this correct or can you just follow the tree using the name of the node itself?

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Oct 30, 2012 @ 15:31
    Chriztian Steinmeier
    0

    Hi Steve,

    It's easiest to understand if you know how the XML looks - very condensed, a site looks like this:

    <root>
    
        <Homepage id="1200" nodeName="Home" isDoc="">
            <bodyText></bodyText>
    
            <!-- Child pages -->
            <Textpage id="1201" nodeName="Page 1" isDoc="">
                <!-- Custom properties -->
                <header></header>
                <bodyText></bodyText>
    
                <!-- Child pages -->
                <Textpage id="1202" nodeName="Page 1, Sub 1" isDoc="">
                    <!-- Custom properties -->
                    <header></header>
                    <bodyText></bodyText>
                </Textpage>           
    
            </Textpage>
    
            <Textpage id="1203" nodeName="Page 2">
                <!-- Custom properties -->
                <header></header>
                <bodyText></bodyText>
            </Textpage>
    
            <!-- Etc. -->
    
        </Homepage>
    </root>

     

    With XPath it's easy to select nodes by their element name (i.e. what Umbraco calls the "doctype alias") or by the value of an attribute, but XML doesn't have any way to distinguish between what Umbraco calls a "Document" and its properties, so Umbraco adds a special "isDoc" attribute to do that (it's always empty, but it's easy to test for, if you need to select multiple doctypes in a single selection).

    Let's say $currentPage is "Home", then you can try all of these to get what you need:

    <xsl:variable name="page1ById" select="$currentPage/*[@id = 1201]" />
    
    <xsl:variable name="page1ByName" select="$currentPage/*[@isDoc][@nodeName = 'Page 1']" />
    
    <xsl:variable name="page1ByPosition" select="$currentPage/Textpage[1]" />
    
    <xsl:variable name="page1ByDoctypeAndName" select="$currentPage/Textpage[@nodeName = 'Page 1']" />
    
    <xsl:variable name="allChildrenByDoctype" select="$currentPage/Textpage" />
    
    <xsl:variable name="allDocumentChildren" select="$currentPage/*[@isDoc]" />
    
    <xsl:variable name="allPropertiesOfPage2" select="$currentPage/Textpage[@nodeName = 'Page 2']/*[not(@isDoc)]" />
    
    Hope this clears up some of the confusion...
    I made a package called XML Dump that gives you easy access to the XML for any page you're on - you can even try some XPath on the document, directly from the URL... 

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft