在我们的一个要求中,我们收到一个n字符的字符串,在我们发送给SAP的提供商处。由于目标端的一些限制,我们需要检查字符串,如果它超过100个字符,我们需要将其拆分并发送到2个不同段(同名)的目标应用程序,如
输入 - 这是测试消息......(直到150个字符)
在XSLT转换中- 我们需要将其拆分为
<text>first 100 char<text>
<text> 101 to 200 char<text>
...
由于未预定义字符数,所以我不能在这里使用子字符串函数。这应该是循环的一部分..
有人可以帮忙吗。
答案 0 :(得分:2)
在XSLT 2.0中,您可以执行以下操作:
简化示例,将输入分为(最多)6个字符的标记。
<强> XML 强>
<input>abcde1abcde2abcde3abcde4abcde5abcde6abcde7abcde8abc</input>
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<output>
<xsl:variable name="string" select="input" />
<xsl:for-each select="0 to (string-length($string) - 1) idiv 6">
<token>
<xsl:value-of select="substring($string, . * 6 + 1, 6)" />
</token>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
<强>结果强>
<output>
<token>abcde1</token>
<token>abcde2</token>
<token>abcde3</token>
<token>abcde4</token>
<token>abcde5</token>
<token>abcde6</token>
<token>abcde7</token>
<token>abcde8</token>
<token>abc</token>
</output>
答案 1 :(得分:2)
这是1.0的选项(也适用于2.0)。它使用递归模板调用。
XML输入(感谢Michael)
<input>abcde1abcde2abcde3abcde4abcde5abcde6abcde7abcde8abc</input>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<output>
<xsl:call-template name="tokenize">
<xsl:with-param name="input" select="."/>
</xsl:call-template>
</output>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="input"/>
<xsl:param name="length" select="6"/>
<token><xsl:value-of select="substring($input,1,$length)"/></token>
<xsl:if test="substring($input,$length+1)">
<xsl:call-template name="tokenize">
<xsl:with-param name="input" select="substring($input,$length+1)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
XML输出
<output>
<token>abcde1</token>
<token>abcde2</token>
<token>abcde3</token>
<token>abcde4</token>
<token>abcde5</token>
<token>abcde6</token>
<token>abcde7</token>
<token>abcde8</token>
<token>abc</token>
</output>