XPath over XML Schema: Examples
Contents
Sample XSD used in examples Boilerplate
Examples: 1, 2, 3, 4, 5, 6 (more examples in the download).
Example 5: /ex:*
/** * In this example we will evaluate an xpath in the context * of results of another xpath. * The first XPath will get all top-level elements, * the second one will get all children of the top-level elements. */ public static List<PathElement> example5(SchemaData schData) { // analyse the first XPath XPath xpath1 = new XPath("/ex:*"); List<PathElement> matched1 = null; try { matched1 = xpath1.analyse(schData, null); } catch (XPathException e) { System.err.println(e.getMessage()); } System.out.println("- First /ex:* XPath"); printMatchedNodes(matched1); // analyse the second XPath XPath xpath2 = new XPath("/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 /ex:* XPath, effectively /ex:*/ex:*"); printMatchedNodes(matched2); return matched2; }
Output
- First /ex:* XPath Matched nodes: /Address[1,1](10:3) /Delivery[1,1](23:3) /Addresses[1,1](34:3) /Deliveries[1,1](42:3) /Collection[1,1](50:3) /ID[1,1](58:3) - Second /ex:* XPath, effectively /ex:*/ex:* Matched nodes: /Address[1,1](10:3)/Line1[1,1](14:7) /Address[1,1](10:3)/Line2[1,1](15:7) /Address[1,1](10:3)/City[1,1](16:7) /Address[1,1](10:3)/State[1,1](17:7) /Address[1,1](10:3)/Zip[1,1](18:7) /Address[1,1](10:3)/ZipExt[1,1](19:7) /Delivery[1,1](23:3)/Date[1,1](27:7) /Delivery[1,1](23:3)/Time[1,1](28:7) /Delivery[1,1](23:3)/Address[1,1](29:7) /Delivery[1,1](23:3)/DeliveryID[1,1](30:7) /Addresses[1,1](34:3)/Address[0,inf](38:7) /Deliveries[1,1](42:3)/Delivery[0,inf](46:7) /Collection[1,1](50:3)/Addresses[1,1](53:7) /Collection[1,1](50:3)/Deliveries[1,1](54:7)