XQuery-带分隔符的HTML元素的输出系列

时间:2018-11-06 14:44:39

标签: xquery exist-db xquery-3.0

在XQuery 3.1中,我正在构造一个HTML表。在一个<td>元素中,我输出一系列<a ref="">

所以,目前这个简单的for

 <td>
   {
     for $b in collection($globalvar:URIdata)/tei:TEI/tei:text//tei:seg[@type="dep_event" 
                           and @corresp = $a/data(@corresp)
                           and @xml:id != $a/data(@xml:id)] 

     order by $b/data(@xml:id)

     return <a href="{concat($globalvar:URLdoc,$b/ancestor::tei:TEI/tei:text/@xml:id)}">{$b/data(@xml:id)}</a>                        
    }
 </td>

输出以下内容:

     <td>
      <a href="http://localhost:8081/exist/apps/deheresi/doc/MS609-0006">MS609-0006-8</a>
      <a href="http://localhost:8081/exist/apps/deheresi/doc/MS609-0419">MS609-0419-5</a>
      <a href="http://localhost:8081/exist/apps/deheresi/doc/MS609-0613">MS609-0613-4</a>
    </td>

但是我希望它输出以逗号分隔的<a href="">列表:

     <td>
      <a href="http://localhost:8081/exist/apps/deheresi/doc/MS609-0006">MS609-0006-8</a>, 
      <a href="http://localhost:8081/exist/apps/deheresi/doc/MS609-0419">MS609-0419-5</a>, 
      <a href="http://localhost:8081/exist/apps/deheresi/doc/MS609-0613">MS609-0613-4</a>
    </td>

编辑:下面的方法可以工作...但是不能按期望的顺序输出结果,并且由于“基数”问题(该错误未在原始消息中弹出),我无法使用order by $b/data(@xml:id)进行处理。

    let $coll := collection($globalvar:URIdata)/tei:TEI/tei:text//tei:seg[@type="dep_event" 
                     and @corresp = $a/data(@corresp)
                     and @xml:id != $a/data(@xml:id)] 

     let $last := count($coll)

     for $b at $position in $coll 

     return (<a href="{concat($globalvar:URLdoc,$b/ancestor::tei:TEI/tei:text/@xml:id)}">{$b/data(@xml:id)}</a>,
           if ($position ne $last) then ', ' else '')

在此先感谢您的任何建议。

1 个答案:

答案 0 :(得分:3)

我不确定XQuery中是否有一个通用的习惯用法,但我认为使用

 for $b in collection($globalvar:URIdata)/tei:TEI/tei:text//tei:seg[@type="dep_event" 
                       and @corresp = $a/data(@corresp)
                       and @xml:id != $a/data(@xml:id)] 

 order by $b/data(@xml:id)
 count $p
 let $a := <a href="{concat($globalvar:URLdoc,$b/ancestor::tei:TEI/tei:text/@xml:id)}">{$b/data(@xml:id)}</a>
 return 
   if ($p > 1)
   then (',', $a)
   else $a

是一种可能的方法,与旧的XSLT方法非常相似,具有<xsl:for-each select="$nodes"><xsl:if test="position() > 1">,</xsl:if><xsl:copy-of select="."/></xsl:for-each>。更接近您也可以尝试

 (
 for $b in collection($globalvar:URIdata)/tei:TEI/tei:text//tei:seg[@type="dep_event" 
                       and @corresp = $a/data(@corresp)
                       and @xml:id != $a/data(@xml:id)] 

 order by $b/data(@xml:id)

 return <a href="{concat($globalvar:URLdoc,$b/ancestor::tei:TEI/tei:text/@xml:id)}">{$b/data(@xml:id)}</a>
 ) ! (if (position() > 1) then (',', .) else .)  

那也可以写成

 (
 for $b in collection($globalvar:URIdata)/tei:TEI/tei:text//tei:seg[@type="dep_event" 
                       and @corresp = $a/data(@corresp)
                       and @xml:id != $a/data(@xml:id)] 

 order by $b/data(@xml:id)

 return <a href="{concat($globalvar:URLdoc,$b/ancestor::tei:TEI/tei:text/@xml:id)}">{$b/data(@xml:id)}</a>
 ) ! (if (position() > 1) then ',' else (), .) 

离您的第二次尝试更近了。

相关问题