IBM Support

How to use XPath with namespaces in RIT

Question & Answer


Question

For most XPath usage in RIT a basic knowledge of XPath will suffice, but commonly knowledge of how to navigate with a namespace is also required.

Answer

When not to use XPath in RIT

IBM Rational Integration Tester (RIT) has a field editor in which many simple validations can be configured, including identity, expression and RegEx. The use of XPath in the field editor should normally be restricted to those instances where these other choices are not suitable.

A basic knowledge of XPath

It is assumed that the reader has at least a basic working knowledge of XPath. There are many good books on this subject, and many books on the topic of XSLT will have a chapter or two covering the XPath syntax. There are also good resources available on line including "Get started with XPath" on the IBM developerWorks site.

Whilst most tutorials do not include namespaces in their example XML most real world XML messages (as will be seen in RIT) do make use of namespaces, and hence the tutorial examples will need modifications to work as examples.

Introduction

This document will first show a simple example in which no namespace is involved. It will then introduce a namespace and show how the XPath expression must be modified to take account of the namespace.

It would not be necessary to use XPath to validate such a simple example within RIT.

A simple example without a namespace

Given the XML:


    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    <child name="Sid">
    <grandchild name="Sue">
    <greatgrandchild name="Fred" />
    </grandchild>
    </child>
    </root>

Then the great-grand-child name "Fred" can be extracted with:

    /root/child/grandchild/greatgrandchild/@name

An example with a namespace on the nodes

But this solution does not work if there is a namespace added to the nodes:

    <?xml version="1.0" encoding="UTF-8"?>
    <my:root xmlns:my="http://www.example.com/mynamespace">
    <my:child name="Sid">
    <my:grandchild name="Sue">
    <my:greatgrandchild name="Fred" />
    </my:grandchild>
    </my:child>
    </my:root>

We cannot now use:

    /root/child/grandchild/greatgrandchild/@name

And there is no available syntax of this nature:

    /my:root/my:child/my:grandchild/my:greatgrandchild/@name

Instead need this expression (normally formatted as a single line):

    /*["root"=local-name()]
    /*["child"=local-name()]
    /*["grandchild"=local-name()]
    /*["greatgrandchild"=local-name()]
    /@name

An example with a namespace on the nodes and attributes

If we add also have the namespace prefix on the attributes:

    <?xml version="1.0" encoding="UTF-8"?>
    <my:root xmlns:my="http://www.example.com/mynamespace">
    <my:child my:name="Sid">
    <my:grandchild my:name="Sue">
    <my:greatgrandchild my:name="Fred" />
    </my:grandchild>
    </my:child>
    </my:root>

Then the same approach is needed for the "name" attribute:

    /*["root"=local-name()]
    /*["child"=local-name()]
    /*["grandchild"=local-name ()]
    /*["greatgrandchild"=local-name()]
    /@*["name"=local-name()]

A purist approach to the XPath syntax

All of the above solutions filter only on the local names of the nodes and attributes. We should also filter by the namespace. (Note that the namespace above is not "my" but is "http://www.example.com/mynamespace&quot;.

Doing this the last solution becomes:

    /*
    ["root"=local-name()]
    ["http://www.example.com/mynamespace"=namespace-uri()]
    /*
    ["child"=local-name()]
    ["http://www.example.com/mynamespace"=namespace-uri()]
    /*
    ["grandchild"=local-name ()]
    ["http://www.example.com/mynamespace"=namespace-uri()]
    /*
    ["greatgrandchild"=local-name()]
    ["http://www.example.com/mynamespace"=namespace-uri()]
    /@*
    ["name"=local-name()]
    ["http://www.example.com/mynamespace"=namespace-uri()]

Often however the namespaces used in the message will be known and so it will be possible to assert that the local names are sufficient to identify the relevant value, which helps to keep the XPath expression readable.

Further Reading

The reader may also be interested in these documents from IBM:

[{"Product":{"code":"SSBLQQ","label":"IBM Rational Test Workbench"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Rational Integration Tester","Platform":[{"code":"PF002","label":"AIX"},{"code":"PF016","label":"Linux"},{"code":"PF022","label":"OS X"},{"code":"PF027","label":"Solaris"},{"code":"PF033","label":"Windows"}],"Version":"8.0;8.5;8.6","Edition":"All Editions","Line of Business":{"code":"LOB45","label":"Automation"}}]

Document Information

Modified date:
11 July 2019

UID

swg21684541