首页 文章

XSLT 2.0如何在输出上的tokenize()中测试position()

提问于
浏览
1

在XSLT 2.0中,我有一个参数,而不是作为分隔的文档名称字符串,如: ms609_0080.xml~ms609_0176.xml~ms609_0210.xml~ms609_0418.xml

tokenize() 这个字符串并循环通过它 xsl:for-each 将每个文件传递给 key . 然后,来自键的结果汇编成逗号分隔的字符串以输出到屏幕 .

<xsl:variable name="list_of_corresp_events">
   <xsl:variable name ="tokenparam" select="tokenize($paramCorrespdocs,'~')"/>
   <xsl:for-each select="$tokenparam">
      <xsl:choose>
          <xsl:when test=".[position() != last()]">
               <xsl:value-of select="document(concat($paramSaxondatapath, .))/(key('correspkey',$correspid))/@xml:id"/>
          </xsl:when>
          <xsl:otherwise>
               <xsl:value-of select="concat(document(concat($paramSaxondatapath, .))/(key('correspkey',$correspid))/@xml:id, ', ')"/>
          </xsl:otherwise>
      </xsl:choose>
   </xsl:for-each>
</xsl:variable>

一切正常,但是当我输出变量 $list_of_corresp_events 时,它看起来如下,带有意外的尾随逗号:

ms609-0080-2, ms609-0176-1, ms609-0210-1, ms609-0418-1,

通常最后一个逗号不应该基于 test=".[position() != last()]" 出现?可能会立即申请 string-join() 以此方式申请 string-join() .

非常感谢 .

3 回答

  • 1

    从@ zx485改进解决方案,试试吧

    <xsl:for-each select="$tokenparam">
       <xsl:if test="position()!=1">, </xsl:if>
       <xsl:value-of select="document(concat($paramSaxondatapath, .))/(key('correspkey',$correspid))/@xml:id"/>
    </xsl:for-each>
    

    这里有两件事:

    (a)您不需要在两个条件分支中重复相同的代码

    (b)它是's more efficient to output the comma separator before every item except the first, rather than after every item except the last. That'因为评估 last() 涉及昂贵的预见 .

  • 1

    看来你可以简化这个

    <xsl:variable name="list_of_corresp_events">
       <xsl:value-of select="for $t in tokenize($paramCorrespdocs,'~') document(concat($paramSaxondatapath, $))/(key('correspkey',$correspid))/@xml:id" separator=", "/>
    </xsl:variable>
    

    或者 string-join

    <xsl:variable name="list_of_corresp_events" select="string-join(for $t in tokenize($paramCorrespdocs,'~') document(concat($paramSaxondatapath, $))/(key('correspkey',$correspid))/@xml:id, ', ')"/>
    
  • 2

    更改

    <xsl:when test=".[position() != last()]">
    

    <xsl:when test="position() != last()">
    

    然后它应该按照需要工作 .

相关问题