如何在 XPath 中执行不区分大小写的搜索?

How to perform case insensitive search in XPath?

我正在尝试使用 XPath 实现不区分大小写的搜索。 我已经提到 how to perform a case-insensitive attribute selector in xquery 所以请在标记为重复之前检查。我正在使用 Lcase 将我的变量 (L_search) 转换为小写和小写函数。

我原来区分大小写的 XPath 表达式是:

XPath       =  "//*[contains(., '"& search &"')]/ancestor-or-self::*/*[local-name()='home' and @locale='en']"

我尝试了很多组合,例如:

XPath       =  "//*lower-case([contains(., '"& L_search &"')])/ancestor-or-self::*/*[local-name()='home' and @locale='en']"

XPath       =  "//*[contains(lower-case(.), '"& L_search &"')])/ancestor-or-self::*/*[local-name()='home' and @locale='en']"

但是其中 none 个正在产生结果。

这是我的代码 运行:

Sub ProcessFolder(FolderPath)
    On Error Resume Next
    Set fldr = fso.GetFolder(FolderPath)

    Set Fls = fldr.files
    For Each thing in Fls
            sFSpec = FSO.GetAbsolutePathName(thing)
            objMSXML.async = True
            objMSXML.load sFSpec
             If 0 = objMSXML.parseError Then
                Dim sXPath   : sXPath       =  "//*[contains(., '"& search &"')]/ancestor-or-self::*/*[local-name()='name' and @locale='en']"

                Dim querySubject : Set querySubject = objMSXML.selectSingleNode(sXPath)
                    Set p = document.createElement("p")
                        p.innerText = thing.Path
                        document.body.appendChild p
                    If querySubject Is Nothing Then
                        MsgBox sXPath, "failed"

XPath 2.0

如果您使用不区分大小写的 matches(),

"//*[matches(., '"& search &"', 'i')]/ancestor-or-self::*/*[local-name()='home' and @locale='en']"

您无需担心 search 变量的大小写。


另请参阅 case insensitive xpath contains() possible? 了解其他 XPath 1.0 和 2.0 解决方案。

VBScript 仅支持 XPath 1.0,不支持 XQuery,因此请先编辑您的问题标题。

在 XPath 1.0 中,translate() 函数用于不区分大小写。

//*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') , search)]/ancestor-or-self::*/*[local-name()='home' and @locale='en']

Where search = Lcase(V_SAEARCH)

它将完美运行。无需在变量周围使用引号。

另一种写法是:-

//*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') , translate('" & search & "', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))]/ancestor-or-self::*/*[local-name()='home' and @locale='en']

这里正在用 XPath 翻译搜索变量。