在 xsl:apply-templates 中使用 XSL 变量作为 Xpath 值 [英] Using an XSL variable as Xpath value in xsl:apply-templates

查看:26
本文介绍了在 xsl:apply-templates 中使用 XSL 变量作为 Xpath 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用节点的内容作为 Xpath 引用.不幸的是,到目前为止我还没有找到实现这一目标的方法.

I am trying to use the content of a node as an Xpath reference. Unfortunately I have not found the way to achieve this so far.

这里是我试图处理的 XML 类型的一个小例子:

So here is a little example of the type of XML I am trying to process:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <a>
    <b name="1">
      <e>value1</e>
    </b>
    <b name="2">
      <e>value2</e>
    </b>
  </a>
  <c>
    <d name="3">
      <ref>/root/a/b[@name = "1"]</ref>
    </d>
    <d name="4">
      <ref>/root/a/b[@name = "2"]</ref>
    </d>
  </c>
</root>

在那里 节点包含 Xpath 到一些包含所需值的文件内节点.

In there the <ref> nodes contain Xpath to some in-file nodes containing the required value.

这是我试图组合在一起以处理此文件的 XSL 文件类型:

Here is the type of XSL file I was trying to put together to process this file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:apply-templates select="root/c/d"/>
  </xsl:template>

  <xsl:template match="d">
    <xsl:variable name="var1" select="ref/text()"/>
    for node <xsl:value-of select="@name"/>
    <xsl:apply-templates select="$var1"/>
  </xsl:template>

  <xsl:template match="b">
   value = <xsl:value-of select="e"/>
  </xsl:template>

</xsl:stylesheet>

但是当我使用建议的 XSL 处理上述 XML 文件时,我得到以下结果:

But when I process the above XML file with the proposed XSL I am getting the following result:

for node 3/root/a/b[@name = "1"]
for node 4/root/a/b[@name = "2"]

显然,$var1 的内容未被评估为有效的 xpath.

Obviously the content of $var1 is not evaluated as a valid xpath.

注意:如果我将 $var1 替换为/root/a/b[@name = "1"] 我会得到

Note: if I replace $var1 with /root/a/b[@name = "1"] I am getting

    for node 3
   value = value1
    for node 4
   value = value1

哪个更接近我想要的但节点 4 显然不再指向 value2 节点.

Which is closer to what I want but the node 4 is obviously no pointing to the value2 node anymore.

我错过了什么?

这可以用 XSL 实现吗?

Is this possible with XSL?

动态评估"是否存在问题,我是否应该使用其他方法(例如 xsl:function 或 xsl:call-template)?

Is there some problem with "dyanmic evaluation" and should I use some other mean (like xsl:function or xsl:call-template)?

感谢您的帮助.

正如Daniel Haley"所建议的那样,使用 dyn:evaluate 确实可以解决问题.

As suggested by "Daniel Haley" using dyn:evaluate does solve the problem.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                            xmlns:dyn="http://exslt.org/dynamic"
                            version="1.0"
                            extension-element-prefixes="dyn">

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:apply-templates select="root/c/d"/>
  </xsl:template>

  <xsl:template match="d"><xsl:variable name="var1" select="ref/text()"/>
    for node <xsl:value-of select="@name"/>
    <xsl:apply-templates select="dyn:evaluate($var1)"/>
  </xsl:template>

  <xsl:template match="b">
   value = <xsl:value-of select="e"/>
  </xsl:template>

</xsl:stylesheet>

例如将 xsltproc 与此 XSL 样式表一起使用确实会产生预期的结果.

using for example xsltproc with this XSL stylesheet does produce the expected result.

$ xsltproc ./file.xsl ./file.xml 

    for node 3
   value = value1
    for node 4
   value = value2
$

我还没有研究过 xsl:evaluate 方法.

I have not investigated the xsl:evaluate method yet.

再次感谢您的帮助.

推荐答案

您可以使用 xsl:evaluate 在 XSLT 3.0 中(前提是您的处理器支持它).

You can use xsl:evaluate in XSLT 3.0 (provided your processor supports it).

你也可以使用dyn:evaluate EXSLT扩展功能(同样,前提是您的处理器支持).

You can also the use dyn:evaluate EXSLT extension function (again, provided your processor supports it).

这篇关于在 xsl:apply-templates 中使用 XSL 变量作为 Xpath 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆