(123565,589655,45585,666669,5888)のような製品IDのセットがあります。これらのIDのセットの前後にコンマを置きたいです。Like(、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の外観を含めるように質問を編集する場合は、それに応じて回答を変更できます。