OpenWGA 7.9 - TMLScript reference

WGA » WGA.Xml
Method :

xpath(domobject, xpath [, namespaces])

On object WGA.Xml
Usage Executes an XPath expression on some XML text or JavaBean
Description

This function always returns single values. If the xpath expression matches multiple values it will only return the first one. To retrieve lists of values use xpathList().

It accepts the following types of DOM object:

  • An XML string. The given XML text is parsed. Then the XPath expression is executed on the resulting DOM document at root level. 
  • Any DOM node from an XML document parsed by parse() or load(). The XPath expression is executed on the given node.
  • A JavaBean. The XPath expression is used to identify bean properties. The result is the value that is stored on the requested bean property. This uses the Apache library JXPath under the hood. See their documentation for details how XPath is used to browse JavaBeans.

If your DOM contains nodes belonging to an XML namespace you will need to define prefixes for those namespaces by using argument "namespaces" and use those prefixes in the XPath expression on these nodes if you want to query them.

For backward compatibility there still is a global function "xpath()" available whose use is discouraged since OpenWGA 5.3.

Parameters

domobject (String, Java object):

The DOM object on which the XPath is to be executed. See the description for valid types.

xpath (String):

XPath expression to execute

namespaces (Lookup table, optional):

Determines XML namespace prefixes to be used inside the XPath expression. Keys are prefixes, values are namespace URIs. The prefixes do not need to match prefixes that that may be present in the parsed XML. DOM elements that are specified in the XPath expression without prefixes determined here are regarded to be of the default namespace.

Return value Variant 1: Result of the expression, either the text of a text node or an attribute, or a DOM4J node object (org.dom4j.Node)

Variant 2: A custom value retrieved from the JavaBean
Allowed in script types
  • WebTML pages and normal WebTML actions
  • Master actions
  • TMLScript tasks in jobs
  • Content type events
    portletevent
Examples An example how xpath() can be used to browser JavaBean objects. Consider the following call to a JavaBean:

customer.getAddress().getStreet()


You may as well request this information by xpath:

WGA.Xml.xpath(customer, "/address/street");


While there might seem to be no benefit in requesting beans this way there are certain situations where this might come handy. For example you might use an xpath expression like a "pointer" to a special location inside a JavaBean that you need to request. This pointer can be stored or passed on as parameter in WebTML actions, just like any string, but then can be used to retrieve this information from some custom JavaBean.

Another example, showing the usage of XPath with namespace prefixes. This defines and uses a prefix "ns2" which points to the namespace "http://www.opengis.net/gml":

WGA.Xml.xpath(xml, "//ns2:pos", {ns2: "http://www.opengis.net/gml"});