Xml 保留具有最低值的节点

Xml 保留具有最低值的节点,xml,xslt,Xml,Xslt,我正在尝试做一些简单的事情,但由于我是xslt新手,我坚持以下几点: 你能建议一下吗?如何只保留最低价格的id 我的xml: LT00 1600 LT00 350 XL50 500 XL50 800 扩展结果: <items> <item> <id>LT00</id> <price>350</price> </item> <item>

我正在尝试做一些简单的事情,但由于我是xslt新手,我坚持以下几点: 你能建议一下吗?如何只保留最低价格的id 我的xml:


LT00
1600
LT00
350
XL50
500
XL50
800
扩展结果:

<items>
    <item>
        <id>LT00</id>
        <price>350</price>
    </item>
    <item>
        <id>XL50</id>
        <price>500</price>
    </item> 
</item>

LT00
350
XL50
500
我的xslt:

`


`希望这有帮助

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                exclude-result-prefixes="msxsl"
                version="1.0">
  <xsl:key match="item" name="grp" use="id"/>
  <xsl:template match="/*">
    <root>
      <xsl:variable name="cur" select=".//item"/>
      <xsl:variable name="temp" select="item[count(.|key('grp',id)[1])=1]"/>
      <xsl:for-each select="$temp">
        <xsl:variable name="temp1" select="id"/>
        <xsl:variable name="main">
          <xsl:for-each select="$cur[id=$temp1]">
            <xsl:sort select="price" data-type="number"/>
            <xsl:copy-of select="."/>
          </xsl:for-each>
        </xsl:variable>
        <xsl:copy-of select="msxsl:node-set($main)/*[1]"/>
      </xsl:for-each>
    </root>
  </xsl:template>
</xsl:stylesheet>

以下是XSLT-1.0的工作。注意:我使用.net提供的node-set()函数将变量转换为结果片段树

我使用muenchian分组将具有相同id值的项目节点分组在一起

<xsl:variable name="temp" select="item[count(.|key('grp',id)[1])=1]"/>

然后,对于每个唯一的id,我找到了具有相同id的项目节点,按升序对它们进行排序,然后添加到变量中


然后将变量转换为结果树片段,并将第一个项节点推送到xml。

使用XSLT2.0,您可以使用

<xsl:template match="items">
    <xsl:copy>
        <xsl:for-each-group select="item" group-by="id" >
          <xsl:for-each select="current-group()">
            <xsl:sort select="number(price)"/>
            <xsl:if test="position()=1">
                <xsl:copy-of select="." />
            </xsl:if>
          </xsl:for-each>
       </xsl:for-each-group>
    </xsl:copy>   
</xsl:template>

或者作为另一种选择

<xsl:template match="items">
    <xsl:copy>
        <xsl:for-each-group select="item" group-by="id" >
          <xsl:copy>
            <xsl:copy-of select="id"/>
            <price><xsl:value-of select="min(current-group()/price)"/></price>
          </xsl:copy>
       </xsl:for-each-group>
    </xsl:copy>   
</xsl:template>

使用XSLT 1.0,我会这样做:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="by-id" match="item" use="id"/>
<xsl:template match="items">
    <xsl:copy>
        <xsl:for-each select="item[generate-id() = generate-id(key('by-id', id)[1])]">
          <xsl:for-each select="key('by-id', id)">
            <xsl:sort select="price" data-type="number"/>
            <xsl:if test="position()=1">
                <xsl:copy-of select="." />
            </xsl:if>
          </xsl:for-each>
       </xsl:for-each>
    </xsl:copy>   
</xsl:template>
</xsl:transform>


您使用的XSLT版本是什么?此外,请显示您尝试的XSLT代码,即使它不起作用。您需要在
id
上分组,并在
price
上对组进行排序,以在每组中取第一个或最低个。我使用的是1.0版,正如Martin建议的那样,请查看我的XSLT代码above@rkc,您说您使用XSLT1.0,但随后为每个组发布一个带有
的示例,即XSLT2.0。因此,请澄清您想要使用哪个版本。上面的代码非常棒,正如我预期的那样工作,非常感谢您的帮助,顺便说一句:谢谢您也给了我一个很好的教训,因为现在我可以看到这两个版本之间的差异。谢谢您Saurav,代码很棒,但是我在php内部实现这一点时遇到了一些问题:(现在排序:)
<xsl:template match="items">
    <xsl:copy>
        <xsl:for-each-group select="item" group-by="id" >
          <xsl:copy>
            <xsl:copy-of select="id"/>
            <price><xsl:value-of select="min(current-group()/price)"/></price>
          </xsl:copy>
       </xsl:for-each-group>
    </xsl:copy>   
</xsl:template>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="by-id" match="item" use="id"/>
<xsl:template match="items">
    <xsl:copy>
        <xsl:for-each select="item[generate-id() = generate-id(key('by-id', id)[1])]">
          <xsl:for-each select="key('by-id', id)">
            <xsl:sort select="price" data-type="number"/>
            <xsl:if test="position()=1">
                <xsl:copy-of select="." />
            </xsl:if>
          </xsl:for-each>
       </xsl:for-each>
    </xsl:copy>   
</xsl:template>
</xsl:transform>