XPath and JXPath Syntax


After you've gotten the Swing client working, you're ready to experiment with some basic XPath queries. For this example, JXPath translates your queries into Java using JavaBean-based patterns (such as the getProperty() methods). For example, to obtain Bob's mother, the bob.getMother() method is called. So, to obtain his grandmother using a mother/mother query, the call would effectively be bob.getMother().getMother().

Basic XPath

Here are some additional examples of basic XPath syntax and the results.

 mother/mother 

This query simply returns Bob's grandmother.

 father/father 

Returns Bob's grandfather.

 mother|father 

Returns Bob's parents. Note the use of the vertical bar (|) to indicate that both properties should be retrieved.

 children 

Returns all of Bob's children by calling the Bob object's getChildren() method.

Complex XPath

In addition to basic queries, JXPath supports a full suite of XPath queries as described at http://www.w3.org/TR/xpath. It's beyond the scope of this text to describe the full range of possible XPath queries, but you should be aware that XPath supports a variety of specialized query tokens. Some examples include:

 child::* 

Returns the following values:

 Smith, Jon Smith, Kitty Smith, Willow Smith, Robert Bob male Smith Smith, Maggy 

As can be seen, the expression child::* refers to all of the possible child attributesnot just the children as defined by our Java code, but rather the child values as expressed by object properties, including all of the children, the mother, the father, and the other properties of the Bob object. This illustrates an important conceptthe notion that child and parent have particular meanings in XPath based on the object graph traversal, which may or may not be conceptually the same as the parent/child relationship for your application.

 descendant::* 

This query will actually result in an out-of-memory error, as it will attempt to load all of the descendants of all of the objects. Unfortunately, attempting to load all of the descendant properties of Bob leads to an infinitely recursive loop for our particular object graph. One of the Bob object's attributes is his father. His father, in turn, has the Bob object as a child, and so JXPath will attempt to continue loading Bob, his father, Bob, his father, and so on until all the available memory is exhausted. If this were used to represent a finite graph, for example, an XML document, this query would operate correctly.

 count(children) 

Returns the string 3.0. This query is an example of an XPath function, used in conjunction with a node query (called a node-set in XPath).

 20 mod 6 

Returns the string 2.0. Another XPath function. XPath does have some support for basic string and numeric functions, but these operations are often simpler, easier, and more powerful when expressed in the native programming language.

 concat('foo', 'bar') 

Returns foobar. This is another example of an XPath function.

JXPath Extensions

JXPath defines a few additional extensions to the XPath syntax. The most notable extensions are the ability to call arbitrary Java code, as shown in the following. Notice that the fully qualified class name is used in the query.

 com.cascadetg.ch08.Person.new('male', 'Indigo', 'Smith') 

Returns Smith, Indigo (the newly created object). The new term is used to indicate the class constructor for the object.

 com.cascadetg.ch08.FamilyFactory.getPerson() 

Calls the static method to return Smith, Bob.

 com.cascadetg.ch08.FamilyFactory.getPerson()/children 

Returns the values:

 Smith, Jon Smith, Kitty Smith, Willow 

This shows that you can mix the results of a Java method call with JXPath's XPath queries.

 getFirstName(/) 

Returns Bob. Note that the first argument of the method is the object that the method should be called uponthere is no implicit this prefacing the method call. In this case, we are using the / operator, which is a reference to the base Bob object.

 setFirstName(/, 'Sam') 

This query doesn't return anything, but instead modifies the base Bob object's first name, changing the value to Sam. This shows how JXPath can be used to modify an object. Following this up with a simple / query to obtain the Bob (now Sam) object, we can see that the name has been successfully changed.

For more information on other JXPath extensions to the XPath language, see http://jakarta.apache.org/commons/jxpath/users-guide.html.



    Apache Jakarta Commons(c) Reusable Java Components
    Real World Web Services
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 137
    Authors: Will Iverson

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net