如何在需要路径调用的udf中设置变量的值 [英] How to set the value of a variable in a udf requiring a path-call

查看:147
本文介绍了如何在需要路径调用的udf中设置变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在XSLT中使用用户定义的函数,它重复调用某个字符串的值。该字符串基于XPath表达式的结果,该表达式在单个函数调用的范围内不会更改。我认为将它分配给一个变量是一个好主意,而不是一遍又一遍地查看它。



不幸的是,至少在撒克逊的实现中,你不能使用一个XPath表达式需要一个函数内部的节点,甚至是一个基于绝对路径的节点,而不是先使用一次性抛弃线让函数知道你正在讨论根文档而不是其他文档。

例如,下面的代码会抛出一个错误:

 < xsl:function name = UDF:LeafMatch > 
< xsl:param name =sID>< / xsl:param>

正常情况下,解决方案只是首先调用任何全局变量来提供上下文。例如,在udf($ root是一个用根节点标识的变量)内部的以下工作:

 < xsl :for-each select =$ root> 
< / xsl:for-each>

但是,当尝试使用Xpath修复变量的值时,这不起作用, m不允许将表达式放在for-each中。



我也尝试过使用

 < xsl:choose>< xsl:when select$ root>< xsl:value-of select =/ potato / stem [@ sessionID = $ sID] [scc] / scc / @ leafnumber/>< / XSL:当>< / XSL:选择> 

给它上下文,继续我在这里看到的: http://www.stylusstudio.com/xsllist/200504/post00240.html


$

FWIW将变量传递给函数是有问题的,因为用于定义myleaf的Xpath表达式取决于上下文节点,并且我不知道如何让Xpath根据当前上下文节点中的值调用一个路径。

例如,在调用此代码的代码中函数我有类似的东西:

 < xsl:for-each select =/ potato / stem [eye]> 
< leaf ={udf:LeafMatch(@sessionID)}/>
< / xsl:for-each>

我正在处理/ potato / stem [eye]节点并使用udf查找具有@sessionID相同值的/ potato / stem [scc]节点。我不知道如何在XPath的谓词谓词中的当前上下文节点中引用@sessionID的值,以便在XML树的完全不同的部分中搜索其他节点,所以我使用的是udf来做到这一点。它工作正常,直到我决定尝试为字符串使用变量而不是让处理器每次查看它。



我试图避免去一个级别更深一层(让我的函数自己调用一个已命名的模板,或者将一个已命名的模板放在我原来的for-each中,并让该模板调用一个函数)。

所以我的问题是:



A。对于用户定义的函数,如何设置一个依赖于XPath表达式的变量?



B。在Xpath中,是否有一种时髦的方式来使用从当前内容节点中抽取的值来测试Xpath表达式的谓词? >


所以我的问题是:

A。对于用户定义的函数,如何在XPath表达式上设置一个依赖
的变量?



B。在Xpath中,是否有一种时髦的方式来使用从当前
内容节点中抽取的值来测试
的Xpath表达式的谓词?


这两个问题都很不清楚。

A:我假设你的意思是:


在$ 内部xsl:function 如何定义一个依赖于
上下文节点的变量?


答案:你不行。根据定义,在 xsl:function 中没有上下文节点。这由 W3C XSLT 2.0规范 定义如下所示:


在样式表函数的主体中,焦点最初是
undefined; this意味着任何尝试引用上下文项,
上下文位置或上下文大小都是不可恢复的动态错误。
[XPDY0002]


< blockquote>

但是,您可以将预定的上下文节点(或仅作为当前必须使用的文档节点)作为参数传递。或者,您也可以参考一个全局定义的变量。



B:这个问题完全不可理解:


  1. 什么是时髦?

  2. 什么是当前内容节点?请提供一个具体任务的例子,以时髦的方式完成。


I'm trying to use a user-defined function in XSLT that repeatedly calls the value of a certain string. That string is based on the outcome of an XPath expression that doesn't change within the span of a single function call. I thought it would be a good idea to assign it to a variable rather than look it up over and over again.

Unfortunately, at least in Saxon's implementation, you cannot use an XPath expression requiring a node inside a function, even one based on an absolute path, without first using a throw-away line to let the function know you are discussing the root document rather than some other one.

So, for example, the following code throws an error:

<xsl:function name="udf:LeafMatch">
<xsl:param name="sID"></xsl:param>
<xsl:variable name="myLeaf" select="/potato/stem[@sessionID=$sID][scc]/scc/@leafnumber"/>

Normally, the solution is just to first call any global variable to give context. For example, the following works inside of an udf ($root is a variable identified with the root node):

<xsl:for-each select="$root">
<xsl:value-of select="/potato/stem[@sessionID=$sID][scc]/scc/@leafnumber"/>
</xsl:for-each>

But this doesn't work when trying to use Xpath to fix the value of a variable because I'm not allowed to put the expression within a for-each.

I also tried using

<xsl:choose><xsl:when select"$root"><xsl:value-of select="/potato/stem[@sessionID=$sID][scc]/scc/@leafnumber"/></xsl:when></xsl:choose>

to give it context, going on what I saw here:http://www.stylusstudio.com/xsllist/200504/post00240.html

That didn't work either.

FWIW, passing the variable into the function is problematic because the Xpath expression used to define "myleaf" depends on the context node, and I don't know how to get Xpath to call one path based on values in the current context node.

For example, in the code calling this function I have something like:

<xsl:for-each select="/potato/stem[eye]">
<leaf = "{udf:LeafMatch(@sessionID)}"/>
</xsl:for-each>

I'm working in the context of a /potato/stem[eye] node and using the udf to look for a /potato/stem[scc] node that has the same value of @sessionID. I don't know how to reference the value of @sessionID from the current context node in the predicate of an XPath searching for other nodes in a completely different part of the XML tree, so I was using a udf to do that. It was working fine until I decided to try to use a variable for the string rather than having the processor look it up each time.

I was trying to avoid going one level deeper (having my function itself call a named template or putting a named template inside my original for-each and having that named template call a function).

So my questions are:

A. For a user-defined function, how do I set a variable that depends on an XPath expression?

B. Is there a snazzy way in Xpath to use values drawn from the current content node in the predicates of the Xpath expression you are trying to test?

解决方案

So my questions are:

A. For a user-defined function, how do I set a variable that depends on an XPath expression?

B. Is there a snazzy way in Xpath to use values drawn from the current content node in the predicates of the Xpath expression you are trying to test?

Both questions are quite unclear.

A: I assume you actually mean:

"Inside an xsl:function how do I define a variable that depend on the context node?"

The answer: You can't. By definition there is no context node within an xsl:function. This is defined by the W3C XSLT 2.0 specification in the following way:

"Within the body of a stylesheet function, the focus is initially undefined; this means that any attempt to reference the context item, context position, or context size is a non-recoverable dynamic error. [XPDY0002]"

You can, however, pass as a parameter the intended context node (or just the document node that must be used as current). Or, alternatively, you may refer to a globally defined variable.

B: This question is completely not understandable:

  1. What is "snazzy"?

  2. What is "current content node"? Please, provide an example of a specific task to be accomplished in the wanted "snazzy" way.

这篇关于如何在需要路径调用的udf中设置变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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