Str:XSLT中的标记化未提供预期的结果 [英] Str:Tokenize in XSLT not giving expected result

查看:96
本文介绍了Str:XSLT中的标记化未提供预期的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下定界格式文件

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

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>

以下是我尝试过的内容

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

我做错了什么

谢谢

推荐答案

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

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>

这篇关于Str:XSLT中的标记化未提供预期的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆