Understanding X Path

 Understanding X PathIf you are going to work with XML, you will need to understand Xpath.  Xpath is the syntax by which elements or data inside an XML file are referenced.  Parsers and XSLT use Xpath so that they can find the data that needs to be parsed or transformed.  If you understand the fundamentals of XML and how it is formed, understanding Xpath is a logical next step.  This is a brief explanation of how Xpath works and how it is used.

Xpath

Xpath is positional in its syntax.  This means that an Xpath expressions walks the parser from node to sub-node to the point where the data is located.  Xpath expressions can look like this $data = /header/doc/date so that the parser knows that whatever is found in the relational location is assigned to the variable $date.

Here is an example XML file that I made up for this discussion:

<?XML>
<Header>
<Doc>
<Date>September 1, 2004</Date>
<Time>11:00:00</Time>
<Sender>
<Ident>999999999</Ident>
</Sender>
<Receiver>
<Ident>111111111</Ident>
</Receiver>
</Doc>
<PayloadID>1481094801384021384</PayloadID>
</Header>
<Body>
<LineItem>
<LineNumber>1</LineNumber>
<PartNumber>123ABC</PartNumber>
<Price>1.00</Price>
</LineItem>
<LineItem>
<LineNumber>2</LineNumber>
<PartNumber>!VC456</PartNumber>
<Price>2.00</Price>
</LineItem>
<LineItem>
<LineNumber>3</LineNumber>
<PartNumber>8675309</PartNumber>
<Price>4.00</Price>
</LineItem>
</Body>
</XML>

Using this example, we can illustrate using Xpath with and expression that says, “Total of item prices” and we can say that with Xpath like this, “$total = sum(/XML/Body/LineItem/Price)” where each value fitting the path, will be totalled and then assigned to the “$total” variable.  In our example file this would be 7.00.  (Those familiar with XML will notice that none of my nodes have any attributes.  This is just the beginning.  And those with no familiarity with XML will still notice that our price has no curency.  This would normally be held in an attribute.)

Sounds Easy

This is supposed to be an easy example.  We haven’t talked about attributes where some tags will not just delimit data, but contain it.  (for example what if we changed the schema of the line to look like this. “<LineItem LineNumber=”"> and thus eliminated the need for the <LineNumber> tagged value?  How does this help us use XM?)  And we haven’t talked about DTD or Schema files that help a parser understand the XML file layout.  And we haven’t talked about validation, or scope, or a few other nice and powerful parts of XML that you will run into.  (Maybe next time.)

Subscribe to "The Integration Engineer" by Email
Find out about the tools and services available at The Integration Engineer's Consulting site.

Related Articles:

    Leave a Reply