There is no quote escaping in XPath string literals. (Note: This answer applies to XPath 1.0. In higher versions of XPath, this issue is addressed - see the comment below.)
The only way to get the desired result in pure XPath is by concatenating alternately-quoted strings.
//label[contains(., concat('Ayuntamiento de la Vall d', "'", 'Uixó - Festivales Musix'))]
You can build these kinds of expressions mechanically by splitting the target string at the single quote and joining the parts again with ', "'" , '
as the new separator. Python example:
search_value = "Ayuntamiento de la Vall d'Uixó - Festivales Musix" # could contain both " and '
xpath = "//label[contains(., %s)]" % xpath_string_escape(search_value)
def xpath_string_escape(input_str):
""" creates a concatenation of alternately-quoted strings that is always a valid XPath expression """
parts = input_str.split("'")
return "concat('" + "', \"'\" , '".join(parts) + "', '')"
Some XPath libraries support bound parameters (much like SQL) to get around this, but the above is the only approach that works everywhere.