| <html><head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Using PsychoPath XPath 2.0 API</title><link href="book.css" rel="stylesheet" type="text/css"><link href="../book.css" rel="stylesheet" type="text/css"><meta content="DocBook XSL Stylesheets V1.74.0" name="generator"><link rel="home" href="index.html" title="XPath 2.0 Processor User Manual"><link rel="up" href="index.html" title="XPath 2.0 Processor User Manual"><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" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="How_to_feed_Psychopath_XPath_expressions"></a>Using PsychoPath XPath 2.0 API</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 type="1"><li><p>Load the XML document</p></li><li><p>Optionally validate the XML document</p></li><li><p>Initialize static and dynamic context in respect to the document | |
| root</p></li><li><p>Parse the XPath 2.0 expression</p></li><li><p>Statically verify the XPath 2.0 expression</p></li><li><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><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="Non-Schema_Aware"></a>Non-Schema Aware</h2></div></div></div><pre class="programlisting">/** | |
| * First load and optionally validate the XML document | |
| */ | |
| // Create an InputStream from the XML document | |
| InputStream is = new FileInputStream(“XPexample.xml”); | |
| // Initializing the Xerces DOM loader. | |
| DOMLoader loader = new XercesLoader(); | |
| // Optionally set flag to validate XML document loader.setvalidating(validate); | |
| // Loads the XML document and stores the DOM root | |
| Document doc = loader.load(is); | |
| /** | |
| * Dynamic contexts must be initialised to defaults | |
| * dependent on the XML Schema. | |
| */ | |
| // Extracting the schema from DOM root of Xpexpression.xml. | |
| ElementPSVI rootPSVI = (ElementPSVI)doc.getDocumentElement(); | |
| XSModel schema = rootPSVI.getSchemaInformation(); | |
| // Initialising the DynamicContext. | |
| DynamicContext dc = new DefaultDynamicContext(schema, doc); | |
| // Register the namespaces of the XPath 2.0 predefined datatypes | |
| dc.addnamespace(“xs”,”[http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema]”); | |
| // Register the XPath 2.0 standard functions | |
| dc.addfunctionlibrary(new FnFunctionLibrary()); | |
| dc.addfunctionlibrary(new XSCtrLibrary()); | |
| /** | |
| * Parsing the XPath 2.0 expression into an AST representation | |
| */ | |
| // Initialises PsychoPath’s supplied parser. | |
| XPathParser xpp = new JflexCupParser(); | |
| // Parses the XPath expression. | |
| XPath xp = xpp.parse(xpath); | |
| /** | |
| * Static check the AST to verift structural validity of | |
| * XPath 2.0 expression | |
| */ | |
| // Initializing StaticChecker. | |
| StaticChecker namecheck = new StaticNameResolver(sc); | |
| // Static Checking the Xpath expression ’Hello World!’ namecheck.check(xp); | |
| /** | |
| * Evaluate the XPath 2.0 expression | |
| */ | |
| // Initializing the evaluator with DynamicContext and the name | |
| // of the XML document XPexample.xml as parameters. | |
| Evaluator eval = new DefaultEvaluator(dc, doc); | |
| // Evaluates the XPath 2.0 expression, storing the result | |
| // in the ResultSequence | |
| ResultSequence rs = eval.evaluate(xp); | |
| </pre></div></div></body></html> |