checking value of a variable inside a higher-precendence variable in XSLT

StackOverflow https://stackoverflow.com/questions/400909

  •  03-07-2019
  •  | 
  •  

Question

I need to be able to check what the "current" value of a variable is inside a redeclaration of that same variable in a different xslt that includes the other.

main.xslt:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:import href="other.xslt"/>
    <xsl:variable name="email">
        <xsl:if test="string-length($email) = 0">
            bar@foo.com
        </xsl:if>
    </xsl:variable>
    <xsl:template match="/">
        <Email>
            <xsl:value-of select="$email"/>
        </Email>
    </xsl:template>
</xsl:stylesheet>

other.xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="email">foo@bar.com</xsl:variable>
</xsl:stylesheet>

What I would like to do is be able to check what the value of the lower-precedence variable will be to see if I need to overwrite it in the variable in the current xslt. (disclaimer - the current code crashes viciously)

Was it helpful?

Solution

As others have noted, adding a default to a global variable that is defined in an imported stylesheet, cannot be done using the same variable name. This is because the variable with that name that is defined in the current xslt stylesheet has higher precedence than the one in the imported stylesheet and only the former will be used (you cannot access the identically named variable in the lower precedence stylesheet).

Here is how one can add a default:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--                                            -->
  <xsl:import href="other.xsl"/> 
<!--                                            -->
    <xsl:variable name="vMyEmail" select=
     "concat(substring('bar@foo.com', 1 div not($vEmail)), $vEmail)"
     />
<!--                                            -->
    <xsl:template match="/">
      <xsl:value-of select="$vMyEmail"/>
    </xsl:template>
</xsl:stylesheet>

Do note that the global variable $vMyEmail is defined in such a way that it has the value of the variable $vEmail (defined in the imported stylesheet) if this is a string with length at least 1, or the desired default value -- otherwise.

Using this technique, one will use the so defined $vMyEmail anywhere following its definition. The $vEmail variable from the imported stylesheet will not be used directly at all.

OTHER TIPS

You can't do what you want directly, you'd have to set a second variable.

But I can't understand the intent: if this was template based you could supply a default value to any param (which act very similarly to variables). The way you phrase the question suggests that's the right approach to me, but can you clarify how these templates relate and are used?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top