XSL: get variable data without exslt: node -set

Using the built-in XSL library in PHP. Is it possible to get the value of a node inside a variable without having to call it through exslt: node -set every time ... it's 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" /> 
+4
source share
2 answers
 <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 are statically defined, then it is possible to access it from an XPath expression without using the xxx:node-set() extension function.

An 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 conversion is applied to any XML document (not used), the desired, correct result is obtained :

 2 
+6
source

You can call node-set only once. Convert the variable to 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" /> 
0
source

Source: https://habr.com/ru/post/1335165/


All Articles