1

我有这部分代码,我一直在使用它来根据搜索的名称过滤掉字段。问题是我需要它对用户可以输入的内容有点宽容。所以我必须使用该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 还是很陌生,所以如果可能的话,我需要稍微简化一下。


回复迈克尔·凯-

嗨,我正在尝试将搜索到的术语/单词应用于 XSLT 表中的作者字段。是的,你是对的,这就是我想要做的。编辑样式表并在输入表单中应用条件集。到目前为止,我一直在使用预构建的功能。根据需要进行修改。如果对要搜索的输入值进行硬编码,我可以使包含在外部工作。但是,我想按原样“即时​​”执行此操作,并且我真的不确定如何在我所追求的环境中使其工作。我实际上拥有的是在表单中构建的查询 - 指定要搜索的字段、操作数和搜索词。但我不想搜索整个字符串,因为这有点不灵活 - 所以我想使用 contains 函数来允许搜索灵活性。

谢谢你的帮助

4

3 回答 3

1

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.

于 2011-12-19T09:53:52.640 回答
0

您通常可以使用样式表参数来实现您想要的:查找 xsl:param。如果您想提供的不仅仅是一个搜索值作为参数(例如,如果搜索词的数量是可变的),那么有时最好的解决方案是调用 xx:evaluate() - 许多 XSLT 处理器都有一个扩展,允许XPath 表达式作为要动态构造和评估的字符串提供。最后,在执行之前修改样式表是一种选择:不是最简单的,但有时是正确的答案。定位要修改的元素的最简单、最灵活的方法是给它一个 xml:id 属性:

<xsl:for-each select="...." xml:id="point-of-change">
  ...
</xsl:for-each>
于 2011-12-21T10:44:18.760 回答
0

如果要评估 XPath 表达式的 XSLT 样式表包含以下片段

   <xsl:for-each select=
    "book[contains(author, ZZZZZ)]">

     <!-- Code here. -->
    </xsl:for-each>

那么一个选择这个xsl:for-each元素(并且如果它们存在也可以选择其他这样的元素)的 XPath 表达式是:

//xsl:for-each
      [
       @select = 'book[contains(author, ZZZZZ)]'
      ]

基于 XSLT 的验证

当进行以下变换时:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes"/>

 <xsl:template match="/">
  <xsl:copy-of select=
  "//xsl:for-each
      [@select = 'book[contains(author, ZZZZZ)]'
      ]
  "/>
 </xsl:template>
</xsl:stylesheet>

应用于此 XML 文档

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
   <xsl:for-each select=
    "book[contains(author, ZZZZZ)]">

     <!-- Code here. -->
    </xsl:for-each>
 </xsl:template>

</xsl:stylesheet>

它选择想要的元素并将其作为结果输出

<xsl:for-each xmlns:xsl="http://www.w3.org/1999/XSL/Transform" select="book[contains(author, ZZZZZ)]">

     <!-- Code here. -->
    </xsl:for-each>
于 2011-12-19T14:22:21.753 回答