Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] New XPath issue: what to do with non-nodes?

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Michael Abato <mrabato AT earthlink.net>
  • To: Elliotte Harold <elharo AT metalab.unc.edu>
  • Cc: xom-interest <xom-interest AT lists.ibiblio.org>
  • Subject: Re: [XOM-interest] New XPath issue: what to do with non-nodes?
  • Date: Thu, 13 Jan 2005 10:00:54 -0500

On Jan 13, 2005, at 9:05 AM, Elliotte Harold wrote:

Michael Abato wrote:

Another reason a developer may prefer XPath over java code is that it allows you to move the details of the data model into declarative configuration. This is particularly appealing in IoC/DI infrastructures (SpringFramework, PicoContainer, HiveMind, etc.) which bind objects at runtime and minimize concrete object coupling. Moving the XPath expression external decouples runtime objects from the concrete data model as well.

I haven't worked with these. Would such frameworks be likely to serialize booleans, numbers, and strings as XPath expressions? Or would these simply be strings in an XML file somewhere? i.e. what sort of XPath expressions do these frameworks use? Concrete examples would be helpful.

--
Elliotte Rusty Harold elharo AT metalab.unc.edu
XML in a Nutshell 3rd Edition Just Published!
http://www.cafeconleche.org/books/xian3/
http://www.amazon.com/exec/obidos/ISBN=0596007647/cafeaulaitA/ref=nosim

The IoC systems don't use the XPath as such, but embedding XPath as expressions to "setter" methods and constructors is a natural idiom.

For example, an application I'm working provides apache cocoon-like services for data access/transform. Basically, it pipes XOM nodes through a sequence of processors with a variety of selectors, switches, sequences, transforms, etc. It configures via the IoC layer of Spring. The configuration (the plumbing) is done in the IoC config file.

A Switch object uses an XPath expression (I've simply wrapped the Jaxen example that's been talked about on this list in a XOMXPath class...) to select what to switch on. It looks something like:

import nu.xom.Node;
public interface Processor {
Node process(Node node);
}
public class Switch implements Processor {
private final XPath selector;
private final Map actions;
public Switch(String selectorExpression, Map actions) {
this.selector = XOMXPath.parse(selectorExpression);
this.actions = actions;
}
public Node process(Node node) {
Object key = xpath.stringValueOf(node);
Processor action = (Processor)actions.get(key);
return action.process(node);
}
}

Here's a simple deployment example. Take starting an XML version of an HTTP request (for concreteness) "GET /test.xml?action=foo":

<request method='GET' request-uri='/test.xml'>
<parameter name='action'>foo</paramater>
</request>


A Switch can be configured to switch on the action parameter:

<bean class="com.example.Switch">
<!-- constructor injection -->
<constructor-arg>
<value>/request/parameter[@name='action']</value>
</constructor-arg>
<constructor-arg>
<map>
<entry key='foo'>
<bean class='com.example.bar.FooProcessor' />
</entry>
</map>
</constructor-arg>
</bean>

A common case I've run into is an configuring an expression to look for existence (boolean result) and counting (e.g.: how many page hits?) - hence the desire for non-node results.

A more business-like example might use an xpath expression to specify how to extract a formatted name from a "person" XML record:

<bean class="com.example.Person">
<!-- property injection - calls Person.setNameFormat() -->
<property name='nameFormat'>
<value>concat(name/lastname,', ',name/firstname)</value>
</property>
</bean>

The answer might be to take my use-case to nux and company, but I'd also like variables so that I can configure:

<property name='find'>
<value>//*[@id=$id]</value>
</property>

Michael Abato





Archive powered by MHonArc 2.6.24.

Top of Page