只是我想做簡單的連接。當我迭代值時,我只想在一個字串中獲取所有產品名稱并列印該值..這是我的 XML:
<CartItems>
<CartItem>
<ProductPrice>605.0000</ProductPrice>
<ProductWeight>2.2975</ProductWeight>
<ProductID>722</ProductID>
<VariantID>235</VariantID>
<Quantity>1</Quantity>
<ProductName>ProductName1</ProductName>
</CartItem>
<CartItem>
<ProductPrice>349.9900</ProductPrice>
<ProductWeight>6.1731</ProductWeight>
<ProductID>070</ProductID>
<VariantID>296</VariantID>
<Quantity>1</Quantity>
<ProductName>ProductName2</ProductName>
</CartItem>
</CartItems>
這是我的代碼:
<xsl:template name="CartItemProductNames">
<xsl:param name="Data" />
<xsl:variable name="CombineProductName">
</xsl:variable>
<xsl:for-each select="$Data">
<xsl:if test="position()=1">
-- set value in CombineProductName
</xsl:if>
{
'ProductName':'<xsl:value-of select="ProductName" disable-output-escaping="yes" />',
<xsl:variable name="ProductName" select="ProductName">
<xsl:value-of select="$Total_Amount"/>
</xsl:variable>,
'Combined':'<xsl:value-of select="concat($CombineProductName,', ', $ProductName)" />'
}
</xsl:for-each>
<xsl:value-of select="$CombineProductName" disable-output-escaping="yes" />
</xsl:template>
這個 ackage 被稱為:
'$PRODUCTNAMES' ='<xsl:call-template name="CartItemProductNames">
<xsl:with-param name="Data" select="/root/CartItems/CartItem"/>
</xsl:call-template>',
期望的輸出:
'$PRODUCTNAMES':'{ProductName1, ProductName2, and more if there will be in list}'
我是 xslt 的新手。請幫我解決這個問題。
uj5u.com熱心網友回復:
您顯示的結果可以很容易地使用:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/CartItems">
<xsl:text>'$PRODUCTNAMES':'{</xsl:text>
<xsl:for-each select="CartItem">
<xsl:value-of select="ProductName"/>
<xsl:if test="position()!=last()">, </xsl:if>
</xsl:for-each>
<xsl:text>}'</xsl:text>
</xsl:template>
</xsl:stylesheet>
在 XSLT 2.0 或更高版本中更容易。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/365387.html
下一篇:使用xsl:key查找重復項?
