Copied to clipboard

Flag this post as spam?

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


  • Wes 3 posts 23 karma points
    Dec 13, 2012 @ 11:41
    Wes
    0

    Help! - Xslt newbie

    Can someone please explain why this piece of code works:

     

      <xsl:variable name="maxItems" select="number(6)" />
        <msxsl:script language="JavaScript" implements-prefix="lk">
            function Random(r) { return Math.ceil(Math.random()*r); }
        </msxsl:script>

    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>
    <xsl:template match="/">
      
    <xsl:variable name="sliderNode" select="umbraco.library:GetXmlNodeById(1280)" />

            <xsl:for-each select="$sliderNode/child::Testimonial">
              
              <xsl:sort select="lk:Random($maxItems)" order="descending" />
                  <xsl:if test="position() &lt;= $maxItems">
                 <p><span class="regular-text"><xsl:value-of select="testimonialText" disable-output-escaping="yes"/>
                <span class="italic-text"><xsl:value-of select="testimonialName" disable-output-escaping="yes"/><br />
                 <xsl:value-of select="testimonialPosition" disable-output-escaping="yes"/><br />
                  <xsl:value-of select="testimonialCompany" disable-output-escaping="yes"/><br /><br /></span>
                <href="#">More Testimonials</a></span></p>
                  </xsl:if>

          </xsl:for-each>

    </xsl:template>

     

    but this piece of code doesn't.

    It gives me this error:
    System.Xml.Xsl.XslTransformException: To use a result tree fragment in a path expression, first convert it to a node-set using the msxsl:node-set() function. 

      <xsl:variable name="maxItems" select="number(6)" />
        <msxsl:script language="JavaScript" implements-prefix="lk">
            function Random(r) { return Math.ceil(Math.random()*r); }
        </msxsl:script>

    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>
    <xsl:template match="/">
      
      <xsl:variable name="sliderNode">  
      <xsl:choose>
        <xsl:when test="$currentPage/@id = '1206'">
           <xsl:value-of select="umbraco.library:GetXmlNodeById(1280)" />
        </xsl:when>
        <xsl:otherwise>
        <xsl:value-of select="umbraco.library:GetXmlNodeById(1279)" />
        </xsl:otherwise>
      </xsl:choose>
      </xsl:variable>
      
            <xsl:for-each select="$sliderNode/child::Testimonial">
              
              <xsl:sort select="lk:Random($maxItems)" order="descending" />
                  <xsl:if test="position() &lt;= $maxItems">
                 <p><span class="regular-text"><xsl:value-of select="testimonialText" disable-output-escaping="yes"/>
                <span class="italic-text"><xsl:value-of select="testimonialName" disable-output-escaping="yes"/><br />
                 <xsl:value-of select="testimonialPosition" disable-output-escaping="yes"/><br />
                  <xsl:value-of select="testimonialCompany" disable-output-escaping="yes"/><br /><br /></span>
                <href="#">More Testimonials</a></span></p>
                  </xsl:if>
              
      
          </xsl:for-each>

    </xsl:template>

     

    Any help is appreciated!

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Dec 13, 2012 @ 11:52
    Chriztian Steinmeier
    0

    Hi Wes,

    It's because in the second version you're using a value-of instruction to assign to the $sliderNode variable - value-of always returns a string, and you need a nodeset (a single node is still a nodeset). You can use copy-of to get the node instead - but you still need to create a nodeset from the created variable.

    There's a built-in extension function available for it that you can use like this:

    <xsl:variable name="sliderNodeProxy">  
        <xsl:choose>
            <xsl:when test="$currentPage/@id = '1206'">
                <xsl:copy-of select="umbraco.library:GetXmlNodeById(1280)" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="umbraco.library:GetXmlNodeById(1279)" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <!-- Create an XPath navigable variable from the above -->
    <xsl:variable name="sliderNode" select="msxsl:node-set($sliderNodeProxy)" />

    (msxsl: may need to be msxml: depending on which is used at the top of your stylesheet where it would say xmlns:msxsl="urn:schemas-...")

    /Chriztian

  • Wes 3 posts 23 karma points
    Dec 13, 2012 @ 12:20
    Wes
    0

    Thanks for the quick reply Chriztian!

    That's stopped the error from occuring but it's not actually returning any data on screen now.

    This is what I have:

    <xsl:variable name="pageID" select="$currentPage/@id" />     
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
    <xsl:template match="/">
      
    <xsl:variable name="sliderNodeProxy">  
            <xsl:choose>
                    <xsl:when test="$pageID = '1206'">
                            <xsl:copy-of select="umbraco.library:GetXmlNodeById(1280)" />
                    </xsl:when>
                    <xsl:otherwise>
                            <xsl:copy-of select="umbraco.library:GetXmlNodeById(1279)" />
                    </xsl:otherwise>
            </xsl:choose>
    </xsl:variable>
    <!-- Create an XPath navigable variable from the above -->
    <xsl:variable name="sliderNode" select="msxml:node-set($sliderNodeProxy)" />

            <xsl:for-each select="$sliderNode/child::Testimonial">
              <xsl:sort select="lk:Random($maxItems)" order="descending" />
                  <xsl:if test="position() &lt;= $maxItems">
                 <p><span class="regular-text"><xsl:value-of select="testimonialText" disable-output-escaping="yes"/>
                <span class="italic-text"><xsl:value-of select="testimonialName" disable-output-escaping="yes"/><br />
                 <xsl:value-of select="testimonialPosition" disable-output-escaping="yes"/><br />
                  <xsl:value-of select="testimonialCompany" disable-output-escaping="yes"/><br /><br /></span>
                <href="#">More Testimonials</a></span></p>
                  </xsl:if>
          </xsl:for-each>
    </xsl:template>

    I think it's an issue with the choose.

    $pageID definitely returns the correct ID, the otherwise doesn't seem to work either.

    I'm not sure how best to debug it either, I've tried to output text from the when and otherwise but nothing is ever returned.

     

    Cheers again!

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Dec 13, 2012 @ 12:54
    Chriztian Steinmeier
    0

    Ah - yes - the node-set() function creates a document-node wrapper, so you need to do this instead, to select the node that was picked:

    <xsl:variable name="sliderNode" select="msxml:node-set($sliderNodeProxy)/*[@isDoc]" />

    /Chriztian

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Dec 13, 2012 @ 12:55
    Chriztian Steinmeier
    0

    - Oh, and to debug you can always just dump the contents of $sliderNode to a textarea:

    <textarea rows="8" cols="40">
        <xsl:copy-of name="$sliderNode" />
    </textarea>

    /Chriztian

  • Wes 3 posts 23 karma points
    Dec 13, 2012 @ 12:58
    Wes
    0

    That's brilliant Chriztian!

    All working now. Thanks for the help and the tips!

Please Sign in or register to post replies

Write your reply to:

Draft