2014-03-30 162 views
0

我正在使用XSLT版本1.0,並且我有一個要求來分隔由管道分隔的文本字符串並使用xml元素中的每個值。例如,如果字符串爲<Code>111|222|333|444</Code>那麼我想輸出<Postalcode>111</Postalcode>用管道分隔符分隔文本並使用每個值

<Postalcode>222</Postalcode> 
<Postalcode>333</Postalcode> 
<Postalcode>444</Postalcode> 

我要做到這一點使用XSLT。感謝有人能幫助我。

感謝, 拉維

回答

1

繼樣式表有一個名爲令牌化模板,標記化基礎上通過分離器和每一個令牌字符串創建郵編元素:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output method="xml" indent="yes"/> 
<xsl:strip-space elements="*"/> 
<xsl:template name="tokenize"> 
    <xsl:param name="text"/> 
    <xsl:param name="separator" /> 
    <xsl:choose> 
     <xsl:when test="not(contains($text, $separator))"> 
      <Postalcode> 
       <xsl:value-of select="$text"/> 
      </Postalcode> 
     </xsl:when> 
     <xsl:otherwise> 
      <Postalcode> 
       <xsl:value-of select="normalize-space(substring-before($text, $separator))"/> 
      </Postalcode> 
      <xsl:call-template name="tokenize"> 
       <xsl:with-param name="text" select="substring-after($text, $separator)"/> 
       <xsl:with-param name="separator" select="$separator"/> 
      </xsl:call-template> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
<xsl:template match="/Code"> 
    <xsl:copy> 
     <xsl:variable name="tokenize"> 
      <xsl:call-template name="tokenize"> 
       <xsl:with-param name="text" select="."/> 
       <xsl:with-param name="separator" select="'|'"/> 
      </xsl:call-template> 
     </xsl:variable> 
     <xsl:copy-of select="$tokenize"/> 
    </xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 
+0

Lingamurthy嗨,你的代碼工程奇蹟: )。它給出了我正在尋找的確切輸出。萬分感謝!!! – user3478371

相關問題