如何在 document.evaluate() 中使用 XPath contains() 函数?
我有这部分代码,我一直用它来根据搜索的名称过滤字段。问题是我需要它对用户可以输入的内容有一点宽容。所以我必须使用 contains
函数。我已经让它在其他代码片段上工作,但不确定如何将它与以下代码一起使用:
var value = stylesheet.evaluate(
"//xsl:for-each[@select='book[author="contains(.,'containsSearchTerm')"']",
stylesheet, nsResolver,
XPathResult.ANY_UNORDERED_NODE_TYPE, null);
抱歉,如果引号/转义有点混乱。我没有打开 IDE 来检查我的错误,我正准备开始工作。
所以,也许可以看到 containsSearchTerm 是我想要获取表单值并使用 contains
函数处理它的内容。这是如何实现的?
请注意,我对 XML、XSLT 和 XPath 总体来说还很陌生,因此如果可能的话,需要进行一些简化。
回复 Michael Kay -
您好,我正在尝试将搜索到的术语/单词应用到 XSLT 表中的作者字段。 是的,你是对的,这就是我正在努力做的。编辑样式表并应用输入表单中设置的条件。 到目前为止我一直在使用预构建的函数。按要求修改。 如果对要搜索的输入的值进行硬编码,我可以使包含在外部工作。然而,我想像以前一样“即时”地完成它,并且我真的不确定如何在我所追求的上下文中使其工作。 我实际上拥有的是在表单中构建的查询 - 指定要搜索的字段、操作数和搜索词。但我不想搜索整个字符串,因为这有点不灵活 - 所以我想使用 contains 函数来允许搜索灵活性。
谢谢你的帮助
I have this section of code which I've been using to filter out fields based on a searched name. The problem is that I need it to be a little forgiving in terms of what the user can type. So I have to use the contains
function. I have gotten it working on other code snippets but am not sure how I can use it with the following:
var value = stylesheet.evaluate(
"//xsl:for-each[@select='book[author="contains(.,'containsSearchTerm')"']",
stylesheet, nsResolver,
XPathResult.ANY_UNORDERED_NODE_TYPE, null);
Sorry if the quotes/escaping is a little messed up. I don't have an IDE open to check my errors and I'm getting ready for work.
SO as might be able to see the containsSearchTerm is what I want to take the value of a form and process it using the contains
function. How is this achieved?
Please be aware I'm pretty new to XML, XSLT and XPath in general so will need a little simplification if at all possible.
Reply to Michael Kay -
Hi, I'm trying to apply a searched term/word to the authors field within a XSLT table.
Yes you are right that is what I'm trying to do. Edit the stylesheet and apply the condition set within an input form.
I've been using a prebuilt function thus far. Modifying as required.
I can make the contain work externally if hard-coding the value for the input to search for. However I want to do it 'on-the-fly' as it were and am really just not sure how to get this working in the context I'm after.
What I actually have is the query being build within the form - specifying the field to search, operand and search term. But I don't want to search for whole strings because thats a little inflexible - so I wanted to use the contains function to allow search flexibility.
Thank you for you help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您是 XML 和 XSLT 的新手,因此最好确认您不会陷入困境。您正在使用 XPath 来搜索 XSLT 样式表。这是一件非常先进/雄心勃勃的事情,并且很高兴知道它在您的应用程序上下文中有意义。那么你想解决什么问题呢?
至于细节,我必须通过对语法不正确的代码进行逆向工程来猜测你的意图,这总是很困难。我想您可能正在寻找一个 xsl:for-each 指令,其 select 属性具有一般形式
book[author = ....]
并且您想对“....”是什么进行一些限制,但超出我真的不知道你在做什么。另外,如果您想“宽容”,那么您可能希望在可能出现空格的地方允许空格等。正则表达式匹配可能会满足您的需求(正则表达式在 XPath 2.0 中可用),但是我实在说不出来。
Since you're new to XML and XSLT, it would be nice to confirm that you aren't going down a rabbit-hole here. You are using XPath to search an XSLT stylesheet. That's a pretty advanced/ambitious thing to do, and it would be nice to know that it's something that makes sense in the context of your application. So what problem are you trying to solve?
As for the detail, I'm having to guess your intent by reverse-engineering syntactically-incorrect code, which is always difficult. I imagine you're probably looking for an xsl:for-each instruction whose select attribute has the general form
book[author = ....]
and you want to place some constraints on what "...." is, but beyond that I can't really tell what you are doing. Also, if you want to be 'forgiving', then you presumably want to allow whitespace in the places where whitespace can appear, etc. It might be that a regular expression match will meet your needs (regexes are available in XPath 2.0), but I really can't tell.
如果您要评估 XPath 表达式的 XSLT 样式表包含此片段:
然后是一个选择此
xsl:for-each
元素的 XPath 表达式 strong>(并且可以选择其他此类元素(如果存在))是:基于 XSLT 的验证:
当以下转换:
应用于此 XML 文档时:
it选择想要的元素并将其输出为结果:
If the XSLT stylesheet against which you want to evaluate an XPath expression, contains this fragment:
Then one XPath expression that selects this
xsl:for-each
element (and may select other such elements if they exist) is:XSLT - based verification:
When the following transformation:
is applied on this XML document:
it selects the wanted element and outputs it as the result:
您通常可以使用样式表参数来实现您想要的目的:查找 xsl:param。如果您想要提供的不仅仅是一个搜索值作为参数(例如,如果搜索项的数量是可变的),那么有时最好的解决方案是 xx:evaluate() 调用 - 许多 XSLT 处理器都有一个扩展,允许XPath 表达式作为字符串提供,以便动态构造和计算。最后,在执行之前修改样式表是一种选择:不是最简单的,但有时是正确的答案。定位要修改的元素的最简单、最灵活的方法是为其指定 xml:id 属性:
You can often achieve what you want using stylesheet parameters: look up xsl:param. If you want to supply more than just a search value as a parameter (for example if the number of search terms is variable), then sometimes the best solution is an xx:evaluate() call - many XSLT processors have an extension that allows an XPath expression supplied as a string to be dynamically constructed and evaluated. Finally, modifying the stylesheet before execution is an option: not the easiest, but sometimes the right answer. The simplest and most flexible way to locate the element that you want to modify is to give it an xml:id attribute: