【问题标题】:Merge specific elements but keep document order using XSLT 1.0使用 XSLT 1.0 合并特定元素但保持文档顺序
【发布时间】:2016-07-11 10:54:37
【问题描述】:

给定以下文件:

<doc>
  <a>a</a>
  <b>1</b>
  <b>2</b>
  <b>3</b>
  <c>c</c>
</doc>

我想把它变成:

<doc>
  <a>a</a>
  <b>1,2,3</b>
  <c>c</c>
</doc>

到目前为止,我所拥有的是(主要取自 SO 的另一篇文章):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

  <xsl:template match="doc">
    <xsl:copy>
      <xsl:apply-templates select="*[not(self::b)]"/>
      <b><xsl:apply-templates select="b/text()"/></b>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="b/text()">
    <xsl:if test="position() &gt; 1">,</xsl:if>
    <xsl:value-of select="."/>
  </xsl:template>

</xsl:stylesheet>

这会产生以下结果:

<doc><a>a</a><c>c</c><b>1,2,3</b></doc>

但我正在努力寻找一种能够保持文档顺序完整的解决方案。 &lt;b&gt; 元素的任何运行都应替换为包含原始元素文本的单个元素,作为逗号分隔的列表。其他元素不应重新排序。

【问题讨论】:

  • "任何 元素的运行" 会不会不止一个?
  • 不,只有一次这样的运行。

标签: xml xslt xslt-1.0


【解决方案1】:

你为什么不简单地做:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/doc">
    <xsl:copy>
        <xsl:copy-of select="a"/>
        <b>
            <xsl:for-each select="b">
                <xsl:value-of select="."/>
                <xsl:if test="position() !=last()">,</xsl:if>
            </xsl:for-each>
        </b>
        <xsl:copy-of select="c"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

这是假设您知道传入 XML 的结构并且可以分别枚举b 之前和之后的元素。否则,您必须执行以下操作:

<xsl:template match="/doc">
    <xsl:copy>
        <xsl:apply-templates select="*[not(self::b or preceding-sibling::b)]"/>
        <b>
            <xsl:for-each select="b">
                <xsl:value-of select="."/>
                <xsl:if test="position() !=last()">,</xsl:if>
            </xsl:for-each>
        </b>
        <xsl:apply-templates select="*[not(self::b or following-sibling::b)]"/>
    </xsl:copy>
</xsl:template>

【讨论】:

  • 传入的 XML 的结构可能会有所不同,所有元素都是可选的。但是您的第二个解决方案与 Martin 给出的解决方案同样有效。而且它不使用递归,所以我想它会更好地扩展到真正大量的&lt;b&gt;
  • "所有元素都是可选的。" 咳咳:以上假设b 是强制性的。但是您可以通过将模板限制为doc[b] 并让身份转换处理异常来轻松解决这个问题..
  • 我想我会把你的解决方案和马丁的解决方案结合起来。 &lt;xsl:template match="b[not(preceding-sibling::*[1][self::b])]"&gt; &lt;b&gt; &lt;xsl:for-each select="../b"&gt;...&lt;/b&gt; ... 之类的东西和用于删除其余 &lt;b&gt; 元素的空模板。
  • 这也可以。我自己的偏好是尽可能在apply-templates 阶段缩小范围。
  • @Markus,如果你知道b元素只有一个序列,你可以将匹配简化为&lt;xsl:template match="b[1]"&gt; &lt;b&gt; &lt;xsl:for-each select="../b"&gt;...&lt;/b&gt; ...&lt;/xsl:template&gt;&lt;xsl:template match="b[position() &gt; 1]"/&gt;
【解决方案2】:

您可以在不同的模式下使用兄弟递归:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="b[not(preceding-sibling::*[1][self::b])]">
        <xsl:copy>
            <xsl:apply-templates select="." mode="merge"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="b[preceding-sibling::*[1][self::b]]"/>

    <xsl:template match="b" mode="merge">
        <xsl:value-of select="."/>
        <xsl:variable name="next" select="following-sibling::*[1][self::b]"/>
        <xsl:if test="$next">
            <xsl:text>,</xsl:text>
            <xsl:apply-templates select="$next" mode="merge"/>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

【讨论】:

  • 工作就像一种享受。再次感谢您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-23
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-14
相关资源
最近更新 更多