membersound
membersound

Reputation: 86847

Xpath matches with single quotes?

How can I assert an xpath match that contains single quotes within the string to be asserted?

This is my string with value '40' to be asserted.

I assumed to escape the single quote characters with \' but that does not work.

matches( //faultstring[1]/text(), 'This is my string with value \'40\' to be asserted.' )

How is this done properly?

Upvotes: 2

Views: 1895

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243519

My understanding of this question is that the problem is caused by the need to use nested quotes if the XPath expression is within an XML document.

If this is the case, one can use this XPath expression:

$yourString = "This is my string with value '40' to be asserted."

XSLT - based verification:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
     <xsl:copy-of select=
     "/* = &quot;This is my string with value &apos;40&apos; to be asserted.&quot;"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<t>This is my string with value '40' to be asserted.</t>

the XPath expression is evaluated and the result of this evaluation is copied to the output:

true

Upvotes: 1

Cylian
Cylian

Reputation: 11182

Try this

//faultstring[matches(text(),'&#x0027;')]

or

//faultstring[matches(text(),'&apos;')]

or

//faultstring[matches(text(),'&#39;')]

For a more elegant solution see this post

Upvotes: 1

Related Questions