1

How can we check value for value inside node retun i.e

Hurricane's Grill Darling Harbour

Actually, i am not able to pick apostrophe sign with below code exists(//ns1:name[text()='Hurricane's Grill Darling Harbour']).

Getting error message: RuntimeException:net.sf.saxon.trans.XPathException: XPath syntax error at char 33 on line 2 in {...name[text()='Hurricane's Gr...}: expected "]", found name "s"

aman
  • 11
  • 4
  • 2
    Possible duplicate of [How to select element that contains single quotes using XPath?](http://stackoverflow.com/questions/37403236/how-to-select-element-that-contains-single-quotes-using-xpath) – kjhughes Oct 06 '16 at 16:31
  • @kjhughes, The answers to the question cited as "possible duplicate" do not include the simplest XPath 2.0 solution, which is valid in any case, even when the string contains both types of quotes and the XPath expression is not embedded inside an XML document -- in which case builtin XML character entity references are not recognized and cause XPath parse errors. Therefore, please, revert your vote to close the question. – Dimitre Novatchev Oct 09 '16 at 18:16
  • 1
    @DimitreNovatchev: I'll gladly retract my duplicate vote in support of your fine and thoroughly comprehensive answer provided here. – kjhughes Oct 09 '16 at 18:38
  • @kjhughes Yhank you! – Dimitre Novatchev Oct 09 '16 at 19:04

2 Answers2

2

i am not able to pick apostrophe sign with below code exists(//ns1:name[text()='Hurricane's Grill Darling Harbour']).

Getting error message: RuntimeException:net.sf.saxon.trans.XPathException: XPath syntax error at char 33 on line 2 in {...name[text()='Hurricane's Gr...}: expected "]", found name "s

In XPath 2.0 an apostrophe or a double quote can be escaped by simply doubling that character. See rules [75] and [76] here: http://www.w3.org/TR/xpath20/#terminal-symbols

Use:

exists(//ns1:name[text()='Hurricane''s Grill Darling Harbour'])

XSLT 2.0 - based verification:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ns1="ns1">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/">
    <xsl:sequence 
         select="exists(//ns1:name[text()='Hurricane''s Grill Darling Harbour'])"/>
  </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<ns1:name xmlns:ns1="ns1">Hurricane's Grill Darling Harbour</ns1:name>

the wanted, correct result is produced:

true

In case you only can use an XPath 1.0 expression within an XML document and the string contains both kinds of quotes, use:

boolean(//ns1:name
   [text()=concat('Hurricane &quot;Mathew&quot;',  &quot; strength&apos;s value&quot;)])

Here is a full XSLT 1.0 - based verification:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ns1="ns1">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/">
    <xsl:value-of select=
      "boolean(//ns1:name
  [text()=concat('Hurricane &quot;Mathew&quot;',  &quot; strength&apos;s value&quot;)])"/>
  </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<ns1:name xmlns:ns1="ns1">Hurricane "Mathew" strength's value</ns1:name>

the wanted, correct result is produced:

true
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • @albciff, You are welcome! And your answer adds more from the SoapUI side, which I am not a specialist in, though recently I showed my teammates how to generate automatically thousands of tests, using an XSLT tool. – Dimitre Novatchev Oct 07 '16 at 14:02
2

Saxon starts to support some of the XPath 2.0 requeriments in 7.0 version.

SOAPUI version 5.1.2 use Saxon 9.1 version (Saxon change history for new version can be viewed here)

Then as @DimitreNovatchev perfectly explain, a possible option to escape ' for XPath 2.0 in text is to use double the symbol as '':

exists(//ns1:name[text()='Hurricane''s Grill Darling Harbour'])

I only want to add another possibility; since " quotes are also valid you can wrap the whole text with " an use ' inside. So in your SOAPUI XPath match assertion you can use:

exists(//ns1:name[text()="Hurricane's Grill Darling Harbour"])

Note that I purpose this way, since I'm assuming that you've to use only ' in your XPath finding node texts. If you need to use also " inside your the text wrapped with " you've to double the " as happens with '.

Additionally note that in SOAPUI it's possible to use * as a wildcard for namespaces so you can reduce your expression:

declare namespace ns='http://somenamespace/';
exists(//ns1:name[text()="Hurricane's Grill Darling Harbour"])

To:

exists(//*:name[text()="Hurricane's Grill Darling Harbour"])
albciff
  • 18,112
  • 4
  • 64
  • 89
  • albcif, I upvoted this answer. However, it cannot be applied in case the string contains both an apostrophe and a double quote. – Dimitre Novatchev Oct 08 '16 at 03:44
  • @DimitreNovatchev I assume that the OP only has `'` in the node text which he want to get with *XPath*, maybe I did a bad assumption. So I edit the answer to add a note explaining this behavior. Thanks :) – albciff Oct 10 '16 at 06:54