Skip to main content
Skip table of contents

Using Namespaces Within XPath Expressions

There are multiple places within the Java API and DCP configuration where XPath expressions can be provided to select XML nodes. With these XPath expressions it is often desirable to be able to reference nodes that are within XML namespaces. This can be done using the namespace-uri() XPath function, but this is verbose and repetitive. To make this simpler it is possible to define prefix URI pairs that can be used within XPath expressions.

Java API

JAVA
DocumentComparator documentComparator = new DocumentComparator();
NamespaceConfiguration namespaceConfiguration = new NamespaceConfiguration();
namespaceConfiguration.setDefaultNamespace("http://docbook.org/ns/docbook");
namespaceConfiguration.addNamespace("xi", "http://www.w3.org/2001/XInclude");
documentComparator.setNamespaceConfiguration(namespaceConfiguration);

In the above example, the default namespace is set to the namespace for DocBook. This means that any unprefixed node name within an XPath expression will refer to nodes within the DocBook namespace. Additionally, the example above also defines a prefix “xi” that is set to the XInclude namespace. If a node name within an XPath expression is prefixed with “xi” then it will refer to nodes within the XInclude namespace.

E.g. if the below expression is used with the above config then chapter is in the DocBook namespace and include is in the XInclude namespace.

CODE
chapter/xi:include

DCP

XML
<documentComparator>
...
<standardConfig>
  ...
  <namespaceConfiguration>
    <defaultNamespace uri="http://docbook.org/ns/docbook"/>
    <userNamespaces>
      <userNamespace prefix="xi" uri="http://www.w3.org/2001/XInclude"/>
    </userNamespaces>
  </namespaceConfiguration>
  <ignoreChangesConfig>
    <locations>
      <location ignoreXpath="chapter/xi:include" resultRule="B"/>
    </locations>
  </ignoreChangesConfig>
  ...
</standardConfig>
...
</documentComparator>

The above DCP fragment defines the same namespace configuration as the previous Java API example. Unprefixed node names in XPath expressions will refer to elements within the DocBook namespace and node names using the “xi” prefix will refer to elements within the XInclude.

The DCP also contains an example usage of the defined namespaces using the ignore changes feature.

Which features can use the namespace configuration?

Currently the namespace configuration only has an effect when used with the following features:

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.