XSL能不能动态改变XPath?

graycarl 2004-09-14 12:40:28
<xsl:variable name="Name" select="/Nodes/Node2"/>
<xsl:apply-templates select="{$Name}"/>
这个语句无法执行。

有没有办法实现这样的功能,让这个XPath达到延迟绑定的效果?
...全文
101 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2004-09-15
  • 打赏
  • 举报
回复
XSLT 1.0 deoesn't support dynamic evaluation of xpath expressions

either you need to write a recursive template to parse the xpath string yourself and match the name() against the xpath, or if you are using MSXML3/4 or IE6 , you could do something like


1. xml:

<doc>
<link xpath="/doc/link/linkdata/node1/node3">
<linkdata>
<node1>
<node3>A_VALUE</node3>
</node1>
<node2>
</node2>
</linkdata>
</link>
</doc>


2. xsl:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="whatever">

<msxsl:script language="jscript" implements-prefix="user">
function getNodes(node, xpath)
{
return node.nextNode().selectNodes(xpath);
}
</msxsl:script>


<xsl:template match="/">
<xsl:value-of select="user:getNodes(.,string(/doc/link/@xpath))" />
</xsl:template>
</xsl:stylesheet>
graycarl 2004-09-14
  • 打赏
  • 举报
回复
是这样的
<xsl:variable name="Name" select="'/Nodes/Node2'"/>
<xsl:apply-templates select="{$Name}"/>
graycarl 2004-09-14
  • 打赏
  • 举报
回复
<doc>
<link xpath="/doc/linkdata/node1/node3">
<linkdata>
<node1>
<node3>A_VALUE</node3>
</node1>
<node2>
...
</node2>
</linkdata>
</doc>
graycarl 2004-09-14
  • 打赏
  • 举报
回复
看来我还是没有把问题表述清楚,真是脸红
我现在明白msxsl:node-set并不是我想象的那个概念了
我的XML是这样的
<doc>
<link xpath="/doc/linkdata/group1/item1">
<linkdata>
<node1>
<node3>A_VALUE</node3>
</node1>
<node2>
...
</node2>
</linkdata>
</doc>
我想根据link所指定的XPATH表达式来找到它对应的linkdata
我获取的是一个XPATH字符串,我有没有办法把它变成一个真正的XPath?
至于具体获取数据用xsl:for-each,或者xsl:apply-templates都无所谓
但它们都有一个讨厌的select属性只接受XPath表达式……而不能是一个字符串变量
:(

能不能做到这样的效果?
孟子E章 2004-09-14
  • 打赏
  • 举报
回复
<xsl:template match="">不支持变量,你只能传递参数,类似

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
version="1.0">
<xsl:variable name="Name">
<document><title>fff</title></document>
</xsl:variable>
<xsl:template match="/">
<xsl:call-template name="MM"/>
</xsl:template>
<xsl:template name="MM">
<xsl:for-each select="msxsl:node-set($Name)">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
graycarl 2004-09-14
  • 打赏
  • 举报
回复
请大家帮忙看看
graycarl 2004-09-14
  • 打赏
  • 举报
回复
抱歉 我表述不清

就是说我将要select的NodeSet我希望在运行时替它指定XPath:
就是下面这段代码应该达到的效果
<xsl:variable name="Name" select="'/Document/Data/Item1'"/>
<xsl:apply-templates select="msxsl:node-set($Name)"/>

但是这段代码不起作用
孟子E章 2004-09-14
  • 打赏
  • 举报
回复
延迟绑定,什么意思啊。

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧