>XPath and Web Tools DOM API

>While working on the the XSL Tooling project’s content assist support, I learned (thanks to Doug Satchwell’s excellent XPath View in 0.5m5) that the WTP DOM (Document Object Model) implementation is robust enough to run XPath’s against. The WTP DOM implements the DOM Level 1 API and some of the DOM Level 2 API, but for some reason I thought DOM Level 3 was required, but apparently it isn’t.

The reason this is important, is because it opens up a time saving way to working with the StructuredDocument’s in WTP. Particularly those documents that are markup based like HTML, XHTML, and XML related documents. Instead of retrieving a DOM representation and tediously working your way through the document, one can use XPath statements to run against the Nodes within the document. The statements can be run at any Node level and doesn’t have to be the Document’s root node.

Here’s a link to a decent tutorial, this is currently updated to XPath 2.0, but most of the 1.0 items are still relevant and can be used with Xalan. XPath allows for the returning of a single node, or a group of nodes based on the XPath expression. A program then can work just on those nodes.

XPath Example:


The above XPath statement would return all the globally defined XSL variable elements in an XSL stylesheet. The nodeset that is returned can then be manipulated as necessary. Without XPath, the DOM would need to be retrieved, and walked, pulling out the nodes for the entire document. If anybody has written DOM related code, you know it can be tedious trying to get something buried far down in an XML document. With XPath, the amount of code you need to write is limited to just the code you need to use to manipulate the document. Think of it as SQL for XML documents. In fact, XPath 2.0 is the basis for both XQuery 1.0 and XSLT 2.0. Most java implementations including the JAXP default implementation are limited to XPath 1.0.

Executing the above statement is as simple as calling Xalan’s XPathAPI statement:

NodeList nodes = XPathAPI.selectNodeList(node, "xsl:stylesheet/xsl:variable")

You can then use the methods provided on the NodeList to iterate through the list of returned values.

XPath is a powerful thing, and with Xalan now in Orbit, it’s easily available for all eclipse projects to use. XPath is also supported by JAXP in JDK 1.5. Some more experimentation is needed to see if manipulation and updates of the nodes returned can be done and have the information appear in the StructuredDocument being manipulated, but at the very least, there is a standard way to query against the StructuredDocument.

This entry was posted in eclipse, xml. 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