My questions are at the bottom of this post, if you wish to read them before the full explanation.
I'm converting an XML document to a pretty web page using XSL, and am having trouble with correctly passing a variable. I have many xsl:template
s defined, and need to pass a specific parameter to just one of them. I was hoping that I would be able to pass a named parameter that would presumably be sent to all of the xsl:template
s, but only be used by a single one and ignored by the others. However, when trying to test this for myself (and my limited understanding of XSL), I was unable to pass the parameter at all, let alone test if it was accidentally disturbing any other xsl:template
s.
The following is simplified example code (typed up for this question, it may contain a typo or two). I have many many different xsl:template
s defined to deal with nodes in the XML, and everything has been working fine until now. It is in adding a parameter to these templates that I appear to be having issues.
XML file:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="main.xsl"?>
<wrapperNode>
<testNode>
<subNode/>
</testNode>
</wrapperNode>
main.xsl:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="test.xsl"/>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates>
<xsl:with-param name="testParam">TEST_PARAMETER</xsl:with-param>
</xsl:apply-templates>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
test.xsl:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="testNode">
<xsl:param name="testParam" />
TEST1
<xsl:value-of select="$testParam" />
TEST2
</xsl:template>
</xsl:stylesheet>
Output (actual):
TEST1 TEST2
Output (expected/desired):
TEST1 TEST_PARAMETER TEST2
My questions in regards to this:
Is it possible to send a named parameter to all of my
xsl:template
s using anxsl:apply-templates
withxsl:with-param
, but select this value specifically byname=
within the actual template so that it can be explicitly used in a single template and ignored by all others (even if I wanted to add other, differently named, parameters for other templates later)?What am I doing wrong with my current sample code that it does not seem to receive the parameter at all?
Is there a better way to accomplish this?
Edit: I want to make it clear that due to other output within the test.xsl
:testNode
template, I know for sure that it IS being successfully called. It is ONLY the parameter part that is not working. I do not mean to waste people's time figuring out why that template is not being called. It is.
Update: In response to the answers I initially received, which pointed out that the example I made up was not completely correct (my mistake) and did not very clearly show the issue (ie: that the correct template is being called, but that only the parameter appears to not be working), I have replaced the examples with much better ones. This example more clearly shows that the testNode
template is successfully being called, but that the parameter does not seem to be passed. I have tested this numerous times, before and after consideration of the previous answers to this question. I am absolutely stumped, as everything appears to be correct from what I have read elsewhere and what people have suggested so far.