保留具有最低值的节点
Keep node with lowest value
我正在尝试处理简单的事情,但是由于我是 xslt 的新手,所以我坚持使用以下内容:
你能建议一下吗?如何只保留最低价的id
我的 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>
`
希望这对您有所帮助,
<?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。
通过 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>
我正在尝试处理简单的事情,但是由于我是 xslt 的新手,所以我坚持使用以下内容: 你能建议一下吗?如何只保留最低价的id 我的 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>
`
希望这对您有所帮助,
<?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。
通过 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>