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

时间:2016-04-04 15:35:57

标签: xml xpath vbscript msxml

我正在尝试使用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']"

但他们都没有产生结果。

这是我正在运行的代码:

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"

2 个答案:

答案 0 :(得分:5)

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

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

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

搜索= Lcase(V_SAEARCH)

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

另一种写这个的方法是: -

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

此处搜索变量正在XPath中翻译。

答案 1 :(得分:2)

XPath 2.0

If you use case-insensitive matches(),

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

you won't need to worry about the case of your search variable.


See also case insensitive xpath contains() possible? for other XPath 1.0 and 2.0 solutions.

相关问题