我有一组产品 ID,例如 (123565,589655,45585,666669,5888) 我想在这些 ID 的前后放置逗号,例如 (,123565,589655,45585,666669,5888,)..
我怎样才能为此编写 XSLT 代码?
只需使用:
<xsl:text>,</xsl:text><xsl:value-of select="$yourSequence"
separator=","/><xsl:text>,</xsl:text>
很大程度上取决于您的输入 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 的样子,我可以相应地修改我的答案。