| <html><head><META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>How to feed Psychopath XPath expressions</title><link href="book.css" type="text/css" rel="stylesheet"><link href="book.css" type="text/css" rel="stylesheet"><meta content="DocBook XSL Stylesheets V1.76.1" name="generator"><link rel="home" href="index.html" title="usermanual"><link rel="up" href="index.html" title="usermanual"><link rel="prev" href="ch01.html" title="Introduction"><link rel="next" href="ch02s02.html" title="Schema Aware"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" title="How to feed Psychopath XPath expressions"><div class="titlepage"><div><div><h2 class="title"><a name="How_to_feed_Psychopath_XPath_expressions"></a>How to feed Psychopath XPath expressions</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><ul><li><span class="section"><a href="ch02.html#Non-Schema_Aware">Non-Schema Aware</a></span></li><li><span class="section"><a href="ch02s02.html">Schema Aware</a></span></li><li><span class="section"><a href="ch02s03.html">How to use the XPath 2.0 grammar with PsychoPath</a></span><ul><li><span class="section"><a href="ch02s03.html#Constants">Constants</a></span></li><li><span class="section"><a href="ch02s03s02.html">Path expressions</a></span></li><li><span class="section"><a href="ch02s03s03.html">Axis steps</a></span></li><li><span class="section"><a href="ch02s03s04.html">Set difference, intersection and Union</a></span></li><li><span class="section"><a href="ch02s03s05.html">Arithmetic Expressions</a></span><ul><li><span class="section"><a href="ch02s03s05.html#Unary">Unary</a></span></li><li><span class="section"><a href="ch02s03s05s02.html">Multiplication and Division:</a></span></li><li><span class="section"><a href="ch02s03s05s03.html">Addition and Subtraction:</a></span></li></ul></li><li><span class="section"><a href="ch02s03s06.html">Range expressions</a></span></li><li><span class="section"><a href="ch02s03s07.html">Comparisons</a></span></li><li><span class="section"><a href="ch02s03s08.html">Conditional Expressions</a></span></li><li><span class="section"><a href="ch02s03s09.html">Quantified Expressions</a></span></li><li><span class="section"><a href="ch02s03s10.html">And, Or Expressions</a></span></li><li><span class="section"><a href="ch02s03s11.html">SequenceType Matching Expressions</a></span></li></ul></li></ul></div><p>Since PsychoPath has been implemented as an external library and not as a complete program, in order to use it, it needs to be accessed from inside another program. To process XPath 2.0 expressions using PsychoPath from another programs one needs to go through the following process: </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p>Load the XML document </p></li><li class="listitem"><p>Optionally validate the XML document </p></li><li class="listitem"><p>Initialize static and dynamic context in respect to the document root </p></li><li class="listitem"><p>Parse the XPath 2.0 expression </p></li><li class="listitem"><p>Statically verify the XPath 2.0 expression </p></li><li class="listitem"><p>Evaluate the XPath 2.0 expression in respect to the XML document</p></li></ol></div><p>To give a better idea of how this process actually works, we’ll go through an example of processing and evaluating the string expression “Hello World!”. In this example the XML document that we load is called “XPexample.xml”. </p><p>All 5 main steps have been explained in detail in |
| <a class="ulink" href="/wiki/PsychoPathXPathProcessor/UserInterface" target="_top">User Interface</a>, so below is just a brief code summary: |
| |
| </p><div class="section" title="Non-Schema Aware"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="Non-Schema_Aware"></a>Non-Schema Aware</h2></div></div></div><div class="literallayout"><p>/**<br> |
| * First load and optionally validate the XML document <br> |
| */<br> |
| </p></div><div class="literallayout"><p>// Create an InputStream from the XML document<br> |
| InputStream is = new FileInputStream("XPexample.xml");<br> |
| </p></div><div class="literallayout"><p>// Initializing the Xerces DOM loader.<br> |
| DOMLoader loader = new XercesLoader();<br> |
| </p></div><div class="literallayout"><p>// Optionally set flag to validate XML document<br> |
| // loader.set_validating(validate);<br> |
| // Loads the XML document and stores the DOM root<br> |
| Document doc = loader.load(is);<br> |
| </p></div><div class="literallayout"><p>// Initialising the StaticContext using a builder with suitable defaults.<br> |
| StaticContextBuilder scb = new StaticContextBuilder();<br> |
| </p></div><div class="literallayout"><p>/**<br> |
| * Parsing the XPath 2.0 expression into an executable expression, including<br> |
| * a static check and verification.<br> |
| */<br> |
| String xpathText = "string-join(//character/name, ', ')";<br> |
| XPath2Expression expr = new Engine().parseExpression(xpathText, scb);<br> |
| </p></div><div class="literallayout"><p>// Evaluates the XPath 2.0 expression, storing the result<br> |
| // in the ResultSequence<br> |
| ResultSequence rs = expr.evaluate(new DynamicContextBuilder(scb),<br> |
| new Object[] { doc });<br> |
| </p></div><div class="literallayout"><p>for (int i = 0; i < rs.size(); ++i) {<br> |
| System.out.println("#" + i + ": " + rs.value(i));<br> |
| }<br> |
| </p></div><p> |
| |
| </p></div></div></body></html> |