FluentDOM Usage: The id() Problem

There is a node-set function called id() in xpath. It might be the case that it does not work on your data if you load in HTML documents into FluentDOM / DOM-XML. The result is that you do not get any result, no nodes are returned by the find() function. That’s a pitty because you can use it to access elements in a short notation. There are two ways to deal with the problem:

  1. Give it a doctype: That’s what missing here to switch XPATH into XML mode or something like that. It’s enough that you add “<!DOCTYPE>” in front of the HTML string before loading it into FluentDOM.
  2. Use the predicate: Just query the elements by their id attribute value: //*[@id="content"] replaces pretty well id("content")

You can find out more about FluentDOM on a previous Blog-Post of mine.

This entry was posted in Hakre's Tips and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s