2012-03-26 11 views
1

を分離:XSLTスプリットコンマは、私はこのようなXML要素内のいくつかのデータを持っているファイル

<?xml version="1.0" encoding="UTF-8"?> 
<payload> 
    <set> 
     <month>JAN,FEB,MAR</month> 
     <season>Season1</season> 
     <productId>ABCD</productId> 
    </set> 
</payload> 

私は好きで、全く新しいセットタグにカンマ区切り文字列を分割することであるに興味を持って事:

<payload> 
    <set> 
     <month>JAN</month> 
     <season>Season1</season> 
     <productId>ABCD</productId> 
    </set> 
</payload> 
<payload> 
    <set> 
     <month>FEB</month> 
     <season>Season1</season> 
     <productId>ABCD</productId> 
    </set> 
</payload> 
<payload> 
    <set> 
     <month>MAR</month> 
     <season>Season1</season> 
     <productId>ABCD</productId> 
    </set> 
</payload> 

これをXSLTでどのように行うことができますか?あなたはここで、文字列を分割するという名前のテンプレートに再帰呼び出しを使用する必要がXSLT 1.0を使用して

+0

私はバージョン1 –

答えて

0

が可能なソリューションです:

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

    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="@* | node()"> 
    <xsl:param name="month"/> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"> 
     <xsl:with-param name="month" select="$month"/> 
     </xsl:apply-templates> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="month"> 
    <xsl:param name="month"/> 
    <month> 
     <xsl:choose> 
     <xsl:when test="$month"> 
      <xsl:value-of select="$month"/> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:apply-templates/> 
     </xsl:otherwise> 
     </xsl:choose> 
    </month> 
    </xsl:template> 

    <xsl:template name="splitMonths"> 
    <xsl:param name="months"/> 
    <xsl:variable name="firstMonth" select="substring-before($months,',')"/> 
    <xsl:variable name="month"> 
     <xsl:choose> 
     <xsl:when test="$firstMonth"> 
      <xsl:value-of select="$firstMonth"/> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$months"/> 
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:variable> 
    <xsl:variable name="otherMonths" select="substring-after($months,',')"/> 
    <xsl:if test="$month"> 
     <payload> 
     <xsl:apply-templates> 
      <xsl:with-param name="month" select="$month"/> 
     </xsl:apply-templates> 
     </payload> 
    </xsl:if> 
    <xsl:if test="$otherMonths"> 
     <xsl:call-template name="splitMonths"> 
     <xsl:with-param name="months" select="$otherMonths"/> 
     </xsl:call-template> 
    </xsl:if> 
    </xsl:template> 

    <xsl:template match="payload"> 
    <xsl:call-template name="splitMonths"> 
     <xsl:with-param name="months" select="set/month"/> 
    </xsl:call-template> 
    </xsl:template> 

</xsl:stylesheet> 
+0

を使用しています私は1つの微調整をした、ありがとうございましたマルチセット 'セット'があるかもしれないので。ここに私の変更...... ......

関連する問題