XPath over XML Schema: Examples
Home Downloads
Contents
Sample XSD used in examples
Boilerplate
Examples: 1, 2, 3, 4, 5, 6 (more examples in the download).
Example 6: /ex:Collection/ex:Deliveries/ex:Delivery/ex:Address/ex:City
/**
 * In this example we will first select a node, and then we will write 
 * another XPath which selects all ancestors of this node.
 */
public static List<PathElement> example6(SchemaData schData) {

    // analyse the first XPath
    XPath xpath1 = new XPath("/ex:Collection/ex:Deliveries/ex:Delivery/ex:Address/ex:City");
    List<PathElement> matched1 = null;
    try {
        matched1 = xpath1.analyse(schData, null);
    } catch (XPathException e) {
        System.err.println(e.getMessage());
    }
    System.out.println("- First XPath: /ex:Collection/ex:Deliveries/ex:Delivery/ex:Address/ex:City");
    printMatchedNodes(matched1);
    
    // analyse the second XPath
    XPath xpath2 = new XPath("ancestor::ex:*");
    List<PathElement> matched2 = null;
    try {
        matched2 = xpath2.analyse(schData, xpath1);
    } catch (XPathException e) {
        System.err.println(e.getMessage());
    }
    
    // print final matched nodes
    System.out.println("\n- Second XPath: ancestor::ex:*");
    printMatchedNodes(matched2);
    
    return matched2;
}
    
Output
- First XPath: /ex:Collection/ex:Deliveries/ex:Delivery/ex:Address/ex:City
Matched nodes:
/Collection[1,1](50:3)/Deliveries[1,1](54:7)/Delivery[0,inf](46:7)/Address[1,1](29:7)/City[1,1](16:7)

- Second XPath: ancestor::ex:*
Matched nodes:
/Collection[1,1](50:3)/Deliveries[1,1](54:7)/Delivery[0,inf](46:7)/Address[1,1](29:7)
/Collection[1,1](50:3)/Deliveries[1,1](54:7)/Delivery[0,inf](46:7)
/Collection[1,1](50:3)/Deliveries[1,1](54:7)
/Collection[1,1](50:3)
    
SourceForge.net Logo Hit Counter