0

I want to extract the links from this HTML file using Objective-C.

<html>
 <head>
  <base href='http://example.com/' />
  <title>Example website</title>
 </head>
 <body>
  <div id='images'>
   <a href='image1.html'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>
   <a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
   <a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
   <a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>
   <a href='image5.html'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>
  </div>
 </body>
</html>

HEre is the Xpath Query:

//a[contains(@href, "image")]/@href

But the problem here is that it errors on Quotation marks around image text in query!

Error

2 Answers 2

2

You'll have to escape the double quotes to use them inside a quoted string:

@"//a[contains(@href,\"image\")]/@href"

Or use single quotes, if XPath supports that:

@"//a[contains(@href,'image')]/@href"
Sign up to request clarification or add additional context in comments.

2 Comments

I TRIED YOUR both queries! it runs without error now, but it does not return the result, it return null!
How so? Does it say (null)? I don't know enough about XPath to verify your query.
1

Since the XPath expression is delimited by double quote characters, you cannot use these characters inside of it. You'll have to switch to single quotes instead:

//a[contains(@href, 'image')]/@href

1 Comment

I TRIED YOUR both queries! it runs without error now, but it does not return the result, it return null!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.