Str:Tokenize in XSLT not giving expected result

你。 提交于 2020-01-06 17:57:25

问题


I have a following delimited format file

<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>

I want the following file into this format 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 is what I have tried

<?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>

but I am not getting any values in output only closed tags

What have I done incorrect

Please

Thanks


回答1:


First thing, you are not using the str:tokenize() function properly. Try just this template:

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

As you can see, the function does all the required work on its own: there's no need to call a processing template, and the result is already a node-set. All that's left for you to do is pick the cherries from the tree, say:

<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>


来源:https://stackoverflow.com/questions/21850648/strtokenize-in-xslt-not-giving-expected-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!