Copied to clipboard

Flag this post as spam?

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


  • Mads Sørensen 188 posts 433 karma points
    Feb 23, 2013 @ 11:48
    Mads Sørensen
    0

    Variable and choose, can't reach node propertys

    Why cant i reach the propertys when i do like this:


    <xsl:variable name="country">
    <xsl:choose>
    <xsl:when test="$currentPage/ancestor-or-self::* [name()='DK']">
    <xsl:value-of select="$currentPage/ancestor-or-self::Pages/DK/descendant::* [name()='Article']" />
    </xsl:when>
    </xsl:choose>
    </xsl:variable>

    <xsl:value-of select="$country/@nodeName" />


    But if i do like this it works fine:

    <xsl:variable name="country" select="$currentPage/ancestor-or-self::Pages/DK/descendant::* [name()='Article']" />

    <xsl:value-of select="$country/@nodeName" />

     

    I cant see what i'm missing? any clues?

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 23, 2013 @ 12:24
    Jan Skovgaard
    0

    Hi Mads

    what does your test return if you just write in a value-of like this: <xsl:value-of select="$currentPage/ancestor-or-self::* [name()='DK']" /> - does it return true or false?

    /Jan

  • Mads Sørensen 188 posts 433 karma points
    Feb 23, 2013 @ 15:37
    Mads Sørensen
    0

    Hej Jan

    It returns all the content so i guess it's true :o)

    And if i try following, like above, just not with the @nodeName, it gives me the Article content but i cant select the single content parts like /pageHeader, /@nodeName or /contentText.

    <xsl:variablename="country">
           
    <xsl:choose>
                   
    <xsl:whentest="$currentPage/ancestor-or-self::* [name()='DK']">
                           
    <xsl:value-ofselect="$currentPage/ancestor-or-self::Pages/DK/descendant::* [name()='Article']"/>
                   
    </xsl:when>
           
    </xsl:choose>
    </xsl:variable>
                                   
    <xsl:value-ofselect="$country"/>

     

  • Mads Sørensen 188 posts 433 karma points
    Feb 23, 2013 @ 15:39
    Mads Sørensen
    0

    BTW it gives me a Error parsing XSLT file

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 23, 2013 @ 16:08
    Jan Skovgaard
    0

    Hi Mads

    There are some errors in the above snippet. whentest is not in one line but should be when test as in your first example. And value-ofselect should be value-of select.

    However...what is it you're trying to achieve here?

    /Jan

  • Mads Sørensen 188 posts 433 karma points
    Feb 23, 2013 @ 18:52
    Mads Sørensen
    0

    Hi Jan

    Dont know why that happend :D

    I have a structure look like this

    Content

    Site
    -DK (DocType DK)
    --FrontPage
    --Media
    ---Article Archive
    ----Articles

    -UK (DocType UK)
    --FrontPage
    --Media
    ---Article Archive
    ----Articles

    I have a articles.xslt placed at
    FrontPage
    Article Archive
    Articles

    At the frontpage i would like dick down i the article archive at the current Language and reach the latest article.

    So i try to do something like this

    <xsl:when test="$currentPage/self::* [name()='frontPage']">

    <xsl:variable name="country">
    <xsl:choose>
    <xsl:when test="$currentPage/ancestor-or-self::* [name()='DK']">
    <xsl:value-of select="$currentPage/ancestor-or-self::Pages/DK/descendant::* [name()='Article']" />
    </xsl:when>
    </xsl:choose>
    </xsl:variable>



    <xsl:for-each select="$country">

    <xsl:sort order="descending" />

    <xsl:if test="position()=1">

    <h1>Latest article</h1><br/>

    <img src="images/front_box.jpg" class="imgAbsoluteLeft" /><br/><br/>
    <p>
    <strong>

    <xsl:choose>
    <xsl:when test="pageHeader =''">
    <xsl:value-of select="@nodeName" />
    </xsl:when>

    <xsl:otherwise>
    <xsl:value-of select="pageHeader" />
    </xsl:otherwise>

    </xsl:choose>

    </strong><br/>
    </p>

    <xsl:value-of select="substring(contentText, 1, 300)" disable-output-escaping="yes" /><xsl:text>...</xsl:text>

    <a class="readMoreBtn" href="{umbraco.library:NiceUrl(@id)}">read more</a>

    </xsl:if>

    </xsl:for-each>

    </xsl:when>

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Feb 24, 2013 @ 11:42
    Chriztian Steinmeier
    100

    Hi Mads,

    To answer the question about the variable:

    The value-of instruction always returns a string, so you can't subsequently use XPath on that variable. If you need to conditionally select different nodes, use this pattern:

    <xsl:variable name="nodeProxy">
        <xsl:choose>
            <xsl:when test="something">
                <xsl:copy-of select="somenode" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="someothernode" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="node" select="msxsl:node-set($nodeProxy)" />

    The important part is that you're using copy-of instead of value-of, and then you need to use the msxsl:node-set() extension to be able to use XPath on the variable afterwards.

    But the solution to the problem at hand is much simpler - I always use a siteRoot variable right after the currentPage param:

    <xsl:param name="currentPage" />
    <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />

    (You may need to adjust the level to match your setup)

    Then you can select all Article nodes below that, like this:

    <xsl:variable name="articles" select="$siteRoot//Article" />

    - and use that in your for-each - it reads a million times easier for anyone reading the code.

    /Chriztian

  • Mads Sørensen 188 posts 433 karma points
    Feb 24, 2013 @ 21:18
    Mads Sørensen
    0

    ahh great idea about the siteRoot that save me for a lot of code :D

    Btw i really like your http://pimpmyxslt.com/axesviz.aspx it really make sense to a none-web-shark :o)

Please Sign in or register to post replies

Write your reply to:

Draft