保持节点具有最低值

时间:2015-01-13 10:05:27

标签: xml xslt

我正在尝试处理简单的事情,但是因为我是xslt的新手,所以我坚持下面: 你能告诉我吗?如何保持只有最低价格的身份证 我的xml:

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

展望结果:

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

我的xslt:

`

<xsl:template match="/">
    <items>
        <xsl:for-each-group select="items/item/id" group-by="id" order="ascending" data-type="number">
            <xsl:copy-of select="current-group( )"/>
        </xsl:for-each-group>

        <xsl:for-each select="current-group( )">
            <xsl:if test="position()=1">
                <xsl:copy-of select="current()" />
            </xsl:if>
        </xsl:for-each>
    </items>   

</xsl:template>

`

2 个答案:

答案 0 :(得分:1)

使用XSLT 2.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>

答案 1 :(得分:0)

希望这有帮助,

<?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。 Do -note我使用通过.net提供的node-set()函数将变量转换为结果片段树

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

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

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

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