How to return text from several elements in one string using XPath?

后端 未结 2 654
被撕碎了的回忆
被撕碎了的回忆 2020-12-19 03:20

I would like to use XPath to extract all the text from all the

  • elements, that are in the specialList list and return one string separate
  • 相关标签:
    2条回答
    • 2020-12-19 03:33

      Though this possible using XSLT 1.0

      <xsl:for-each select="ul/li">
         <xsl:value-of select="."/>
         <xsl:if test="position() != last()">
             <xsl:text>,</xsl:text>
         </xsl:if>
      </xsl:for-each>
      
      0 讨论(0)
    • 2020-12-19 03:36

      In XPath 1.0, this is only possible if you know the number of elements in advance using concat(...):

      concat(//li[1], ', ', //li[2], ', ', //li[3], ', ', //li[4])
      

      If you're lucky, you can just return all result strings for //li/text() and set the output parameters of your XPath processor to concatenate them like you want. This depends on the processor, so there is no general solution and this is no way to go if you want to further process the results within XPath.

      In XPath 2.0, you can use fn:string-join($sequence, $delemiter) for input of arbitrary length:

      fn:string-join(//li, ', ')
      
      0 讨论(0)
    提交回复
    热议问题