繁体   English   中英

XSLT:合并具有相同属性值的兄弟姐妹的值

[英]XSLT: Merge values of siblings with same attribute value

给定像这样的输入:

<root>
    <childA style="style_1">Some</childA>
    <childA style="style_1">Text</childA>
    <childA style="style_1">Here</childA>
    <childB style="style_2"/>
    <childA style="style_2">Fake</childA>
    <childB style="style_2"/>
    <childA style="style_1">Some</childA>
    <childA style="style_1">Other</childA>
    <childB style="style_2"/>
    <childA style="style_1">Text</childA>
    <parent>            
        <childA style="style_1">More</childA>
        <childA style="style_1">Text</childA>
    </parent>
</root>

如何合并彼此跟随的元素? 因此,所需的输出为:

<root>
    <childA style="style_1">SomeTextHere</childA>
    <childB style="style_2"/>
    <childA style="style_2">Fake</childA>
    <childB style="style_2"/>
    <childA style="style_1">SomeOther</childA>
    <childB style="style_2"/>
    <childA style="style_1">Text</childA>
    <parent>            
        <childA style="style_1">MoreText</childA>
    </parent>
</root>

我尝试不同的技巧与for-each-groupfor-each它内部的,但得到了重复的节点,并且还在于<childB>两者之间<childA>当我使用的节点被忽略group-adjacent="@style"和我一个节点而不是两个节点中的SomeOtherText

这是我的尝试:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output indent="yes" />
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="childA">
        <xsl:copy-of select="." />
    </xsl:template>

    <xsl:template match="childB">
        <xsl:copy-of select="." />
    </xsl:template>

    <xsl:template match="parent">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*[childA]">
        <xsl:copy>
            <xsl:for-each-group select="childA" group-adjacent="@style">
                <xsl:element name="childA">
                    <xsl:attribute name="style">
                        <xsl:value-of select="current-grouping-key()"/>
                    </xsl:attribute>
                    <xsl:for-each select="current-group()">
                        <xsl:value-of select="."/>
                    </xsl:for-each>
                </xsl:element>
            </xsl:for-each-group>
        </xsl:copy>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="text()"/>

</xsl:stylesheet>

怎么样:

XSLT 2.0

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

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

<xsl:template match="*[childA]">
    <xsl:copy>
        <xsl:for-each-group select="*" group-adjacent="concat(name(), '|', @style)">
            <xsl:choose>
                <xsl:when test="self::childA">
                    <childA style="{@style}">
                        <xsl:value-of select="current-group()" separator=""/>
                    </childA>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="current-group()"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

你可以这样做(如澄清评论),而不依赖于XSLT 2.0或3.0的特性,虽然也许在性能损失最好的一个版本。 您只需要知道如何编写一个XPath表达式即可将每个组中的第一个<childA>与其余组区分开, 反之亦然 例如:

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

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

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

  <xsl:template match="childA">
    <!-- this is the first childA in a group -->
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="." mode = "merge"/>
    </xsl:copy>
  </xsl:template>

  <!-- Matches (only) the second and subsequent members of a childA group: -->
  <xsl:template match="childA[name(preceding-sibling::*[1]) = 'childA' and @style = preceding-sibling::*[1]/@style]"/>

  <!-- merge in this childA's content and that of the remaining group elements -->
  <xsl:template match="childA" mode="merge">
    <xsl:apply-templates select="node()"/> <!-- attributes ignored -->
    <!-- merge the next sibling, too, if it's in the same group: -->
    <xsl:apply-templates select="following-sibling::*[1][name() = 'childA' and @style = preceding-sibling::*[1]/@style]" mode = "merge"/>
  </xsl:template>

</xsl:stylesheet>

我认为使用XSLT 3.0(如Saxon 9.8或实际的Altova支持的):

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

    <xsl:output indent="yes"/>

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

    <xsl:template match="*">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:for-each-group select="*"
                group-adjacent="node-name() eq xs:QName('childA'), @style" composite="yes">
                <xsl:choose>
                    <xsl:when test="current-grouping-key()[1]">
                        <xsl:copy>
                            <xsl:apply-templates select="@*, current-group()/node()"/>
                        </xsl:copy>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="current-group()"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM