Str:XSLT中的Tokenize没有给出预期的结果

时间:2014-02-18 10:19:33

标签: xml xslt xslt-1.0 delimiter exslt

我有以下分隔格式文件

<Sample>PRPS~262772109~K0~LRGLINE=1111112345|40|0|0|1|0|0|0|0|0||X,XXX,621|01/17/2002|01/17/2034|</Sample>

我希望将以下文件格式化为XML

<ResponseType>
 <XXXXX>262772109</XXXXX>
<zzzzz>0</zzzzz>
<RGLINE>
<Number>1111112345</Number>
<ID23>40</ID23>
<CCode>0</CCode>
<TC>0</TC>
<AC>1</AC>
<RC>0</RC>
<CHECKCODE>0</CHECKCODE>
<CODE1>0</CODE1>
<VOIDCODE>0</VOIDCODE>
<SUBACTIONCODE/>
<SEQUENCE>X,XXX,621</SEQUENCE>
<EFFECTIVEDATE>01/17/2002</EFFECTIVEDATE>
<POSTDATE>01/17/2034</POSTDATE>
</RGLINE>
</ResponseType>

FOllowing是我尝试过的

<?xml version="1.1" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxml="urn:schemas-microsoft-com:xslt" version="1.0" xmlns:str="http://exslt.org/strings" exclude-result-prefixes="msxml">
  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/">
    <xsl:variable name="tokenized">
      <items>
        <xsl:call-template name="tokenize">
          <xsl:with-param name="string" select="//text()" />
          <xsl:with-param name="delimiters" select="string" />
        </xsl:call-template>
      </items>
    </xsl:variable>
    <ResponseType>

<XXXXX>
<xsl:copy-of select="msxml:node-set($tokenized)/items/item[1]/text()" />
</XXXXX>
<zzzzz>
<xsl:copy-of select="msxml:node-set($tokenized)/items/item[2]/text()" />
</StatusCode>
<RGLINE>

      <Number>
        <xsl:copy-of select="msxml:node-set($tokenized)/items/item[3]/text()" />
      </Number>
      <Id23>
        <xsl:copy-of select="msxml:node-set($tokenized)/items/item[4]/text()" />
      </Id23>
      <CCode>
        <xsl:copy-of select="msxml:node-set($tokenized)/items/item[5]/text()" />
      </CCode>
      <TC>
        <xsl:copy-of select="msxml:node-set($tokenized)/items/item[6]/text()" />
      </TC>
 <AC>
        <xsl:copy-of select="msxml:node-set($tokenized)/items/item[7]/text()" />
      </AC>
 <RC>
        <xsl:copy-of select="msxml:node-set($tokenized)/items/item[8]/text()" />
      </RC>
<CHECKCODE>
        <xsl:copy-of select="msxml:node-set($tokenized)/items/item[9]/text()" />
      </CHECKCODE>
<CODE1>
        <xsl:copy-of select="msxml:node-set($tokenized)/items/item[10]/text()" />
      </CODE1>
<VOIDCODE>
        <xsl:copy-of select="msxml:node-set($tokenized)/items/item[11]/text()" />
      </VOIDCODE>
<SUBACTIONCODE>
        <xsl:copy-of select="msxml:node-set($tokenized)/items/item[12]/text()" />
      </SUBACTIONCODE>
<SEQUENCE>
        <xsl:copy-of select="msxml:node-set($tokenized)/items/item[13]/text()" />
      </SEQUENCE>
<EFFECTIVEDATE>
        <xsl:copy-of select="msxml:node-set($tokenized)/items/item[14]/text()" />
      </EFFECTIVEDATE>
<POSTDATE>
        <xsl:copy-of select="msxml:node-set($tokenized)/items/item[15]/text()" />
      </POSTDATE>
    </ResponseType>
</RGLINE>
  </xsl:template>

<xsl:template name="tokenize">
<xsl:param name="string" />
<xsl:param name="delimiters" />
<xsl:variable name="item">
<xsl:value-of select="str:tokenize('string', '~KJ=|')"/>
</xsl:variable>
<item>
        <xsl:value-of select="$item" />
      </item>
</xsl:template>
</xsl:stylesheet>

但是我没有在仅输出闭合标签中获得任何值

我做错了什么

由于

1 个答案:

答案 0 :(得分:0)

首先,您没有正确使用str:tokenize()函数。试试这个模板:

<xsl:template match="/">
    <xsl:variable name="tokens" select="str:tokenize(Sample, '~KJ=|')" />
    <output>  
        <xsl:copy-of select="$tokens"/>
    </output>
</xsl:template>

如您所见,该函数自行完成所有必需的工作:无需调用处理模板,结果已经是节点集。剩下的就是从树上采摘樱桃,说:

<xsl:template match="/">
    <xsl:variable name="tokens" select="str:tokenize(Sample, '~KJ=|')" />
    <ResponseType>  
        <XXXXX><xsl:value-of select="$tokens[2]" /></XXXXX>
        <zzzzz><xsl:value-of select="$tokens[3]" /></zzzzz>
        <!-- etc. -->
    </ResponseType>
</xsl:template>