Forum Discussion

jack_edwards's avatar
jack_edwards
Occasional Contributor
9 years ago

Using FindChildByXPath with apostrophe in XPath

TC11 Documentation:  https://support.smartbear.com/viewarticle/72332/

 

Hello,

 

We have used FindChildByXPath successfully in the past, but are having an issue when the XPath contains an apostrophe.  We are using VBScript.

 

Example:

Function FindIcon
    Dim Field, Value, objField, TempStr, icon

    Field = "Aliases.Parent.Object"
    Value = "This doesn't work"

    Set objField = Eval(Field)

    TempStr = "//*[@title='" & Value & "']"

    Set icon = objField.FindChildByXPath(TempStr, False) 'Exception occurs!

    'More code here...

End Function

 

I'm pretty sure the issue is caused by how we format our "TempStr".  We use apostrophes to wrap the value of the element's Title, so including an apostrophe within our "Value" throws things off.  (The apostrophes are not wrapping the value - sorry for the confusion.)  Is there a better or alternate way to create the "TempStr" that will allow us to include apostrophes?  Is there a way to just ignore the apostrophe and still have the majority of the initial "Value" be searched for?

 

Edit:  I've also tried using Replace() to find and replace the apostrophe with "?", "*", and "'" (minus the double quotes), but no luck.  Again, I think one of these options may work, but not with how we are currently formatting the "TempStr".

  • Hi Jack,

     

    If a value contains apostrophes, you need to wrap it in quotes instead of apostrophes:

    Value = "A Midsummer Night's Dream"
    TempStr = "//*[@title=" & Chr(34) & Value & Chr(34) & "]"
    
    ' This is the same as TempStr = "//*[@title=""A Midsummer Night's Dream""]"
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Hi Jack,

     

    If a value contains apostrophes, you need to wrap it in quotes instead of apostrophes:

    Value = "A Midsummer Night's Dream"
    TempStr = "//*[@title=" & Chr(34) & Value & Chr(34) & "]"
    
    ' This is the same as TempStr = "//*[@title=""A Midsummer Night's Dream""]"