Would you expect this to work or not? (it seems you can’t use XPath on dynamically created elements, perhaps because they’re not in the ‘behind the scenes’ magic used to make XPath perform well)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="data">
<fu><bar>Test</bar></fu>
</xsl:variable>
Value: <xsl:value-of select="$data/fu/bar" />
</xsl:template>
</xsl:stylesheet>
I get an error in the XSLT processors that I’ve tried, e.g. “invalid type” when using PHP5; removing the “$” from before “data” will make the error go away, but obviously not do what is desired, as it will try to match data/fu/bar from the XML document that the XSLT is being ran against.
There aren’t any easy answers to this, but “the way” to do this is with the the node-set() function. But it’s not technically part of XSLT 1.0 and therefore isn’t necessarily supported by all transformation engines. See this link for more info: http://www.xml.com/pub/a/2003/07/16/nodeset.html
Basically, $data is a string, not a tree, so you need a special function to convert it into a tree. That is why you are getting “invalid type” – a data type mismatch.