我需要使用XSLT将一些xml转换为另一个。
输入看起来像这样:
<RootNode>
<Products>
<Price>10.02</Price>
<TaxRate>1.23</TaxRate>
<CurrencyId>USD</CurrencyId>
</Products>
</RootNode>
和输出:
<RootNode>
<Products>
<Product>
<Prices>
<Price>10.02</Price>
<TaxRate type="0">16</TaxRate>
<Currency>USD</Currency>
</Prices>
</Product>
</Products>
</RootNode>
目前,我正在尝试类似的操作,但这不起作用:
<xsl:template match="/">
<RootElement>
<xsl:for-each select="Supplier-Catalog/Products">
<xsl:attribute-set name="Price" use-attribute-sets="Prices">
<xsl:value-of select="Price" />
</xsl:attribute-set>
</xsl:for-each>
</RootElement>
</xsl:template>
</xsl:stylesheet>
TaxRate type =“ 0”-取决于输入中的TaxRate值。
有人可以帮我吗?
编辑
好的,我创建了这样的内容:
<xsl:template match="/">
<RootElement>
<xsl:for-each select="Supplier-Catalog/Products">
<Prices>
<Price>
<xsl:value-of select="Price" />
</Price>
<TaxRate>
<xsl:value-of select="TaxRate" />
</TaxRate>
<Currency>
<xsl:value-of select="CurrencyId" />
</Currency>
</Prices>
</xsl:for-each>
</RootElement>
</xsl:template>
可以,但是仍然不知道如何在TaxRate属性中添加“ type = 0”
编辑2
还有一个问题...
对于此结构
Products>
<Stock>
<Quantity>5</Quantity>
</Stock>
<Stock>
<Quantity>50</Quantity>
</Stock>
</Products><Products>
<Stock>
<Quantity>1</Quantity>
</Stock>
<Stock>
<Quantity>2</Quantity>
</Stock>
</Products>
和此XSLT
<xsl:for-each select="Products">
<Product>
<Stock>
<Quantity>
<xsl:value-of select="sum(//Products/Stock/Quantity)" />
</Quantity>
</Stock>
</Product>
</xsl:for-each>
</Products>
我明白了:
<Products>
<Product>
<Stock>
<Quantity>58</Quantity>
</Stock>
</Product>
<Product>
<Stock>
<Quantity>58</Quantity>
</Stock>
</Product>
这是错误的,因为它将所有值求和,而不是:
<Products>
<Product>
<Stock>
<Quantity>55</Quantity>
</Stock>
</Product>
<Product>
<Stock>
<Quantity>3</Quantity>
</Stock>
</Product>
我在哪里弄错了?
答案 0 :(得分:0)
在创建属性和分配值时,您需要选择选择/何时条件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<RootElement>
<xsl:for-each select="RootNode/Products">
<Prices>
<Price>
<xsl:value-of select="Price" />
</Price>
<TaxRate>
<xsl:choose>
<xsl:when test="TaxRate=1.23">
<xsl:attribute name="type"><xsl:value-of select="0"/></xsl:attribute>
<xsl:value-of select="'16'"/>
</xsl:when>
<xsl:when test="TaxRate=0.7">
<xsl:attribute name="type"><xsl:value-of select="1"/></xsl:attribute>
<xsl:value-of select="'3'"/>
</xsl:when>
<xsl:when test="TaxRate=0">
<xsl:attribute name="type"><xsl:value-of select="0"/></xsl:attribute>
<xsl:value-of select="'0'"/>
</xsl:when>
</xsl:choose>
</TaxRate>
<Currency>
<xsl:value-of select="CurrencyId" />
</Currency>
</Prices>
</xsl:for-each>
</RootElement>
</xsl:template>
</xsl:stylesheet>