<?xml version="1.0" encoding="UTF-8"?>
<entry>
  <author-id type="integer">1</author-id>
  <body>This evening, I took my first dive into XPath, and I have to say, navigating XML documents using JSTL tags and XPath is incredibly easy.&lt;br/&gt;
&lt;br/&gt;
The reason I had to parse an XML document is because one of the features I added for Argus 0.9 was a system that would allow an administrator to upgrade the database easily when deploying a new version of the software. After a successful upgrade, I displayed a list of new features, improvements and bug fixes for that version. What I had envisioned though, is dynamically generating this list, from an XML file.&lt;br/&gt;
&lt;br/&gt;
So how do you use JSTL and XPath to parse your XML document? Well, first off, you have to import and parse your XML file:&lt;br/&gt;

&lt;blockquote&gt;
&lt;code&gt;
&amp;lt;c:import url=&quot;changelog.xml&quot; var=&quot;url&quot;/&amp;gt;&lt;br /&gt;
&amp;lt;x:parse xml=&quot;${url}&quot; var=&quot;changelog&quot;/&amp;gt;
&lt;/code&gt;
&lt;/blockquote&gt;

After you do this, you now have a parsed XML document that you can start selecting elements from. Let's take a section of my changelog XML to work with.&lt;br/&gt;

&lt;blockquote&gt;&lt;div&gt;&lt;code&gt;&amp;lt;log&amp;gt;&lt;/code&gt;&lt;/div&gt;&lt;div style=&quot;margin-left:10px;&quot;&gt;&lt;code&gt;&amp;lt;changes version=&quot;0.9.2&quot;&amp;gt;&lt;/code&gt;&lt;/div&gt;&lt;div style=&quot;margin-left:20px;&quot;&gt;&lt;code&gt;&amp;lt;fix&gt;Fixed a changelog email bug while trying to load users with permission.&amp;lt;/fix&amp;gt;&lt;/code&gt;&lt;/div&gt;&lt;div style=&quot;margin-left:10px;&quot;&gt;&lt;code&gt;&amp;lt;/changes&amp;gt;&lt;/code&gt;&lt;/div&gt;
&lt;div style=&quot;margin-left:10px;&quot;&gt;&lt;code&gt;&amp;lt;changes version=&quot;0.9.1&quot;&amp;gt;&lt;/code&gt;&lt;/div&gt;
&lt;div style=&quot;margin-left:20px;&quot;&gt;&lt;code&gt;&amp;lt;fix&gt;Hid the 'Assigned to me' scope for regular users.&amp;lt;/fix&amp;gt;&lt;/code&gt;&lt;/div&gt;
&lt;div style=&quot;margin-left:10px;&quot;&gt;&lt;code&gt;&amp;lt;/changes&amp;gt;&lt;/code&gt;&lt;/div&gt;
&lt;div style=&quot;margin-left:10px;&quot;&gt;&lt;code&gt;&amp;lt;changes version=&quot;0.9&quot;&amp;gt;&lt;/code&gt;&lt;/div&gt;
&lt;div style=&quot;margin-left:20px;&quot;&gt;&lt;code&gt;&amp;lt;feature&amp;gt;Upgrade script that can upgrade the database from any older version to the current version.&amp;lt;/feature&amp;gt;&lt;/code&gt;&lt;/div&gt;
&lt;div style=&quot;margin-left:20px;&quot;&gt;&lt;code&gt;&amp;lt;feature&amp;gt;Personalized ticket scopes added to the filter bar.&amp;lt;/feature&amp;gt;&lt;/code&gt;&lt;/div&gt;
&lt;div style=&quot;margin-left:20px;&quot;&gt;&lt;code&gt;&amp;lt;improvement&amp;gt;'Revisions' renamed as 'Versions'.&amp;lt;/improvement&amp;gt;&lt;/code&gt;&lt;/div&gt;
&lt;div style=&quot;margin-left:20px;&quot;&gt;&lt;code&gt;&amp;lt;improvement&amp;gt;Revision live and beta abandoned for simple 'released' flag.&amp;lt;/improvement&amp;gt;&lt;/code&gt;&lt;/div&gt;
&lt;div style=&quot;margin-left:20px;&quot;&gt;&lt;code&gt;&amp;lt;improvement&amp;gt;Ability to manually set the sort order for Versions.&amp;lt;/improvement&amp;gt;&lt;/code&gt;&lt;/div&gt;
&lt;div style=&quot;margin-left:20px;&quot;&gt;&lt;code&gt;&amp;lt;improvement&amp;gt;Ability to remove the version selected for a ticket.&amp;lt;/improvement&amp;gt;&lt;/code&gt;&lt;/div&gt;
&lt;div style=&quot;margin-left:20px;&quot;&gt;&lt;code&gt;&amp;lt;fix&amp;gt;Ticket description no longer gets truncated during an update.&amp;lt;/fix&amp;gt;&lt;/code&gt;&lt;/div&gt;
&lt;div style=&quot;margin-left:10px;&quot;&gt;&lt;code&gt;&amp;lt;/changes&amp;gt&lt;/code&gt;&lt;/div&gt;
&lt;div&gt;&lt;code&gt;&amp;lt;/log&amp;gt;&lt;/code&gt;&lt;/div&gt;
&lt;/blockquote&gt;

If you want to iterate over all of the the &lt;code&gt;changes&lt;/code&gt; elements, you can use the x:forEach tag:&lt;br/&gt;

&lt;blockquote&gt;
&lt;code&gt;
&amp;lt;x:forEach select=&quot;$changelog//changes&quot;&amp;gt;
&lt;/code&gt;
&lt;/blockquote&gt;

The key to using XPath syntax, though, is to know where you are in the XML document's DOM. For example, during each iteration, the top level object is a &lt;code&gt;changes&lt;/code&gt; element. So your XPath syntax should be relative to that object. Let's say that the first thing we want to do is display the version attribute of the current &lt;code&gt;changes&lt;/code&gt; element. We would do:&lt;br/&gt;

&lt;blockquote&gt;
&lt;div style=&quot;margin-left:10px;&quot;&gt;&lt;code&gt;&amp;lt;x:out select=&quot;@version&quot;/&amp;gt;&lt;/code&gt;&lt;/div&gt;
&lt;/blockquote&gt;

Now, what if wanted to iterate over all &lt;code&gt;feature&lt;/code&gt; elements that are children of this &lt;code&gt;changes&lt;/code&gt; element? Well, we could do something like this:&lt;br/&gt;

&lt;blockquote&gt;
&lt;div style=&quot;margin-left:10px;&quot;&gt;&lt;code&gt;&amp;lt;x:forEach select=&quot;./feature&quot;&amp;gt;&lt;/code&gt;&lt;/div&gt;
&lt;/blockquote&gt;

As you can see, the XPath syntax is pretty straightforward and if you are familiar with navigating a UNIX based operating system, you will see some similarities. A '.' is used to denote the current element and a slash and then an element name means all elements with that name that are children of this node. While inside this x:forEach tag, remember that the current element will be a &lt;code&gt;feature&lt;/code&gt; element. If you want to display the contents of each &lt;code&gt;feature&lt;/code&gt; element, you would again use x:out like this:&lt;br/&gt;

&lt;blockquote&gt;&lt;div style=&quot;margin-left:20px;&quot;&gt;&lt;code&gt;&amp;lt;x:out select=&quot;.&quot;/&amp;gt;&lt;/code&gt;&lt;/div&gt;&lt;/blockquote&gt;

So, why don't you give it a try. But, don't forget that you'll need to import &lt;code&gt;&amp;lt;%@ taglib prefix=&quot;x&quot; uri=&quot;http://java.sun.com/jsp/jstl/xml&quot; %&amp;gt;&lt;/code&gt; and have the JSTL jar in your project.</body>
  <created-at type="datetime">2007-08-14T17:04:15-04:00</created-at>
  <excerpt>This evening, I took my first dive into XPath, and I have to say, navigating XML documents using JSTL tags and XPath is incredibly easy.</excerpt>
  <id type="integer">1250</id>
  <permalink>2006/06/03/jstl-and-xpath-3</permalink>
  <published-at type="datetime">2006-06-04T00:53:00-04:00</published-at>
  <slug>jstl-and-xpath-3</slug>
  <title>JSTL and XPath</title>
  <tumblr-permalink nil="true"></tumblr-permalink>
</entry>
