SourceForge.net Logo
simple-context-item.cpp

This example parses a document and sets it as the context item. It then executes an XQuery expression that navigates relative to the context item.

#include <iostream>
int main(int argc, char *argv[]) {
// Initialise Xerces-C and XQilla by creating the factory object
XQilla xqilla;
// Parse an XQuery expression
// (AutoDelete deletes the object at the end of the scope)
AutoDelete<XQQuery> query(xqilla.parse(X("foo/bar/@baz")));
// Create a context object
// Parse a document, and set it as the context item
Sequence seq = context->resolveDocument(X("foo.xml"));
if(!seq.isEmpty() && seq.first()->isNode()) {
context->setContextItem(seq.first());
context->setContextPosition(1);
context->setContextSize(1);
}
// Execute the query, using the context
Result result = query->execute(context);
// Iterate over the results, printing them
Item::Ptr item;
while(item = result->next(context)) {
std::cout << UTF8(item->asString(context)) << std::endl;
}
return 0;
}
Sequence.hpp
XQQuery::execute
Result execute(DynamicContext *context) const
Executes the query using the given DynamicContext, returning a lazy iterator over the results.
DynamicContext::setContextItem
virtual void setContextItem(const Item::Ptr &item)=0
Set the context item to item.
RefCountPointer< const Item >
XQQuery::createDynamicContext
DynamicContext * createDynamicContext(xercesc::MemoryManager *memMgr=xercesc::XMLPlatformUtils::fgMemoryManager) const
Creates a DynamicContext based on the static context used to parse this query.
Sequence::isEmpty
bool isEmpty() const
Returns true if the list is empty.
DynamicContext::setContextSize
virtual void setContextSize(size_t size)=0
Set the context size.
XQilla::parse
static XQQuery * parse(const XMLCh *query, DynamicContext *context=0, const XMLCh *queryFile=NULL, unsigned int flags=0, xercesc::MemoryManager *memMgr=xercesc::XMLPlatformUtils::fgMemoryManager, XQQuery *result=0)
Parse the expression contained in the given query string.
Item::asString
virtual const XMLCh * asString(const DynamicContext *context) const =0
Sequence
An eagerly evaluated result of a query execution.
Definition: Sequence.hpp:40
Item::isNode
virtual bool isNode() const =0
DynamicContext::setContextPosition
virtual void setContextPosition(size_t pos)=0
Set the context position.
Result
A scoped pointer wrapper for the lazily evaluated query result.
Definition: Result.hpp:38
ResultImpl::next
virtual Item::Ptr next(DynamicContext *context)
Get the next item from the iterator. Returns null if the is no next value.
DynamicContext::resolveDocument
virtual Sequence resolveDocument(const XMLCh *uri, const LocationInfo *location=0, const QueryPathNode *projection=0)=0
Resolve the given uri (and baseUri) to an XML document.
XQilla
Provides factory methods for creating XQQuery and DynamicContext objects.
Definition: XQilla.hpp:53
xqilla-simple.hpp
Sequence::first
const Item::Ptr & first() const
AutoDelete
Definition: XPath2MemoryManager.hpp:261