2013-02-25 210 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的内容看起来像,我可以相应地修改我的答案。