2017-01-26 15 views
0

以下のコンマ区切りリストをXSLTを介して奇数と偶数の位置に基づいて以下の出力に変換したいと考えています。奇数と偶数に基づくXSLT変換

入力 - 事前に

field1,value1,field2,value2,field3,value3 

出力 -

<root> 
<field1>value1</field1> 
<field2>value2</field2> 
<field3>value3</field3> 
</root> 

感謝。

よろしく Nilay

+0

それぞれXSLTバージョンを使用していますか?使用できますか?これは 'tokenize( 'field1、value1、field2、value2、field3、value3'、 '、')[position()mod 2 = 0]'を使ってXSLT 2.0で簡単に解決できます。 –

+0

XSLT 1.0プロセッサー – Nilay

+0

XSLT 1.0プロセッサーはどれですか? http://exslt.org/str/functions/tokenize/index.htmlをサポートしていますか? –

答えて

0

あなたは、任意の整形式XMLを使用して

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:param name="input" select="'field1,value1,field2,value2,field3,value3'"/> 

    <xsl:template match="/"> 
    <root> 
     <xsl:call-template name="process"> 
     <xsl:with-param name="toProcess" select="$input"/> 
     </xsl:call-template> 
    </root> 
    </xsl:template> 

    <xsl:template name="process"> 
    <xsl:param name="toProcess"/> 
    <xsl:variable name="name" select="normalize-space(substring-before($toProcess,','))"/> 
    <xsl:variable name="value" select="normalize-space(substring-before(
     concat(substring-after($toProcess,','),',') 
     ,','))"/> 
    <xsl:variable name="remaining" select="substring-after(
     substring-after($toProcess,',') 
     ,',')"/> 
    <xsl:element name="{$name}"> 
     <xsl:value-of select="$value"/> 
    </xsl:element> 
    <xsl:if test="string($remaining)"> 
     <xsl:call-template name="process"> 
     <xsl:with-param name="toProcess" select="$remaining"/> 
     </xsl:call-template> 
    </xsl:if> 
    </xsl:template> 

</xsl:stylesheet> 

出力(...拡張機能を使用せずに、再帰的なテンプレートの呼び出しを使用することができます入力)

<root> 
    <field1>value1</field1> 
    <field2>value2</field2> 
    <field3>value3</field3> 
</root> 
+0

ありがとうございます。 – Nilay

関連する問題