【问题标题】:XQuery - output series of HTML elements with separatorXQuery - 输出一系列带有分隔符的 HTML 元素
【发布时间】:2019-04-09 23:33:43
【问题描述】:

在 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>

但我希望它输出以逗号分隔的&lt;a href=""&gt; 列表:

     <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 '')

非常感谢您的任何建议。

【问题讨论】:

    标签: xquery exist-db xquery-3.0


    【解决方案1】:

    我不确定 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
    

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

     (
     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 (), .) 
    

    离你的第二次尝试更近一点。

    【讨论】:

    • 我刚刚使用 xquery 的for $x at $position in $y 编辑了我的问题,count 是一个用于分配变量的函数。 order by 现在奇怪地被 XQuery 拒绝为基数问题。
    • 那将是另一个问题,您还没有显示您在新尝试中下订单的位置。我的两个建议都没有改变你的原始代码,只是将它们合并到更大的表达式中。
    • 我刚试过你的第二个,它有效。这是一个令人着迷的解决方案,我只理解了一半。 ! 在做什么让(if (position() &gt; 1) then (',', .) else .) 运行?
    • ! 是 XPath 3 和 XQuery 3 中引入的所谓的“映射”运算符w3.org/TR/xquery-31/#id-map-operator,用于将左侧序列的每个项目映射到右侧的表达式。跨度>
    • 因此,用简单的语言来说,query result 是一个序列(! 的左侧),该结果映射到(右侧)XSLT position() 以确定是否应该基于此完成?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    相关资源
    最近更新 更多