Extracting information from a message by using XPath 1.0 and a JavaCompute node
XPath is a query language designed for use with XML documents, but you can use it with any tree structure to query contents.
IBM® App Connect Enterprise uses XPath to select elements from the logical message tree regardless of the format of the bit stream. The terminology used in this topic is based on the terminology used in the W3C definition of XPath 1.0. For more information about XPath, see Using XPath; and for more information about the W3C definition of the XPath 1.0 standard, see W3C XPath 1.0 Specification. For examples of XPath use, see the MbXPath topic in the Java user-defined node API documentation.
Using the evaluateXPath method to extract message information
- Namespace axis and namespace node type. The namespace axis returns the actual XML namespace declaration nodes for a particular element. You can therefore manipulate XML prefix or URI declarations within an XPath expression. This axis returns an empty node set for bit streams that are not XML.
- If you use the id() function, it throws an MbRecoverableException.
The evaluateXPath() method can be called on a MbMessage object (for absolute paths), or on a MbElement object (for relative paths). The XPath expression is passed to the method as a string parameter. A second form of this method is provided that takes an MbXPath object. This object encapsulates an XPath expression along with variable bindings and namespace mappings, if these are required.
- java.lang.Boolean, representing the XPath Boolean type
- java.lang.Double, representing the XPath number type
- java.lang.String, representing the XPath string type
- java.util.List, representing the XPath node set. The List interface represents an ordered sequence of objects, in this case MbElements. It allows direct access to the elements, or the ability to get an Iterator or an MbElement array.
XPath variable binding
- Boolean
- node set
- number
- string
XPath namespace support
<ns1:aaa xmlns:ns1='http://mydomain.com/namespace1'
xmlns:ns2='http://mydomain.com/namespace2'>
<ns2:aaa>
<ns1:bbb/>
</ns2:aaa>
</ns1:aaa>
The namespace prefix is convenient for representing the namespace, but is meaningful only within the document that defines that mapping. The namespace URI defines the global meaning. Also, the concept of a namespace prefix is not meaningful for documents that are generated in a message flow, because a namespace URI can be assigned to a syntax element without an XMLNS mapping having been defined.
For this reason, the XMLNSC and MRM parsers expose only the namespace URI to the integration node and to user code (ESQL or user-defined code). By using ESQL, you can set up your own mappings to create abbreviations to these potentially long URIs. These mappings are not related in any way to the prefixes that are defined in the XML document (although they can be the same name).
MbNamespaceBindings ns = new MbNamespaceBindings();
ns.addBinding("other", "http://mydomain.com/namespace2");
ns.setDefaultNamespace("http://mydomain.com/namespace2");
MbXPath xp = new MbXPath("/aaa/other:aaa/bbb", ns);
List<MbElement> nodeset = (List<MbElement>)message.evaluateXPath(xp);