XSL - 对每个行标记分裂字符,发出未保留的空白行

时间:2013-12-17 15:45:52

标签: xml string xslt line

我有以下XML转换代码来标记其中一个XML标记中的每一行,并在每行达到64个字符时拆分..

XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fic="http://www.ba.com/schema/mes/FICOAccessV01">
<xsl:template match="/">
    <xsl:variable name="MessageContent" select="substring(/fic:FICOAccess/FICOAccessMessage/MessageContent,3)"/>
    <fic:FICOAccess>
        <FICOAccessMessage>
            <EncodingCharacterSetName>
                <xsl:value-of select="/fic:FICOAccess/FICOAccessMessage/EncodingCharacterSetName"/>
            </EncodingCharacterSetName>
            <MessageContent>
                <xsl:call-template name="splitBinaryData">
                    <xsl:with-param name="txt">
                        <xsl:value-of select="$MessageContent"/>
                    </xsl:with-param>
                </xsl:call-template>
            </MessageContent>
        </FICOAccessMessage>
    </fic:FICOAccess>
</xsl:template>
<xsl:template name="splitBinaryData">
    <xsl:param name="txt">
    </xsl:param>
    <xsl:param name="accum"/>
    <xsl:for-each select="tokenize(concat($accum,$txt), '\n\r?')[.]">
        <xsl:variable name="temptxt">
            <xsl:value-of select="."/>
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="string-length($temptxt) &gt; 64 ">
                <xsl:call-template name="word-wrap">
                    <xsl:with-param name="str" select="$temptxt"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat($temptxt, '&#10;')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each>
</xsl:template>
<xsl:template name="word-wrap">
    <xsl:param name="str"/>
    <xsl:param name="line-length" select="65"/>
    <xsl:param name="line-ending" select="'&#xA;'"/>
    <xsl:choose>
        <xsl:when test="string-length($str)&gt;=$line-length">
            <xsl:value-of select="substring($str,0,$line-length)"/>
            <xsl:value-of select="$line-ending"/>
            <xsl:call-template name="word-wrap">
                <xsl:with-param name="str" select="substring($str,$line-length)"/>
                <xsl:with-param name="line-length" select="$line-length"/>
                <xsl:with-param name="line-ending" select="$line-ending"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="concat($str, '&#10;')"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

这是我传入的输入XML,

INPUT

<?xml version="1.0" encoding="UTF-8"?>
<fic:FICOAccess xmlns:fic="http://www.ba.com/schema/mes/FICOAccessV01">
<FICOAccessMessage>
    <EncodingCharacterSetName>ASCII</EncodingCharacterSetName>
    <MessageContent>NLKDAJLSJADKLJASASJSADKJSADSADLKAS;KDA;LKSAL;;    DS DASS DSAADSA DSAD
 ABCJDKSJKKSALKSJLALA
                                       DLASJDKADJKSAJKASDJKA
KSAHDJKSAHDJKSAHDJSHAJDHKSADHSAJDSAJKDSAHDJSAHDKJSAHDJKSAHDJ
DSA                S         DSS                  D  D  D  D







ASDDSA          FD   FDS     FDFSS          FSS  FD

BEV PX   N24    FF           CC/YY  101     WW         MM
    </MessageContent>
</FICOAccessMessage>
</fic:FICOAccess>

但问题是,在请求中我之间有空行而且在标记化时我无法保留这些行,下面是我在转换后得到的响应

实际

<fic:FICOAccess xmlns:fic="http://www.ba.com/schema/mes/FICOAccessV01">
<FICOAccessMessage>
      <EncodingCharacterSetName>ASCII</EncodingCharacterSetName>
      <MessageContent>NLKDAJLSJADKLJASASJSADKJSADSADLKAS;KDA;LKSAL;;    DS DASS DSDDSS
     ABCJDKSJKKSALKSJLALA
                                       DLASJDKADJKSAJKASDJKA
KSAHDJKSAHDJKSAHDJSHAJDHKSADHSAJDSAJKDSAHDJSAHDKJSAHDJKSAHDJ
DSA                S         DSS                  D  D  D  D
ASDDSA          FD   FDS     FDFSS          FSS  FD
BEV PX   N24    FF           CC/YY  101     WW         MM
        </MessageContent>
</FICOAccessMessage>
</fic:FICOAccess>

但以下是预期的回应

预期

<fic:FICOAccess xmlns:fic="http://www.ba.com/schema/mes/FICOAccessV01">
<FICOAccessMessage>
  <EncodingCharacterSetName>ASCII</EncodingCharacterSetName>
  <MessageContent>NLKDAJLSJADKLJASASJSADKJSADSADLKAS;KDA;LKSAL;;    DS DASS DSDDSS
 ABCJDKSJKKSALKSJLALA
                                       DLASJDKADJKSAJKASDJKA
KSAHDJKSAHDJKSAHDJSHAJDHKSADHSAJDSAJKDSAHDJSAHDKJSAHDJKSAHDJ
DSA                S         DSS                  D  D  D  D







ASDDSA          FD   FDS     FDFSS          FSS  FD

BEV PX   N24    FF           CC/YY  101     WW         MM
        </MessageContent>
   </FICOAccessMessage>
</fic:FICOAccess>

非常感谢任何帮助。提前致谢

1 个答案:

答案 0 :(得分:0)

您的for-each select表达式为tokenize(concat($accum,$txt), '\n\r?')[.]。谓词[.]是问题所在,因为它会删除序列的所有零长度字符串(您要保留它),请尝试将其删除。