XSLT:y/n,如果价格差异大于;1.
我正在创建一个新的productfeed,需要以下字段:XSLT:y/n,如果价格差异大于;1.,xslt,Xslt,我正在创建一个新的productfeed,需要以下字段: 如果价格和旧价格之间的差异在字段: 如果价格和旧价格之间的差异为1或小于1:n(从“否”字段开始): 文件:Data.xml <?xml version="1.0"?> <products> <product id="0001"> <price>120.00</price> <old_price>125.00</old_price> </
如果价格和旧价格之间的差异在字段:
如果价格和旧价格之间的差异为1或小于1:n(从“否”字段开始):
文件:Data.xml
<?xml version="1.0"?>
<products>
<product id="0001">
<price>120.00</price>
<old_price>125.00</old_price>
</product>
<product id="0002">
<price>5.00</price>
<old_price>5.50</old_price>
</product>
</products>
120
125
5
5.50
希望的产出:
<?xml version="1.0"?>
<products>
<product id="0001">
<diff>y</diff>
</product>
<product id="0002">
<diff>n</diff>
</product>
</products>
Y
N
我还没有测试过,但应该是这样的:
<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="/">
<products>
<xsl:for-each select="//product">
<diff>
<xsl:choose>
<xsl:when test="price - old_price > 1">
y
</xsl:when>
<xsl:otherwise>
n
</xsl:otherwise>
</xsl:choose>
</diff>
<xsl:copy-of select="*" />
</xsl:for-each>
</products>
</xsl:template>
</xsl:stylesheet>
Y
N
将测试并放置更新让这一点有一个回旋:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="price[translate(. - ../old_price, '-', '') > 1]">
<diff>y</diff>
</xsl:template>
<xsl:template match="price">
<diff>n</diff>
</xsl:template>
<xsl:template match="old_price" />
</xsl:stylesheet>
Y
N
在样例输入上运行时,将生成:
<products>
<product id="0001">
<diff>y</diff>
</product>
<product id="0002">
<diff>n</diff>
</product>
</products>
Y
N