Copied to clipboard

Flag this post as spam?

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


  • David Cardine 7 posts 27 karma points
    Feb 17, 2011 @ 21:46
    David Cardine
    0

    setting xpath query in variable inconsistency

    can someone explain to me why the following examples act differently and tell me what i should be doing instead?

    query2

    <xsl:variable name="query2">
      <xsl:value-of select="$currentPage/Posts/Year/Month/*"/>    
    </xsl:variable>

    query3

    <xsl:variable name="query3" select="$currentPage/Posts/Year/Month/*"/>

    the reason i have query2 is b/c i was trying to troubleshoot why my query1 (not shown) isn't working as it is using a choose/when/otherwise to set the query, so i simplified it to just set the value within the variable tags as shown (query2).

    when i set the select of my for-each to query2 i get no results. when i set it to query3 it works fine.

    thanks for any help

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 17, 2011 @ 21:49
    Jan Skovgaard
    0

    Hi David

    At a first sight that seems a bit odd...I think it should be able to do it like you have posted above. But I would really love to see the context in, which you're using the variable so if you can post some more of your code it will be more easy to help you out I think.

    /Jan

  • David Cardine 7 posts 27 karma points
    Feb 17, 2011 @ 21:59
    David Cardine
    0
    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>

    <!-- params passed in from macro  -->    
        <xsl:variable name="month" select="/macro/month" />
        <xsl:variable name="year" select="/macro/year" />

    <xsl:variable name="query">
      <xsl:choose>
        <xsl:when test="$year!='' and $month!=''">
            <xsl:value-of select="$currentPage/Posts/Year[@nodeName=$year]/Month[@nodeName=$month]/*"/>      
        </xsl:when>
       
    <xsl:when test="$year!=''">     
      
    <xsl:value-of select="$currentPage/Posts/Year[@nodeName=$year]/Month/*"/>
        </xsl:when>
        <xsl:when test="$month!=''">
            <xsl:value-of select="$currentPage/Posts/Year/Month[@nodeName=$month]/*"/>
        </xsl:when>
        <xsl:otherwise>     
      
    <xsl:value-of select="$currentPage/Posts/Year/Month/*"/>
        </xsl:otherwise>
      </xsl:choose>    
    </xsl:variable>
        
    <xsl:variable name="query2">
      <xsl:value-of select="$currentPage/Posts/Year/Month/*"/>    
    </xsl:variable>
        
    <xsl:variable name="query3" select="$currentPage/Posts/Year/Month/*"/>

    <xsl:template match="/">  
      <ul>
        <xsl:for-each select="msxml:node-set($query3)">
          <li>     
      
    <xsl:value-of select="@nodeName"/>
          </li>
        </xsl:for-each>
      </ul>

    </xsl:template>

    Thats the gist. my content tree is basically Home/Posts/2011/April/Post.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Feb 18, 2011 @ 10:21
    Chriztian Steinmeier
    0

    Hi David,

    The value-of instruction always returns a string - if the XPath matches more than one node, you'll get the string-value of the first node in that set.

    You might be able to accomplish what you're after by using copy-of instead of value-of - but you have to use the node-set() function on the result, to be able to use XPath on that (but I can see you're already toying with that...)

    /Chriztian 

  • David Cardine 7 posts 27 karma points
    Feb 18, 2011 @ 15:28
    David Cardine
    0

    Thanks Chriztian I finally got it using your advice. Your explaination was great!

    Here's how i finally got it working.

    <xsl:variable
    name="query">
      <xsl:choose>
        <xsl:when test="$year!='' and $month!=''">
            <xsl:copy-of
    select="$currentPage/Posts/Year[@nodeName=$year]/Month[@nodeName=$month]/*"/>      
     
      
    xsl:when>
        <xsl:when test="$year!=''">
         
      
    <xsl:copy-of select="$currentPage/Posts/Year[@nodeName=$year]/Month/*"/>
        xsl:when>
        <xsl:when test="$month!=''">
            <xsl:copy-of select="$currentPage/Posts/Year/Month[@nodeName=$month]/*"/>
        xsl:when>
        <xsl:otherwise>
         
      
    <xsl:copy-of select="$currentPage/Posts/Year/Month/*"/>
        xsl:otherwise>
      xsl:choose>    
    xsl:variable>

     

    <xsl:for-each select="msxml:node-set($query)/*">
Please Sign in or register to post replies

Write your reply to:

Draft