XQuilla/docs/xqc-api/xqc-context-item_8c-example.html
2020-02-17 22:23:50 +01:00

84 lines
7.6 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>XQilla XQC API Documentation</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<a style="float:right;" href="http://sourceforge.net/projects/xqilla"><img src="http://sflogo.sourceforge.net/sflogo.php?group_id=152021&amp;type=2" border="0" alt="SourceForge.net Logo" /></a>
<!-- Generated by Doxygen 1.6.1 -->
<div class="navigation" id="top">
<div class="tabs">
<ul>
<li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
<li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li><a href="examples.html"><span>Examples</span></a></li>
</ul>
</div>
</div>
<div class="contents">
<h1>xqc-context-item.c</h1><p>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.</p>
<div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;<a class="code" href="xqilla-xqc_8h.html">xqilla/xqilla-xqc.h</a>&gt;</span>
<span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])
{
<a name="_a0"></a><a class="code" href="structXQC__Implementation__s.html" title="The XQC_Implementation struct provides factory functions for preparing queries.">XQC_Implementation</a> *impl;
<a name="_a1"></a><a class="code" href="structXQC__Expression__s.html" title="The XQC_Expression struct represents a prepared query, and allows the user to execute...">XQC_Expression</a> *expr;
<a name="_a2"></a><a class="code" href="structXQC__DynamicContext__s.html">XQC_DynamicContext</a> *context;
<a name="_a3"></a><a class="code" href="structXQC__Sequence__s.html">XQC_Sequence</a> *seq, *doc;
<a class="code" href="xqc_8h.html#aeb1ad206e9c38a7f84749cafe04d7a3f" title="The error enumeration used by all XQC functions to designate error condition.">XQC_Error</a> err;
<span class="keyword">const</span> <span class="keywordtype">char</span> *value;
<span class="comment">// XQilla specific way to create an XQC_Implementation struct</span>
impl = <a name="a4"></a><a class="code" href="xqilla-xqc_8h.html#acd9659e93bbfafcd0cac9d7c4c12e13c" title="Creates an XQC_Implementation object that uses XQilla.">createXQillaXQCImplementation</a>(<a name="a5"></a><a class="code" href="xqc_8h.html#a8fbc00dfcf0a3527e0ccba36322e1c84" title="The version of the XQC API in this header file.">XQC_VERSION_NUMBER</a>);
<span class="keywordflow">if</span>(impl == 0) <span class="keywordflow">return</span> 1;
<span class="comment">// Parse an XQuery expression</span>
err = impl-&gt;<a name="a6"></a><a class="code" href="structXQC__Implementation__s.html#a4f82aa0b0e5c675f95f6c78b3bf76216" title="Prepares a query from a UTF-8 string, returning an XQC_Expression object.">prepare</a>(impl, <span class="stringliteral">&quot;foo/bar/@baz&quot;</span>, 0, &amp;expr);
<span class="keywordflow">if</span>(err != 0) <span class="keywordflow">goto</span> free_impl;
<span class="comment">// Parse a document</span>
err = impl-&gt;<a name="a7"></a><a class="code" href="structXQC__Implementation__s.html#a1e3a2cd7fdb4becb469d179bcb4c2396" title="XQC_PARSE_ERROR.">parse_document</a>(impl, <span class="stringliteral">&quot;&lt;foo&gt;&lt;bar baz=&apos;hello&apos;/&gt;&lt;/foo&gt;&quot;</span>, &amp;doc);
<span class="keywordflow">if</span>(err != 0) <span class="keywordflow">goto</span> free_expr;
<span class="comment">// Create a dynamic context</span>
err = expr-&gt;<a name="a8"></a><a class="code" href="structXQC__Expression__s.html#a04be56651df9f3f47aecd99a0b0ad66e" title="Creates a dynamic context suitable for use in the execute() function.">create_context</a>(expr, &amp;context);
<span class="keywordflow">if</span>(err != 0) <span class="keywordflow">goto</span> free_doc;
<span class="comment">// Set the document as the context item</span>
doc-&gt;<a name="a9"></a><a class="code" href="structXQC__Sequence__s.html#ab84594b06f7e69fd870c41f55ae5c6c6" title="Moves the XQC_Sequence so that the current item is positioned at the next item in...">next</a>(doc);
context-&gt;<a name="a10"></a><a class="code" href="structXQC__DynamicContext__s.html#a0c69d262bfe4fd038832db4e4db600af" title="Sets the context item to the current item of the XQC_Sequence given.">set_context_item</a>(context, doc);
<span class="comment">// Execute the query</span>
err = expr-&gt;<a name="a11"></a><a class="code" href="structXQC__Expression__s.html#a3181579fa2677d43c29efd31a649726c" title="Executes the query represented by the XQC_Expression object using the values in the...">execute</a>(expr, context, &amp;seq);
<span class="keywordflow">if</span>(err != 0) <span class="keywordflow">goto</span> free_context;
<span class="comment">// Iterate over the results, printing them</span>
<span class="keywordflow">while</span>((err = seq-&gt;<a class="code" href="structXQC__Sequence__s.html#ab84594b06f7e69fd870c41f55ae5c6c6" title="Moves the XQC_Sequence so that the current item is positioned at the next item in...">next</a>(seq)) == <a name="a12"></a><a class="code" href="xqc_8h.html#aeb1ad206e9c38a7f84749cafe04d7a3fa975a11c6557d4d849e48f4034380a375" title="No error.">XQC_NO_ERROR</a>) {
seq-&gt;<a name="a13"></a><a class="code" href="structXQC__Sequence__s.html#a3bd3b6dab0c68652af816438407dd037" title="Returns the string value of the current item in the sequence - this is equivalent...">string_value</a>(seq, &amp;value);
printf(<span class="stringliteral">&quot;%s\n&quot;</span>, value);
}
<span class="keywordflow">if</span>(err == <a name="a14"></a><a class="code" href="xqc_8h.html#aeb1ad206e9c38a7f84749cafe04d7a3fadfd1d8eea45c90655b98cfd728bfee2e" title="The end of the XQC_Sequence has been reached.">XQC_END_OF_SEQUENCE</a>)
err = <a class="code" href="xqc_8h.html#aeb1ad206e9c38a7f84749cafe04d7a3fa975a11c6557d4d849e48f4034380a375" title="No error.">XQC_NO_ERROR</a>;
<span class="comment">// free everything</span>
seq-&gt;<a name="a15"></a><a class="code" href="structXQC__Sequence__s.html#a13ab651edb4e0754cb8a032e2bce50b0" title="Called to free the resources associated with the XQC_Sequence.">free</a>(seq);
free_context:
context-&gt;<a name="a16"></a><a class="code" href="structXQC__DynamicContext__s.html#ad40117a7e2dd660ea7478c4cc5124eab" title="Called to free the resources associated with the XQC_DynamicContext.">free</a>(context);
free_doc:
doc-&gt;<a class="code" href="structXQC__Sequence__s.html#a13ab651edb4e0754cb8a032e2bce50b0" title="Called to free the resources associated with the XQC_Sequence.">free</a>(doc);
free_expr:
expr-&gt;<a name="a17"></a><a class="code" href="structXQC__Expression__s.html#a6aa28a94f0ff6732c26f131bbe4d44a9" title="Called to free the resources associated with the XQC_Expression.">free</a>(expr);
free_impl:
impl-&gt;<a name="a18"></a><a class="code" href="structXQC__Implementation__s.html#afb636b6cbd236831652a518d9e71418d" title="Called to free the resources associated with the XQC_Implementation.">free</a>(impl);
<span class="keywordflow">return</span> err;
}
</pre></div> </div>
<hr size="1"/><address style="text-align: right;"><small>Generated on 8 Jul 2015 for XQilla XQC API by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address>
</body>
</html>