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 12, 2012 @ 18:04
    Steve
    0

    Template Match with a Variable

    Why do I get a "variable not defined" error parsing this?

    Here is my XML:

    <order>
     <required id="Blur" qnty="3"/>
         <catalogue>
             <album id="Blur" price="12"/>
             <album id="Travis" price="13"/>
         </catalogue>
    </order>

    Here is my XSLT: 

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:variable name="r" select="//required/@id"/>
        <xsl:template match="album[@id=$r]">
            We were looking for <xsl:value-of select="$r"/>and we found it!
        </xsl:template>
    </xsl:stylesheet>

    Thanks!

     

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Oct 12, 2012 @ 18:13
    Chriztian Steinmeier
    0

    Hi Steve,

    Unfortunately you can't use variables in a match pattern — the error message is a little off, though. 

    Though that's probably not your entire code, here's an alternate approach:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <xsl:variable name="r" select="//required/@id" />
    
        <xsl:template match="/">
            <xsl:apply-templates select="//album[@id = $r]" />
        </xsl:template>
    
        <xsl:template match="album">
            We were looking for <xsl:value-of select="@id"/>and we found it!
        </xsl:template>
    
    </xsl:stylesheet>

     

    /Chriztian

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies