I’m having trouble creating a global variable in my XSL 1.0 stylesheet. I want to create the variable from the value of an XML tag that is in the XML I’m trying to transform. Here is what my XML looks like:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<config name="test report" xmlns="http://www.example.com/CONFIG">
<the_one_i_want>1000</the_one_i_want>
<!-- lots of other stuff -->
</config>
And here is what my XSL looks like:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:CONFIG="http://www.example.com/CONFIG">
<xsl:output method="html"/>
<xsl:variable name="normal_global_variable">100</xsl:variable><!-- This works fine -->
<xsl:variable name="variable_from_xml"><xsl:value-of select="/config/the_one_i_want/value"/></xsl:variable><!-- This does not work -->
<!-- lots of other stuff -->
</xsl:stylesheet>
So I would expect that variable_from_xml would have a value of 1000, but it does not. What am I doing wrong?
P.S. The XML tag named the_one_i_want is unique and only appears once in my XML.
The problem is one of namespaces. The
<the_one_i_want>element that you’re after is bound to thehttp://www.example.com/CONFIGnamespace (which you’ve already defined in your XSLT).Therefore, simply change this:
to this:
or, even more simply: