Marklogic学习 —— XQuery语法基础4

论坛 期权论坛     
匿名小用户   2019-10-20 22:55   2662   0
<!-- flowchart 箭头图标 勿删 -->
                    <svg style="display: none;">
                        <path d="M5,0 0,2.5 5,5z" id="raphael-marker-block" stroke-linecap="round" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                    </svg>
                                            <p>1.一个完整的FLWOR语法</p>



<pre class="blockcode"><code class="language-xquery hljs lasso">for <span class="hljs-variable">$prod</span> <span class="hljs-keyword">in</span> doc(<span class="hljs-string">"/product/catalog.xml"</span>)<span class="hljs-comment">//product</span>
<span class="hljs-keyword">let</span> <span class="hljs-variable">$prodDept</span> <span class="hljs-subst">:=</span> <span class="hljs-variable">$prod</span><span class="hljs-subst">/</span>@dept
<span class="hljs-keyword">where</span> <span class="hljs-variable">$prodDept</span> <span class="hljs-subst">=</span> <span class="hljs-string">"南极人"</span> <span class="hljs-literal">or</span> <span class="hljs-variable">$prodDept</span> <span class="hljs-subst">=</span> <span class="hljs-string">"果国际"</span>
<span class="hljs-keyword">return</span> <span class="hljs-variable">$prod</span>/name</code></pre>

<p>返回:</p>



<pre class="blockcode"><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-title">name</span> <span class="hljs-attribute">language</span>=<span class="hljs-value">"cn"</span>&gt;</span>毛呢外套<span class="hljs-tag">&lt;/<span class="hljs-title">name</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">name</span> <span class="hljs-attribute">language</span>=<span class="hljs-value">"cn"</span>&gt;</span>牛仔裤<span class="hljs-tag">&lt;/<span class="hljs-title">name</span>&gt;</span></code></pre>

<p>2.范围表达式</p>



<pre class="blockcode"><code class="language-xquery hljs bash"><span class="hljs-keyword">for</span> <span class="hljs-variable">$i</span> <span class="hljs-keyword">in</span> <span class="hljs-number">1</span> to <span class="hljs-number">3</span>
<span class="hljs-keyword">return</span> &lt;ele&gt;{<span class="hljs-variable">$i</span>}&lt;/ele&gt;</code></pre>

<p>返回:</p>



<pre class="blockcode"><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-title">ele</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-title">ele</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">ele</span>&gt;</span>2<span class="hljs-tag">&lt;/<span class="hljs-title">ele</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">ele</span>&gt;</span>3<span class="hljs-tag">&lt;/<span class="hljs-title">ele</span>&gt;</span></code></pre>

<p>3.多个for语句叠加</p>

<pre class="blockcode"><code class="hljs ruby"><span class="hljs-keyword">for</span> <span class="hljs-variable">$i</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)
<span class="hljs-keyword">for</span> <span class="hljs-variable">$j</span> <span class="hljs-keyword">in</span> (<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>)
<span class="hljs-keyword">return</span>&lt;ele&gt;i is {<span class="hljs-variable">$i</span>} <span class="hljs-keyword">and</span> j is {<span class="hljs-variable">$j</span>}&lt;<span class="hljs-regexp">/ele&gt;</span></code></pre>

<p>返回:</p>



<pre class="blockcode"><code class="hljs vbnet">&lt;ele&gt;i <span class="hljs-keyword">is</span> <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> j <span class="hljs-keyword">is</span> a&lt;/ele&gt;
&lt;ele&gt;i <span class="hljs-keyword">is</span> <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> j <span class="hljs-keyword">is</span> b&lt;/ele&gt;
&lt;ele&gt;i <span class="hljs-keyword">is</span> <span class="hljs-number">2</span> <span class="hljs-keyword">and</span> j <span class="hljs-keyword">is</span> a&lt;/ele&gt;
&lt;ele&gt;i <span class="hljs-keyword">is</span> <span class="hljs-number">2</span> <span class="hljs-keyword">and</span> j <span class="hljs-keyword">is</span> b&lt;/ele&gt;</code></pre>

<p>4.一个for语句绑定多个变量</p>

<pre class="blockcode"><code class="hljs ruby"><span class="hljs-keyword">for</span> <span class="hljs-variable">$i</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>), <span class="hljs-variable">$j</span> <span class="hljs-keyword">in</span> (<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>)
<span class="hljs-keyword">return</span> &lt;ele&gt;i is {<span class="hljs-variable">$i</span>} <span class="hljs-keyword">and</span> j is {<span class="hljs-variable">$j</span>}&lt;<span class="hljs-regexp">/ele&gt;</span></code></pre>

<p>返回:</p>

<pre class="blockcode"><code class="hljs vbnet">&lt;ele&gt;i <span class="hljs-keyword">is</span> <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> j <span class="hljs-keyword">is</span> a&lt;/ele&gt;
&lt;ele&gt;i <span class="hljs-keyword">is</span> <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> j <span class="hljs-keyword">is</span> b&lt;/ele&gt;
&lt;ele&gt;i <span class="hljs-keyword">is</span> <span class="hljs-number">2</span> <span class="hljs-keyword">and</span> j <span class="hljs-keyword">is</span> a&lt;/ele&gt;
&lt;ele&gt;i <span class="
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:50
帖子:1079
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP