XSLT - 在循环中的每第 n 个字符上拆分字符串 [英] XSLT - split string on every nth character in loop

查看:47
本文介绍了XSLT - 在循环中的每第 n 个字符上拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的一个要求中,我们收到一个包含 n 个字符的字符串,我们在提供者处将其发送给 SAP.由于目标端的一些限制,我们需要检查字符串,如果它超过 100 个字符,我们需要将其拆分并发送到 2 个不同的段(同名)中的目标应用程序,如

in one of our requirement we are receiving a string of n character and at provider at we we sending that to SAP. Due to some limitation at target end, we need to check for string that if its more then 100 char, we need to split that and send to target application in 2 different segment(same name) like

输入 - 这是一条测试消息......(直到 150 个字符)

input - This is a test message......(till 150 char)

在 XSLT 转换中 - 我们需要像这样拆分它

in XSLT transformation -we need to split it like

<text>first 100 char<text>
<text> 101 to 200 char<text>
...

由于字符数未预定义,所以我不能在这里使用子字符串函数.这应该是循环的一部分..

Since number of character is not predefined so I cant use substring function here. This should be as a part of loop..

有人可以帮忙吗.

推荐答案

在 XSLT 2.0 中,您可以这样做:

In XSLT 2.0, you could do something like this:

示例简化为将输入拆分为(最多)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>

这篇关于XSLT - 在循环中的每第 n 个字符上拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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