summaryrefslogtreecommitdiff
path: root/examples/cxx/tree/xpath
diff options
context:
space:
mode:
Diffstat (limited to 'examples/cxx/tree/xpath')
-rw-r--r--examples/cxx/tree/xpath/README47
-rw-r--r--examples/cxx/tree/xpath/dom-parse.cxx113
-rw-r--r--examples/cxx/tree/xpath/dom-parse.hxx26
-rw-r--r--examples/cxx/tree/xpath/driver-2.cxx139
-rw-r--r--examples/cxx/tree/xpath/driver.cxx137
-rw-r--r--examples/cxx/tree/xpath/makefile71
-rw-r--r--examples/cxx/tree/xpath/people.xml29
-rw-r--r--examples/cxx/tree/xpath/people.xsd39
8 files changed, 601 insertions, 0 deletions
diff --git a/examples/cxx/tree/xpath/README b/examples/cxx/tree/xpath/README
new file mode 100644
index 0000000..48d40d6
--- /dev/null
+++ b/examples/cxx/tree/xpath/README
@@ -0,0 +1,47 @@
+This example shows how to use the C++/Tree mapping together with XPath.
+In particular, it shows how to execute an XPath query on the underlying
+DOM document and then handle the result using the more convenient object
+model representation. For more information on maintaining association
+with the underlying DOM document, refer to Section 5.1, "DOM Association"
+in the C++/Tree Mapping User Manual.
+
+You will need the XQilla library[1] which provides XQuery and XPath 2
+support on top of Xerces-C++ in order to build and run this example.
+
+[1] http://xqilla.sourceforge.net
+
+The example consists of the following files:
+
+people.xsd
+ XML Schema definition for a simple person record vocabulary.
+
+people.xml
+ Sample XML instance document.
+
+people.hxx
+people.cxx
+ C++ types that represent the person record vocabulary and a set of
+ parsing functions that convert XML instance documents to a tree-like
+ in-memory object model. These are generated by XSD from people.xsd.
+
+dom-parse.hxx
+dom-parse.cxx
+ Definition and implementation of the parse() function that parses an
+ XML document to a DOM document.
+
+driver.cxx
+ Driver for Xerces-C++ 3.x.y/XQilla 2.2.x. It first calls the above
+ parse() function to parse the input file to a DOM document using
+ XQilla-provided DOM Implementation with support for XPath 2. It
+ then parses the DOM document to the object model. Finally, it
+ prepares and executes an XPath query on the underlying DOM
+ document and then handles the result by getting back from the
+ returned DOM nodes to object model nodes.
+
+driver-2.cxx
+ Driver for Xerces-C++ 2.x.y/XQilla 2.1.x. It performs the same set
+ of actions as driver.cxx above.
+
+To run the example on the sample XML document simply execute:
+
+$ ./driver people.xml
diff --git a/examples/cxx/tree/xpath/dom-parse.cxx b/examples/cxx/tree/xpath/dom-parse.cxx
new file mode 100644
index 0000000..84ff0af
--- /dev/null
+++ b/examples/cxx/tree/xpath/dom-parse.cxx
@@ -0,0 +1,113 @@
+// file : examples/cxx/tree/xpath/dom-parse.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#include "dom-parse.hxx"
+
+#include <istream>
+
+#include <xercesc/dom/DOM.hpp>
+#include <xercesc/framework/Wrapper4InputSource.hpp>
+
+#include <xsd/cxx/xml/sax/std-input-source.hxx>
+#include <xsd/cxx/xml/dom/bits/error-handler-proxy.hxx>
+
+#include <xsd/cxx/tree/exceptions.hxx>
+#include <xsd/cxx/tree/error-handler.hxx>
+
+using namespace xercesc;
+namespace xml = xsd::cxx::xml;
+namespace tree = xsd::cxx::tree;
+
+xml::dom::auto_ptr<DOMDocument>
+parse (std::istream& is,
+ const std::string& id,
+ bool validate,
+ DOMImplementation* impl)
+{
+#if _XERCES_VERSION >= 30000
+
+ // Xerces-C++ 3.0.0 and later.
+ //
+ xml::dom::auto_ptr<DOMLSParser> parser (
+ impl->createLSParser (DOMImplementationLS::MODE_SYNCHRONOUS, 0));
+
+ DOMConfiguration* conf (parser->getDomConfig ());
+
+ // Discard comment nodes in the document.
+ //
+ conf->setParameter (XMLUni::fgDOMComments, false);
+
+ // Enable datatype normalization.
+ //
+ conf->setParameter (XMLUni::fgDOMDatatypeNormalization, true);
+
+ // Do not create EntityReference nodes in the DOM tree. No
+ // EntityReference nodes will be created, only the nodes
+ // corresponding to their fully expanded substitution text
+ // will be created.
+ //
+ conf->setParameter (XMLUni::fgDOMEntities, false);
+
+ // Perform namespace processing.
+ //
+ conf->setParameter (XMLUni::fgDOMNamespaces, true);
+
+ // Do not include ignorable whitespace in the DOM tree.
+ //
+ conf->setParameter (XMLUni::fgDOMElementContentWhitespace, false);
+
+ // Enable/Disable validation.
+ //
+ conf->setParameter (XMLUni::fgDOMValidate, validate);
+ conf->setParameter (XMLUni::fgXercesSchema, validate);
+ conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false);
+
+ // We will release the DOM document ourselves.
+ //
+ conf->setParameter (XMLUni::fgXercesUserAdoptsDOMDocument, true);
+
+ // Set error handler.
+ //
+ tree::error_handler<char> eh;
+ xml::dom::bits::error_handler_proxy<char> ehp (eh);
+ conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp);
+
+#else // _XERCES_VERSION >= 30000
+
+ // Same as above but for Xerces-C++ 2 series.
+ //
+ xml::dom::auto_ptr<DOMBuilder> parser (
+ impl->createDOMBuilder (DOMImplementationLS::MODE_SYNCHRONOUS, 0));
+
+ parser->setFeature (XMLUni::fgDOMComments, false);
+ parser->setFeature (XMLUni::fgDOMDatatypeNormalization, true);
+ parser->setFeature (XMLUni::fgDOMEntities, false);
+ parser->setFeature (XMLUni::fgDOMNamespaces, true);
+ parser->setFeature (XMLUni::fgDOMWhitespaceInElementContent, false);
+ parser->setFeature (XMLUni::fgDOMValidation, validate);
+ parser->setFeature (XMLUni::fgXercesSchema, validate);
+ parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false);
+ parser->setFeature (XMLUni::fgXercesUserAdoptsDOMDocument, true);
+
+ tree::error_handler<char> eh;
+ xml::dom::bits::error_handler_proxy<char> ehp (eh);
+ parser->setErrorHandler (&ehp);
+
+#endif // _XERCES_VERSION >= 30000
+
+ // Prepare input stream.
+ //
+ xml::sax::std_input_source isrc (is, id);
+ Wrapper4InputSource wrap (&isrc, false);
+
+#if _XERCES_VERSION >= 30000
+ xml::dom::auto_ptr<DOMDocument> doc (parser->parse (&wrap));
+#else
+ xml::dom::auto_ptr<DOMDocument> doc (parser->parse (wrap));
+#endif
+
+ eh.throw_if_failed<tree::parsing<char> > ();
+
+ return doc;
+}
diff --git a/examples/cxx/tree/xpath/dom-parse.hxx b/examples/cxx/tree/xpath/dom-parse.hxx
new file mode 100644
index 0000000..cfa1555
--- /dev/null
+++ b/examples/cxx/tree/xpath/dom-parse.hxx
@@ -0,0 +1,26 @@
+// file : examples/cxx/tree/xpath/dom-parse.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#ifndef DOM_PARSE
+#define DOM_PARSE
+
+#include <string>
+#include <iosfwd>
+
+#include <xercesc/dom/DOMDocument.hpp>
+#include <xercesc/dom/DOMImplementation.hpp>
+
+#include <xsd/cxx/xml/dom/auto-ptr.hxx>
+
+// Parse an XML document from the standard input stream with an
+// optional resource id. Resource id is used in diagnostics as
+// well as to locate schemas referenced from inside the document.
+//
+xsd::cxx::xml::dom::auto_ptr<xercesc::DOMDocument>
+parse (std::istream& is,
+ const std::string& id,
+ bool validate,
+ xercesc::DOMImplementation*);
+
+#endif // DOM_PARSE
diff --git a/examples/cxx/tree/xpath/driver-2.cxx b/examples/cxx/tree/xpath/driver-2.cxx
new file mode 100644
index 0000000..ccbdd7c
--- /dev/null
+++ b/examples/cxx/tree/xpath/driver-2.cxx
@@ -0,0 +1,139 @@
+// file : examples/cxx/tree/xpath/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::auto_ptr
+#include <string>
+#include <fstream>
+#include <iostream>
+
+#include <xercesc/dom/DOM.hpp>
+
+#include <xqilla/xqilla-dom3.hpp>
+
+#include <xsd/cxx/xml/string.hxx> // xml::string, xml::transcode
+
+#include "dom-parse.hxx"
+
+#include "people.hxx"
+
+using namespace std;
+using namespace xercesc;
+namespace xml = xsd::cxx::xml;
+
+int
+main (int argc, char* argv[])
+{
+ if (argc != 2)
+ {
+ cerr << "usage: " << argv[0] << " people.xml" << endl;
+ return 1;
+ }
+
+ int r (0);
+
+ // Initialise Xerces-C++ and XQilla.
+ //
+ XQillaPlatformUtils::initialize();
+
+ // Get the XQilla DOMImplementation object with support for XPath.
+ //
+ DOMImplementation* impl (
+ DOMImplementationRegistry::getDOMImplementation(
+ xml::string ("XPath2 3.0").c_str ()));
+
+ try
+ {
+ using namespace people;
+
+ ifstream ifs;
+ ifs.exceptions (ifstream::badbit | ifstream::failbit);
+ ifs.open (argv[1]);
+
+ // Parse the XML file to DOM using the XQilla DOMImplementation.
+ //
+ xml_schema::dom::auto_ptr<xercesc::DOMDocument> dom (
+ parse (ifs, argv[1], true, impl));
+
+ // Parse the DOM document to the object model. We also request that
+ // the DOM document to be associated with the object model.
+ //
+ std::auto_ptr<directory> d (
+ directory_ (dom,
+ xml_schema::flags::keep_dom | xml_schema::flags::own_dom));
+
+ // Obtain the root element and document corresponding to the
+ // directory object.
+ //
+ DOMElement* root (static_cast<DOMElement*> (d->_node ()));
+ DOMDocument* doc (root->getOwnerDocument ());
+
+ // Obtain namespace resolver.
+ //
+ xml_schema::dom::auto_ptr<XQillaNSResolver> resolver (
+ (XQillaNSResolver*)doc->createNSResolver (root));
+
+ // Set the namespace prefix for the people namespace that we can
+ // use reliably in XPath expressions regardless of what is used
+ // in XML documents.
+ //
+ resolver->addNamespaceBinding (
+ xml::string ("p").c_str (),
+ xml::string ("http://www.codesynthesis.com/people").c_str ());
+
+ // Create XPath expression.
+ //
+ xml_schema::dom::auto_ptr<const XQillaExpression> expr (
+ static_cast<const XQillaExpression*> (
+ doc->createExpression (
+ xml::string ("p:directory/person[age > 30]").c_str (),
+ resolver.get ())));
+
+ // Execute the query.
+ //
+ xml_schema::dom::auto_ptr<XPath2Result> r (
+ static_cast<XPath2Result*> (
+ expr->evaluate (doc, XPath2Result::ITERATOR_RESULT, 0)));
+
+ // Iterate over the result.
+ //
+ cout << "Records matching the query:" << endl;
+
+ while (r->iterateNext ())
+ {
+ const DOMNode* n (r->asNode ());
+
+ // Obtain the object model node corresponding to this DOM node.
+ //
+ person* p (
+ static_cast<person*> (
+ n->getUserData (xml_schema::dom::tree_node_key)));
+
+ // Print the data using the object model.
+ //
+ cout << endl
+ << "First : " << p->first_name () << endl
+ << "Last : " << p->last_name () << endl
+ << "Gender : " << p->gender () << endl
+ << "Age : " << p->age () << endl;
+ }
+ }
+ catch(const DOMException& e)
+ {
+ cerr << xml::transcode<char> (e.getMessage ()) << std::endl;
+ r = 1;
+ }
+ catch (const xml_schema::exception& e)
+ {
+ cerr << e << endl;
+ r = 1;
+ }
+ catch (const std::ios_base::failure&)
+ {
+ cerr << argv[1] << ": unable to open or read failure" << endl;
+ r = 1;
+ }
+
+ XQillaPlatformUtils::terminate();
+ return r;
+}
diff --git a/examples/cxx/tree/xpath/driver.cxx b/examples/cxx/tree/xpath/driver.cxx
new file mode 100644
index 0000000..b55037b
--- /dev/null
+++ b/examples/cxx/tree/xpath/driver.cxx
@@ -0,0 +1,137 @@
+// file : examples/cxx/tree/xpath/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : not copyrighted - public domain
+
+#include <memory> // std::auto_ptr
+#include <string>
+#include <fstream>
+#include <iostream>
+
+#include <xercesc/dom/DOM.hpp>
+
+#include <xqilla/xqilla-dom3.hpp>
+
+#include <xsd/cxx/xml/string.hxx> // xml::string, xml::transcode
+
+#include "dom-parse.hxx"
+
+#include "people.hxx"
+
+using namespace std;
+using namespace xercesc;
+namespace xml = xsd::cxx::xml;
+
+int
+main (int argc, char* argv[])
+{
+ if (argc != 2)
+ {
+ cerr << "usage: " << argv[0] << " people.xml" << endl;
+ return 1;
+ }
+
+ int r (0);
+
+ // Initialise Xerces-C++ and XQilla.
+ //
+ XQillaPlatformUtils::initialize();
+
+ // Get the XQilla DOMImplementation object with support for XPath.
+ //
+ DOMImplementation* impl (
+ DOMImplementationRegistry::getDOMImplementation(
+ xml::string ("XPath2 3.0").c_str ()));
+
+ try
+ {
+ using namespace people;
+
+ ifstream ifs;
+ ifs.exceptions (ifstream::badbit | ifstream::failbit);
+ ifs.open (argv[1]);
+
+ // Parse the XML file to DOM using the XQilla DOMImplementation.
+ //
+ xml_schema::dom::auto_ptr<xercesc::DOMDocument> dom (
+ parse (ifs, argv[1], true, impl));
+
+ // Parse the DOM document to the object model. We also request that
+ // the DOM document to be associated with the object model.
+ //
+ std::auto_ptr<directory> d (
+ directory_ (dom,
+ xml_schema::flags::keep_dom | xml_schema::flags::own_dom));
+
+ // Obtain the root element and document corresponding to the
+ // directory object.
+ //
+ DOMElement* root (static_cast<DOMElement*> (d->_node ()));
+ DOMDocument* doc (root->getOwnerDocument ());
+
+ // Obtain namespace resolver.
+ //
+ xml_schema::dom::auto_ptr<DOMXPathNSResolver> resolver (
+ doc->createNSResolver (root));
+
+ // Set the namespace prefix for the people namespace that we can
+ // use reliably in XPath expressions regardless of what is used
+ // in XML documents.
+ //
+ resolver->addNamespaceBinding (
+ xml::string ("p").c_str (),
+ xml::string ("http://www.codesynthesis.com/people").c_str ());
+
+ // Create XPath expression.
+ //
+ xml_schema::dom::auto_ptr<DOMXPathExpression> expr (
+ doc->createExpression (
+ xml::string ("p:directory/person[age > 30]").c_str (),
+ resolver.get ()));
+
+ // Execute the query.
+ //
+ xml_schema::dom::auto_ptr<DOMXPathResult> r (
+ expr->evaluate (doc, DOMXPathResult::ITERATOR_RESULT_TYPE, 0));
+
+ // Iterate over the result.
+ //
+ cout << "Records matching the query:" << endl;
+
+ while (r->iterateNext ())
+ {
+ DOMNode* n (r->getNodeValue ());
+
+ // Obtain the object model node corresponding to this DOM node.
+ //
+ person* p (
+ static_cast<person*> (
+ n->getUserData (xml_schema::dom::tree_node_key)));
+
+ // Print the data using the object model.
+ //
+ cout << endl
+ << "First : " << p->first_name () << endl
+ << "Last : " << p->last_name () << endl
+ << "Gender : " << p->gender () << endl
+ << "Age : " << p->age () << endl;
+ }
+ }
+ catch(const DOMException& e)
+ {
+ cerr << xml::transcode<char> (e.getMessage ()) << std::endl;
+ r = 1;
+ }
+ catch (const xml_schema::exception& e)
+ {
+ cerr << e << endl;
+ r = 1;
+ }
+ catch (const std::ios_base::failure&)
+ {
+ cerr << argv[1] << ": unable to open or read failure" << endl;
+ r = 1;
+ }
+
+ XQillaPlatformUtils::terminate();
+ return r;
+}
diff --git a/examples/cxx/tree/xpath/makefile b/examples/cxx/tree/xpath/makefile
new file mode 100644
index 0000000..5e73cfb
--- /dev/null
+++ b/examples/cxx/tree/xpath/makefile
@@ -0,0 +1,71 @@
+# file : examples/cxx/tree/xpath/makefile
+# author : Boris Kolpackov <boris@codesynthesis.com>
+# copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC
+# license : GNU GPL v2 + exceptions; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../../../../build/bootstrap.make
+
+xsd := people.xsd
+cxx := driver.cxx dom-parse.cxx
+
+obj := $(addprefix $(out_base)/,$(cxx:.cxx=.o) $(xsd:.xsd=.o))
+dep := $(obj:.o=.o.d)
+
+driver := $(out_base)/driver
+clean := $(out_base)/.clean
+
+
+# Import.
+#
+$(call import,\
+ $(scf_root)/import/libxerces-c/stub.make,\
+ l: xerces_c.l,cpp-options: xerces_c.l.cpp-options)
+
+$(call import,\
+ $(scf_root)/import/libxqilla/stub.make,\
+ l: xqilla.l,cpp-options: xqilla.l.cpp-options)
+
+
+# Build.
+#
+$(driver): $(obj) $(xqilla.l) $(xerces_c.l)
+
+$(obj) $(dep): cpp_options := -I$(src_root)/libxsd
+$(obj) $(dep): $(xqilla.l.cpp-options) $(xerces_c.l.cpp-options)
+
+$(out_base)/$(xsd:.xsd=.hxx) \
+$(out_base)/$(xsd:.xsd=.ixx) \
+$(out_base)/$(xsd:.xsd=.cxx): xsd := $(out_root)/xsd/xsd
+
+$(out_base)/$(xsd:.xsd=.hxx) \
+$(out_base)/$(xsd:.xsd=.ixx) \
+$(out_base)/$(xsd:.xsd=.cxx): $(out_root)/xsd/xsd
+
+$(call include-dep,$(dep))
+
+# Convenience alias for default target.
+#
+.PHONY: $(out_base)/
+$(out_base)/: $(driver)
+
+
+# Clean.
+#
+.PHONY: $(clean)
+
+$(clean): $(driver).o.clean \
+ $(addsuffix .cxx.clean,$(obj)) \
+ $(addsuffix .cxx.clean,$(dep)) \
+ $(addprefix $(out_base)/,$(xsd:.xsd=.cxx.xsd.clean))
+
+
+# How to.
+#
+$(call include,$(bld_root)/cxx/o-e.make)
+$(call include,$(bld_root)/cxx/cxx-o.make)
+$(call include,$(bld_root)/cxx/cxx-d.make)
+$(call include,$(scf_root)/xsd/tree/xsd-cxx.make)
+
+# Dependencies.
+#
+$(call import,$(src_root)/xsd/makefile)
diff --git a/examples/cxx/tree/xpath/people.xml b/examples/cxx/tree/xpath/people.xml
new file mode 100644
index 0000000..24af876
--- /dev/null
+++ b/examples/cxx/tree/xpath/people.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : examples/cxx/tree/xpath/people.xml
+author : Boris Kolpackov <boris@codesynthesis.com>
+copyright : not copyrighted - public domain
+
+-->
+
+<ppl:directory xmlns:ppl="http://www.codesynthesis.com/people"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.codesynthesis.com/people people.xsd">
+
+ <person>
+ <first-name>John</first-name>
+ <last-name>Doe</last-name>
+ <gender>male</gender>
+ <age>32</age>
+ </person>
+
+ <person>
+ <first-name>Jane</first-name>
+ <last-name>Doe</last-name>
+ <gender>female</gender>
+ <age>28</age>
+ </person>
+
+</ppl:directory>
diff --git a/examples/cxx/tree/xpath/people.xsd b/examples/cxx/tree/xpath/people.xsd
new file mode 100644
index 0000000..12c8aef
--- /dev/null
+++ b/examples/cxx/tree/xpath/people.xsd
@@ -0,0 +1,39 @@
+<?xml version="1.0"?>
+
+<!--
+
+file : examples/cxx/tree/xpath/people.xsd
+author : Boris Kolpackov <boris@codesynthesis.com>
+copyright : not copyrighted - public domain
+
+-->
+
+<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+ xmlns:ppl="http://www.codesynthesis.com/people"
+ targetNamespace="http://www.codesynthesis.com/people">
+
+ <xsd:simpleType name="gender">
+ <xsd:restriction base="xsd:string">
+ <xsd:enumeration value="male"/>
+ <xsd:enumeration value="female"/>
+ </xsd:restriction>
+ </xsd:simpleType>
+
+ <xsd:complexType name="person">
+ <xsd:sequence>
+ <xsd:element name="first-name" type="xsd:string"/>
+ <xsd:element name="last-name" type="xsd:string"/>
+ <xsd:element name="gender" type="ppl:gender"/>
+ <xsd:element name="age" type="xsd:unsignedShort"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ <xsd:complexType name="directory">
+ <xsd:sequence>
+ <xsd:element name="person" type="ppl:person" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ <xsd:element name="directory" type="ppl:directory"/>
+
+</xsd:schema>