嗨,下面我已经显示了我的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, ' ')"/>
<xsl:variable name="r" select="substring-after(substring-before(tag[name = '57D']/value, ' '), ' ')"/>
<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
每行Max必须是35,并不是每次数据都应该是35,所以如果不是,我们需要插入空格
好吧,实际的问题与最初的表述相比,需求发生了很大的变化。现在我将提供一个新的答案。
下面的转换行为如下:
- 通过换行来分隔行(使用 Split -string模板)
- 对于每一行,根据字符串长度检查35。如果字符串小于35,则添加填充以填充字符串直到35(使用padding模板)
- 所有行被连接
(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, '
')"/>
<xsl:variable name="r" select="substring-after($string, '
')"/>
<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
有一些选项,但这在很大程度上取决于您的其他约束,主要与您的XSLT处理器及其运行时环境有关:
- 可以使用xslt 2.0,还是需要坚持使用xslt 1.0 ?
- exslt函数可以提供帮助:http://exslt.org/str/index.html
- 您还可以通过递归地调用数据上的命名模板来处理数据,并使用xpath函数substring-before, substring-after -您可能还需要normalize-space以具有常量空白分隔符-参见http://www.w3.org/TR/xpath-functions/了解详细信息