XSLT-复制元素,其具有最大属性值的复制元素比列表中的其他元素



我需要为以下输入xml编写XSL -预期输出应具有具有最大属性(key(的值的群集元素。

输入:

<ProductList>
<Product ProductId="123">
 <ClusterList>
 <Cluster Key="1" Price="100.00"/>
 <Cluster Key="3" Price="200.00"/>
 <Cluster Key="2" Price="300.00"/>
 </ClusterList>
</Product>
<Product ProductId="456">
 <ClusterList>
 <Cluster Key="11" Price="100.00"/>
 <Cluster Key="33" Price="200.00"/>
 <Cluster Key="22" Price="300.00"/>
 </ClusterList>
</Product>
<ProductList>

预期输出:

<ProductList>
<Product ProductId="123">
 <ClusterList>
 <Cluster Key="3" Price="200.00"/>
 </ClusterList>
</Product>
<Product ProductId="456">
 <ClusterList>
 <Cluster Key="33" Price="200.00"/>
 </ClusterList>
</Product>
<ProductList>

这是我写的XSL,但不起作用:(

<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="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="/ClusterList/Cluster">
      <xsl:variable name="Max">
         <xsl:value-of select="/ClusterList/Cluster[not(preceding-sibling::Cluster/@Key &gt;= @Key) and not(following-sibling::Cluster/@Key &gt; @Key)]/@Key" />
      </xsl:variable>
      <xsl:if test="@Key=$Max">
         <xsl:copy>
            <xsl:apply-templates select="@*" />
         </xsl:copy>
      </xsl:if>
   </xsl:template>
</xsl:stylesheet>

我建议您以这种方式尝试:

XSLT 1.0

<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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="ClusterList">
    <xsl:copy>
        <xsl:for-each select="Cluster">
            <xsl:sort select="@Key" data-type="number" order="descending"/>
            <xsl:if test="position()=1">
                <xsl:copy-of select="."/>
            </xsl:if>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

这是假设您使用的XSLT 1.0处理器,该处理器不支持EXSLT math:max()math:highest()扩展功能。

请注意,在领带的情况下,只会返回第一个结果。


要按照您开始的方式进行操作,您需要拥有:

<xsl:template match="Cluster">
    <xsl:if test="not(preceding-sibling::Cluster/@Key &gt; current()/@Key) and not(following-sibling::Cluster/@Key &gt; current()/@Key)">
        <xsl:copy>
            <xsl:apply-templates select="@*" />
        </xsl:copy>
    </xsl:if>
</xsl:template>

这是非常低效的,因为它必须将每个Cluster与所有兄弟姐妹重新进行比较。

最新更新