XSL:获取不带exslt的可变数据:node-set

时间:2011-01-11 21:15:59

标签: php xslt

在PHP中使用本机XSL库。是否有可能在变量中获取节点值而无需每次都通过exslt:node-set调用它......它很长而且很难看。

<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" />

2 个答案:

答案 0 :(得分:6)

<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" />

如果静态定义变量的内容,那么 可能可以从XPath表达式访问它而不使用xxx:node-set()扩展函数

示例

<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>

将此转换应用于任何XML文档(未使用)时,会生成所需的正确结果

2

答案 1 :(得分:0)

可以只调用node-set一次。将变量转换为节点集类型:

<!-- do it once at the beginning -->
<xsl:variable name="mydatans" select="exslt:node-set($mydata)" />
<!-- anytime you need: -->
<xsl:value-of select="$mydatans/foo" />