XPATH 或 XSL 使用自定义比较来匹配两个节点集 [英] XPATH or XSL to match two node-sets using custom comparison

查看:23
本文介绍了XPATH 或 XSL 使用自定义比较来匹配两个节点集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还可以访问 ESXLT 功能.

I also have access to ESXLT functions.

我有两个字符串令牌节点集.一组包含以下值:

I have two node sets of string tokens. One set contains values like these:

/Geography/North America/California/San Francisco
/Geography/Asia/Japan/Tokyo/Shinjuku

另一组包含如下值:

/Geography/North America/
/Geography/Asia/Japan/

我的目标是在两者之间找到匹配".当集合 1 中的任何字符串以集合 2 中的字符串开头时,就会进行匹配.例如,将在 /Geography/North America/California/San Francisco/Geography 之间进行匹配/North America/ 因为集合 1 中的字符串以集合 2 中的字符串开头.

My goal is to find a "match" between the two. A match is made when any string in set 1 begins with a string in set 2. For example, a match would be made between /Geography/North America/California/San Francisco and /Geography/North America/ because a string from set 1 begins with a string from set 2.

我可以通过第三方扩展使用通配符来比较字符串.我还可以在 Xpath 中使用正则表达式.

I can compare strings using wildcards by using a third-party extension. I can also use a regular expression all within an Xpath.

我的问题是如何构建 Xpath 以在两个集合的所有节点之间使用函数进行选择?XSL 也是一个可行的选择.

My problem is how do I structure the Xpath to select using a function between all nodes of both sets? XSL is also a viable option.

这个 XPATH:

count($set1[.=$set2])

将产生 set1 和 set2 之间的交集计数,但它是 1 对 1 的比较.是否可以使用其他方法比较节点?

Would yield the count of intersection between set1 and set2, but it's a 1-to-1 comparison. Is it possible to use some other means of comparing the nodes?

我确实让这个工作正常,但我通过使用其他一些第三方扩展来获得相同的结果来作弊.我仍然对完成这项工作的其他方法感兴趣.

I did get this working, but I am cheating by using some of the other third-party extensions to get the same result. I am still interested in other methods to get this done.

推荐答案

这个:

<xsl:variable name="matches" select="$set1[starts-with(., $set2)]"/>

将把 $matches 设置为包含 $set1 中每个节点的节点集,其文本值以 $set2 中节点的文本值开头.这就是你要找的,对吧?

will set $matches to a node-set containing every node in $set1 whose text value starts with the text value of a node in $set2. That's what you're looking for, right?

好吧,我错了.原因如下.

Well, I'm just wrong about this. Here's why.

starts-with 期望它的两个参数都是字符串.如果不是,它会在评估函数之前将它们转换为字符串.

starts-with expects its two arguments to both be strings. If they're not, it will convert them to strings before evaluating the function.

如果你给它一个节点集作为它的一个参数,它使用节点集的字符串值,它是集合中第一个节点的文本值.所以在上面, $set2 永远不会被搜索;只会检查列表中的第一个节点,因此谓词将只查找 $set1 中以 $set2 中第一个节点的值开头的节点.

If you give it a node-set as one of its arguments, it uses the string value of the node-set, which is the text value of the first node in the set. So in the above, $set2 never gets searched; only the first node in the list ever gets examined, and so the predicate will only find nodes in $set1 that start with the value of the first node in $set2.

我被误导了,因为这种模式(我在过去几天中使用了很多)确实有效:

I was misled because this pattern (which I've been using a lot in the last few days) does work:

<xsl:variable name="hits" select="$set1[. = $set2]"/>

但该谓词使用的是节点集之间的比较,而不是文本值之间的比较.

But that predicate is using an comparison between node-sets, not between text values.

执行此操作的理想方法是嵌套谓词.也就是说,我想在 $set1 中找到每个节点,在 $set2 中有一个节点的值以...开头",这就是 XPath 崩溃的地方.从什么开始?你想写的东西是这样的:

The ideal way to do this would be by nesting predicates. That is, "I want to find every node in $set1 for which there's a node in $set2 whose value starts with..." and here's where XPath breaks down. Starts with what? What you'd like to write is something like:

<xsl:variable name="matches" select="$set1[$set2[starts-with(?, .)]]"/>

只有没有您可以为 ? 编写的表达式来返回当前由外部谓词测试的节点.(除非我遗漏了一些非常明显的东西.)

only there's no expression you can write for the ? that will return the node currently being tested by the outer predicate. (Unless I'm missing something blindingly obvious.)

为了得到你想要的,你必须单独测试每个节点:

To get what you want, you have to test each node individually:

<xsl:variable name="matches">
  <xsl:for-each select="$set1">
    <xsl:if test="$set2[starts-with(current(), .)]">
      <xsl:copy-of select="."/>
    </xsl:if>
  </xsl:for-each>
</xsl:variable>

这不是一个非常令人满意的解决方案,因为它评估的是结果树片段,而不是节点集.如果要在 XPath 表达式中使用变量,则必须使用扩展函数(如 msxsl:node-set)将 RTF 转换为节点集.

That's not a very satisfying solution because it evaluates to a result tree fragment, not a node-set. You'll have to use an extension function (like msxsl:node-set) to convert the RTF to a node-set if you want to use the variable in an XPath expression.

这篇关于XPATH 或 XSL 使用自定义比较来匹配两个节点集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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