Encoding XPath Expressions with both single and double quotes

前端 未结 8 1182
孤街浪徒
孤街浪徒 2020-11-30 07:21

XPath (v1) contains no way to encode expressions.

If you only have single OR double quotes then you can use expressions such as

//review[@name=\"Bob         


        
8条回答
  •  自闭症患者
    2020-11-30 07:59

    I've had problems with all solutions so far. One has extra text sections (e.g. '"' or "'") which breaks what you're looking for. One drops all text after the last quote/dblquote which breaks as well.

    This is a dumb and quick solution from a dumb vb developer:

    Function ParseXpathString(ByVal input As String) As String
        input = Replace(input, "'", Chr(1))
        input = Replace(input, """", Chr(2))
        input = Replace(input, Chr(1), "',""'"",'")
        input = Replace(input, Chr(2), "','""','")
        input = "concat('','" + input + "')"
        Return input
    End Function
    

    Usage (same as previous examples):

    x.SelectNodes("/path[@attr=" & ParseXpathString(attrvalue) & "]")
    

提交回复
热议问题