2013-02-25 243 views
1

我有一套產品喜歡(123565,589655,45585,666669,5888)我想在這些id的前後加上逗號喜歡(,123565,589655,45585,666669,5888, )..逗號分隔

我該如何編寫用於執行此操作的XSLT代碼?

+0

你能告訴你的XML的樣本,和您預計的輸出呢?謝謝! – 2013-02-25 13:33:58

回答

2

只需使用

<xsl:text>,</xsl:text><xsl:value-of select="$yourSequence" 
            separator=","/><xsl:text>,</xsl:text> 
+1

這很酷,我甚至不知道'@ separator'屬性;生活和學習...... +1 – 2013-02-25 19:29:15

+0

@EeroHelenius,不客氣。 – 2013-02-25 21:19:09

+1

@Dimitre Nice.Thanks ... – Binoop 2013-03-13 12:39:07

0

很大程度上取決於您的輸入XML文件以及您希望輸出的樣子。無論如何,由於您使用的是XSLT 2.0,因此您可以使用string-join()函數。

比方說,你有一個看起來像這樣的輸入XML文件:

<products> 
    <product> 
    <name>Product #1</name> 
    <id>123565</id> 
    </product> 
    <product> 
    <name>Product #1</name> 
    <id>589655</id> 
    </product> 
    <product> 
    <name>Product #1</name> 
    <id>45585</id> 
    </product> 
</products> 

你可以有這樣一個樣式表:

<?xml version="1.0" encoding="UTF-8"?> 

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 

    <xsl:output method="text" indent="yes"/> 
    <xsl:variable name="SEPARATOR" select="','"/> 

    <xsl:template match="/"> 
    <!-- 
    Join the values of each products/product/id element with $SEPARATOR; prepend 
    and append the resulting string with commas. 
    --> 
    <xsl:value-of 
     select="concat($SEPARATOR, string-join((products/product/id), 
     $SEPARATOR), $SEPARATOR)"/> 
    </xsl:template> 

</xsl:stylesheet> 

這將產生以下的輸出:

,123565,589655,45585, 

如果您編輯您的問題以包含您的輸入XML以及您希望輸出XML的內容看起來像,我可以相應地修改我的答案。