我有一个要求,我需要省略重复记录。考虑下面的示例
以下是输入
<ExchangeRates>
<Rate> <!--The exchange rates -->
<SourceCurrency>INR</SourceCurrency>
<TargetCurrency>USD</TargetCurrency>
<ConversionFactor>60.2</ConversionFactor>
</Rate>
<Rate>
<SourceCurrency>INR</SourceCurrency>
<TargetCurrency>USD</TargetCurrency>
<ConversionFactor>58.2</ConversionFactor>
</Rate>
<Rate>
<SourceCurrency>YEN</SourceCurrency>
<TargetCurrency>INR</TargetCurrency>
<ConversionFactor>.52</ConversionFactor>
</Rate>
<Rate>
<SourceCurrency>SAR</SourceCurrency>
<TargetCurrency>INR</TargetCurrency>
<ConversionFactor>16.50</ConversionFactor>
</Rate>
<Rate>
<SourceCurrency>SAR</SourceCurrency>
<TargetCurrency>INR</TargetCurrency>
<ConversionFactor>16.50</ConversionFactor>
</Rate>
</ExchangeRates>
现在我想做的是删除所有重复项。如果有一对Source和TargetCurrency,我想先发送第一条记录并删除其他记录。
输出shud就像
<ExchangeRates>
<Rate>
<SourceCurrency>INR</SourceCurrency>
<TargetCurrency>USD</TargetCurrency>
<ConversionFactor>60.2</ConversionFactor>
</Rate>
<Rate>
<SourceCurrency>YEN</SourceCurrency>
<TargetCurrency>INR</TargetCurrency>
<ConversionFactor>.52</ConversionFactor>
</Rate>
<Rate>
<SourceCurrency>SAR</SourceCurrency>
<TargetCurrency>INR</TargetCurrency>
<ConversionFactor>16.50</ConversionFactor>
</Rate>
</ExchangeRates>
我发现在XSLT之后删除了重复项。即。如果内部元素完全相同。 以下代码可以帮助我轻松避免重复。它根据外部标记内的内容识别重复项。
但我想xslt删除记录,如果汇率来自同一对货币而是多个ConversionFactors。
<?xml version="1.0" encoding="UTF-8"?>
<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:if test="not(node()) or not(preceding-sibling::node()[.=string(current())])">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
如果多次出现,我需要降低USD-&gt; INR的费率。并且只转发第一个汇率。
我可以帮助实现这一目标。
干杯
Reji
答案 0 :(得分:1)
您需要根据Rate
和SourceCurrency
对TargetCurrency
进行分组。一种有效的方法是使用Muenchian使用密钥的分组:
<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:key name="currencyPair" match="Rate" use="concat(SourceCurrency,' ',TargetCurrency)"/>
<xsl:template match="ExchangeRates">
<xsl:copy>
<xsl:for-each select="Rate[count(. | key('currencyPair',concat(SourceCurrency,' ',TargetCurrency))[1]) = 1]">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>