【问题标题】:Xslt Concatenate two nodelists with seperatorXslt 用分隔符连接两个节点列表
【发布时间】:2019-11-04 19:46:26
【问题描述】:

我正在尝试将两个具有子元素的节点列表与字符串连接连接起来,但我丢失了子元素中的 xml 标记

输入: 节点1:hello I am trying <abc> some </abc> xslt code

节点2:but not working

预期输出

hello I am trying <abc> some </abc> xslt code, but not working

【问题讨论】:

  • 假设节点 1 或节点 2 中的一个词周围有 xml 标签。我添加了它们,但有些怎么看不到
  • 您能否向我们展示您的输入 XML 和您当前创建/拥有这两个节点“列表”的 XSLT?您能否解释一下您是在寻找 XSLT 代码中的中间结果还是最终转换结果?

标签: string xslt concatenation nodes element


【解决方案1】:

如果您有一个包含您显示的内容的两个元素节点的序列,并且您希望在这些节点之间创建一个带有分隔符 , 的输出,那么一种方法是通过添加分隔符的模板推送元素:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="root">
      <xsl:param name="seq1" select="node1, node2"/>
      <xsl:copy>
          <xsl:apply-templates select="$seq1"/>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="node1 | node2">
      <xsl:if test="position() > 1">, </xsl:if>
      <xsl:apply-templates/>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/pPJ8LVh

如果您想使用string-join 在字符串级别上工作,那么您首先需要序列化内容,例如

  <xsl:output method="text"/>

  <xsl:template match="root">
      <xsl:param name="seq1" select="node1, node2"/>
      <xsl:copy>
          <xsl:value-of select="$seq1 ! serialize(node())" separator=", "/>
      </xsl:copy>
  </xsl:template>

https://xsltfiddle.liberty-development.net/pPJ8LVh/1

  <xsl:output method="text"/>

  <xsl:template match="root">
      <xsl:param name="seq1" select="node1, node2"/>
      <xsl:variable name="str1" select="string-join($seq1 ! serialize(node()), ', ')"/>
      <xsl:copy>
          <xsl:value-of select="$str1"/>
      </xsl:copy>
  </xsl:template>

https://xsltfiddle.liberty-development.net/pPJ8LVh/2

如您所见,最后两个示例将字符串输出创建为文本,您似乎更可能希望创建结果节点,就像第一个建议中所做的那样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 2013-10-06
    • 2011-10-15
    相关资源
    最近更新 更多