SourceForge.net Logo
simple-context-item.cpp

This example parses a document and sets it as the context item.

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
AutoDelete<DynamicContext> context(query->createDynamicContext());
// 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;
}
Definition XPath2MemoryManager.hpp:261
virtual bool isNode() const =0
virtual const XMLCh * asString(const DynamicContext *context) const =0
virtual Item::Ptr next(DynamicContext *context)
Get the next item from the iterator. Returns null if the is no next value.
A scoped pointer wrapper for the lazily evaluated query result.
Definition Result.hpp:38
An eagerly evaluated result of a query execution.
Definition Sequence.hpp:40
const Item::Ptr & first() const
bool isEmpty() const
Returns true if the list is empty.
Provides factory methods for creating XQQuery and DynamicContext objects.
Definition XQilla.hpp:53
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.