XQuilla/docs/xqc-api/xqc-context-item_8c-example.html

88 lines
7.6 KiB
HTML
Raw Normal View History

2020-02-17 21:17:06 +00:00
<!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>
2020-02-17 21:22:42 +00:00
<!-- Generated by Doxygen 1.7.3 -->
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main&#160;Page</span></a></li>
2020-02-17 21:19:57 +00:00
<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>
2020-02-17 21:22:42 +00:00
<div class="header">
<div class="headertitle">
<h1>xqc-context-item.c</h1> </div>
</div>
2020-02-17 21:19:57 +00:00
<div class="contents">
2020-02-17 21:22:42 +00:00
<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>
2020-02-17 21:17:06 +00:00
<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[])
{
2020-02-17 21:19:57 +00:00
<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;
2020-02-17 21:22:42 +00:00
<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 that query any ...">XQC_Expression</a> *expr;
2020-02-17 21:19:57 +00:00
<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;
2020-02-17 21:17:06 +00:00
<span class="keyword">const</span> <span class="keywordtype">char</span> *value;
<span class="comment">// XQilla specific way to create an XQC_Implementation struct</span>
2020-02-17 21:19:57 +00:00
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>);
2020-02-17 21:17:06 +00:00
<span class="keywordflow">if</span>(impl == 0) <span class="keywordflow">return</span> 1;
<span class="comment">// Parse an XQuery expression</span>
2020-02-17 21:19:57 +00:00
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);
2020-02-17 21:17:06 +00:00
<span class="keywordflow">if</span>(err != 0) <span class="keywordflow">goto</span> free_impl;
<span class="comment">// Parse a document</span>
2020-02-17 21:22:42 +00:00
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=&#39;hello&#39;/&gt;&lt;/foo&gt;&quot;</span>, &amp;doc);
2020-02-17 21:17:06 +00:00
<span class="keywordflow">if</span>(err != 0) <span class="keywordflow">goto</span> free_expr;
<span class="comment">// Create a dynamic context</span>
2020-02-17 21:19:57 +00:00
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);
2020-02-17 21:17:06 +00:00
<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>
2020-02-17 21:22:42 +00:00
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 the sequence...">next</a>(doc);
2020-02-17 21:19:57 +00:00
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);
2020-02-17 21:17:06 +00:00
<span class="comment">// Execute the query</span>
2020-02-17 21:22:42 +00:00
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 XQC_DynamicContex...">execute</a>(expr, context, &amp;seq);
2020-02-17 21:17:06 +00:00
<span class="keywordflow">if</span>(err != 0) <span class="keywordflow">goto</span> free_context;
<span class="comment">// Iterate over the results, printing them</span>
2020-02-17 21:22:42 +00:00
<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 the sequence...">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 to calling fn:strin...">string_value</a>(seq, &amp;value);
2020-02-17 21:19:57 +00:00
printf(<span class="stringliteral">&quot;%s\n&quot;</span>, value);
2020-02-17 21:17:06 +00:00
}
2020-02-17 21:19:57 +00:00
<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>;
2020-02-17 21:17:06 +00:00
<span class="comment">// free everything</span>
2020-02-17 21:19:57 +00:00
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);
2020-02-17 21:17:06 +00:00
free_context:
2020-02-17 21:19:57 +00:00
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);
2020-02-17 21:17:06 +00:00
free_doc:
2020-02-17 21:19:57 +00:00
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);
2020-02-17 21:17:06 +00:00
free_expr:
2020-02-17 21:19:57 +00:00
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);
2020-02-17 21:17:06 +00:00
free_impl:
2020-02-17 21:19:57 +00:00
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);
2020-02-17 21:17:06 +00:00
<span class="keywordflow">return</span> err;
}
2020-02-17 21:19:57 +00:00
</pre></div> </div>
2020-02-17 21:22:42 +00:00
</div>
<hr class="footer"/><address class="footer"><small>Generated on Mon Oct 31 2011 19:08:38 for XQilla XQC API by&#160;
2020-02-17 21:17:06 +00:00
<a href="http://www.doxygen.org/index.html">
2020-02-17 21:22:42 +00:00
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.3 </small></address>
2020-02-17 21:17:06 +00:00
</body>
</html>