Using native XSL lib in PHP. Is it possible to get a node value inside a variable without having t开发者_运维百科o call it through exslt:node-set every time.... it long and ugly.
<xsl:variable name="mydata">
<foo>1</foo>
<bar>2</bar>
</xsl:variable>
<!-- How currently being done -->
<xsl:value-of select="exslt:node-set($mydata)/foo" />
<!-- I want to be able to do this -->
<xsl:value-of select="$mydata/foo" />
<xsl:variable name="mydata"> <foo>1</foo> <bar>2</bar> </xsl:variable> <!-- How currently being done --> <xsl:value-of select="exslt:node-set($mydata)/foo" /> <!-- I want to be able to do this --> <xsl:value-of select="$mydata/foo" />
If the contents of the variable is statically defined, then it is possible to access it from an XPath expression without the use of the xxx:node-set()
extension function.
Example:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="mydata">
<foo>1</foo>
<bar>2</bar>
</xsl:variable>
<xsl:template match="/">
<xsl:value-of select=
"document('')/*/xsl:variable[@name='mydata']/bar"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on any XML document (not used), the wanted, correct result is produced:
2
It's possible to call node-set
just once. Convert the variable into a node-set type:
<!-- do it once at the beginning -->
<xsl:variable name="mydatans" select="exslt:node-set($mydata)" />
<!-- anytime you need: -->
<xsl:value-of select="$mydatans/foo" />
精彩评论