如何通过换行分割文本并使用变量填充连接

时间:2011-08-10 07:28:32

标签: xml xslt

在下面我已经显示我的xml存在一个像三行换行的值,但是怀疑如何根据xslt逻辑中的换行来获取值可以帮助我

     <message>
    <block4>
         <tag>
    <name>57D</name>
    <value>BVALESM M0746A
           051028GB ES00069074
           6A051028 GBES00069</value>
         </tag>
    </block4>
</message>

这是我的xslt,我正在尝试,但仍然有些问题,请建议我

<xsl:when test="tag[name = '57D'] ">
                    <xsl:variable name="l" select="substring-before(tag[name = '57D']/value, '&#13;')"/>
                    <xsl:variable name="r" select="substring-after(substring-before(tag[name = '57D']/value, '&#13;'), '&#13;')"/>


                    <xsl:value-of select="concat(substring(concat($l,'                                   '),1,35),substring(concat($r,'                                   '),1,35))"/>

                </xsl:when>

生成输出:

BVALESM M0746A 051028GB ES000690746A051028 GBES00069

它正在考虑第一个clrf之后的总值,因此它没有检查逻辑

需要的输出,如

BVALESM M0746A                     051028GB ES00069074                6A051028 GBES00069

每行最大值必须为35,并不是每次数据都应为35,如果不是意味着我们需要插入空格

2 个答案:

答案 0 :(得分:1)

好的,自最初的表述以来,实际问题的要求发生了很大变化。现在我将提供一个新答案。

以下转换的行为如下:

  • 按换行符拆分行(使用拆分字符串模板)
  • 对于每一行,请根据字符串长度检查35。如果字符串低于35,则添加填充以填充字符串直到35(使用填充模板)
  • 所有行都是连接的

[XSLT 1.0]

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="message/block4/tag">
        <xsl:variable name="result">
            <xsl:call-template name="split-string">
                <xsl:with-param name="string" select="value"/>
            </xsl:call-template>
        </xsl:variable>

        <xsl:value-of select="$result"/>

    </xsl:template>

    <xsl:template name="split-string">
        <xsl:param name="string"/> 
        <xsl:variable name="l" select="substring-before($string, '&#xA;')"/> 
        <xsl:variable name="r" select="substring-after($string, '&#xA;')"/>

        <xsl:choose>
            <xsl:when test="$l">
                <xsl:variable name="spaces">
                    <xsl:call-template name="padding">
                        <xsl:with-param name="times" 
                            select="35 - string-length(normalize-space($l))"/>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:value-of select="concat(normalize-space($l),$spaces)" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="normalize-space($string)" />
            </xsl:otherwise>
        </xsl:choose>

        <xsl:if test="$r">
            <xsl:call-template name="split-string">
                <xsl:with-param name="string" select="$r" /> 
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

    <xsl:template name="padding">
        <xsl:param name="times" select="0"/>
        <xsl:param name="spaces" select="''"/>
        <xsl:choose>
            <xsl:when   test="$times>0">
                <xsl:call-template name="padding">
                    <xsl:with-param name="times" select="$times - 1"/>
                    <xsl:with-param name="spaces" select="concat($spaces,' ')"/>    
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$spaces"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

应用于以下输入时:

<message>
    <block4>
        <tag>
            <name>57D</name>
            <value>BVALESM M0746A
                051028GB ES00069074
                6A051028 GBES00069</value>
        </tag>
    </block4>
</message>

产生此输出:

BVALESM M0746A                     051028GB ES00069074                6A051028 GBES00069

答案 1 :(得分:0)

有一些选项,但它在很大程度上取决于您的其他约束,主要与您的xslt处理器及其运行时环境有关:

  • 你可以使用xslt 2.0,还是需要坚持使用xslt 1.0?
  • exslt函数可以提供帮助:http://exslt.org/str/index.html
  • 您也可以通过递归调用数据上的命名模板来处理数据,并使用xpath函数substring-before,substring-after - 您可能还需要normalize-space来获得常量空格分隔符 - 请参阅{{3} }了解详情