需要组合所有元素值

时间:2017-08-04 07:12:29

标签: xml xslt xslt-2.0

我想将标签元素值组合成单个值

Xml我有:

<h1>aaa</h1>
<h1>bbb</h1>
<h1>ccc</h1>

我使用的XSL:

   <xsl:template match="h1[h1]">
      <h1><xsl:value-of select="h1"/></h1>
   </xsl:template>

但我现在喜欢

<h1>aaa</h1>
<h1>bbb</h1>
<h1>ccc</h1>

但我需要像:

<h1>aaa bbb ccc</h1>

请提供一些代码。提前致谢

1 个答案:

答案 0 :(得分:1)

你的代码:<h1><xsl:value-of select="h1"/></h1>没问题,但是你写了 在错误的地方。

您应该在与标记匹配的模板中使用它(包含h1 标记)并为h1添加一个空模板,以防止原始文件的再现 身份模板的h1元素。

查看以下脚本:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="xml" encoding="UTF-8" indent="yes" />
  <xsl:strip-space elements="*"/>

  <xsl:template match="main">
    <xsl:copy>
      <h1><xsl:value-of select="h1"/></h1>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="h1"/>

  <xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
  </xsl:template>
</xsl:transform>

对于下面给出的输入:

<main>
  <h1>aaa</h1>
  <h1>bbb</h1>
  <h1>ccc</h1>
  <h2>xxx</h2>
</main>

打印:

<main>
   <h1>aaa bbb ccc</h1>
   <h2>xxx</h2>
</main>
相关问题