Copied to clipboard

Flag this post as spam?

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


  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jun 14, 2010 @ 16:42
    Tom Fulton
    0

    Conditionally assign XML to a variable - parse errors

    Hi all,

    I'm having some trouble figuring out how to conditionally assign XML to a variable in XSLT.

    Here's what I started with:

    <xsl:variable name="currentTabXml" select="umbraco.library:GetXmlNodeById(/macro/tabId)"/>


    The rest of my code is referencing things like $currentTabXml/@nodeTypeAlias, which works fine using the above.

    Now instead I'd like to set the variable conditionally. For simplicity I've removed the choose statements and tried to reproduce the equivalent to the above line:

    <xsl:variable name="currentTabXml"><xsl:copy-of select="umbraco.library:GetXmlNodeById(/macro/tabId)"/></xsl:variable>


    But this gives me parse errors in runtime.  I've tried all kinds of combinations, value-of, wrapping GetXmlNodeById in msxml:node-set, etc.  Any ideas on what I'm missing?

    Thanks,
    Tom

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jun 14, 2010 @ 17:37
    Ismail Mayat
    0

    Tom,

    Paste the whole code for the conditional set of the variable it will give clearer picture.

    Regards

    Ismail

  • Peter Duncanson 430 posts 1360 karma points c-trib
    Jun 14, 2010 @ 19:28
    Peter Duncanson
    0

    Not sure why you would want to do that Tom, you are already getting the XML back in the right format using your first example?

    But I guess you are not converting it to a node-set at the right time. You need to build up your XML first then once finished and you want to use it then convert it into a node-set. I tend to do stuff like this:

    <!-- Build up your XML in a temp variable -->
    <xsl:variable name="temp_books">
      <books>
        <xsl:copy-of select="//books/book" />
      </book>
    </xsl:varable>
    
    <!-- Convert your temp variable to a usable node-set, this way you only need to do it once -->
    <xsl:variable name="books" select="msxml:node-set( $temp_books )" />

    Hope that sheds some light?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jun 15, 2010 @ 00:00
    Tom Fulton
    1

    Thanks for the replies - it helped me figure it out. 

    The issue was actually due to my lack of xpath knowledge.  When changing from my initial code to the conditional with copy-of etc, I found I had to add an extra "/node" after the variable.  IE $currentTabXml/@nodeTypeAlias became $currentTabXml/node/@nodeTypeAlias.  I added this to a second variable to avoid having to change a bunch of code as Peter suggested.

    Thanks again, this has been bugging me for a while on other projects

    <xsl:variable name="currentTab">
        <xsl:choose>
            <xsl:when test="count($slideshowxml/node) = 1">
                <xsl:copy-of select="umbraco.library:GetXmlNodeById($slideshowxml/node/@id)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="umbraco.library:GetXmlNodeById(/macro/tabId)"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="currentTabXml" select="msxml:node-set($currentTab)/node"/>
Please Sign in or register to post replies

Write your reply to:

Draft