data.xml:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="myxslt2.xslt"?>
<data>
<foo>Hello</foo>
<bar>World</bar>
<foobar>This is a test</foobar>
</data>
myxslt2.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" version="1.0">
<xsl:output method="html" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" indent="yes"/>
<xsl:variable name="main" select="/data"/>
<xsl:template name="myTemplate">
<xsl:param name="myparam"/>
Inner1:<xsl:value-of select="msxsl:node-set($myparam)"/>
<br/>
Inner2:<xsl:value-of select="msxsl:node-set($myparam)/foobar"/>
</xsl:template>
<xsl:template match="/data">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
HTML STARTS
<br/>
<xsl:variable name="data" select="."/>
Outer1:<xsl:value-of select="$data"/>
<br/>
Outer2:<xsl:value-of select="$data/foobar"/>
<br/>
<xsl:call-template name="myTemplate">
<xsl:with-param name="myparam">
<xsl:value-of select="$data"/>
</xsl:with-param>
</xsl:call-template>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
HTML STARTS
Outer1: Hello World This is a test
Outer2:This is a test
Inner1: Hello World This is a test
Inner2:
Can someone explain why the inner doesn't resolve the subelement while the outer does?
The problem lies with when you pass your the $data variable as a parameter to myTemplate
Because you are using xsl:value-of here, this is only passing the text value of the nodes held in $data; i.e. the parameter is just a single text node. To retain the nodes you would need to use xsl:copy-of
Strictly speaking, this passing a "result tree fragment" which is why you would have to use the node-set extension function, but you would have to modify your use of it because the data node is not strictly the parent node here, the document fragment is
But, you don't actually have to use node-set here at all really. Change how call the template to this...
There is an important difference here. This is no longer a result tree fragment, but is referencing the input document directly, meaning you don't have to use node-set at all.