根据长度和内容使用 xsl 拆分元素
Split element with xsl based on length and content
我有一个内容很长的 XML 元素,比方说
<LongString>Here we have a lengthy string which contains lots of words separated by spaces and it needs to be split</LongString>
我想根据以下规则将其拆分为两个元素(我们称它们为 StartString 和 EndString):
- StartString 将包含尽可能多的字符,但最多 40 个字符(如果 LongString 少于 40 个字符,则 StartString 将等于 LongString,而 EndString 将为空)
- 如果 LongString 的前 40 个字符有 space 个,则将在 space 处拆分
(如果前40个字符内没有space,那么无论如何都会在40个字符后拆分)
- StartString 不会以 space 结尾且 EndString 不会以 space
开头
所以上面的例子应该转化为:
<StartString>Here we have a lengthy string which</StartString>
<EndString>contains lots of words separated by spaces and it needs to be split</EndString>
鉴于
<LongString>Just a short string</LongString>
会变成
<StartString>Just a short string</StartString>
<EndString></EndString>
和
<LongString>12345678901234567890123456789012345678901234567890 spaces only at the end</LongString>
会变成
<StartString>1234567890123456789012345678901234567890</StartString>
<EndString>1234567890 spaces only at the end</EndString>
如何使用 XSLT 完成此操作?
在 XSLT 2 或更高版本中,如果您想 process/analyze 文本和创建节点,xsl:analyze-string
可以提供帮助:
<xsl:template match="LongString">
<xsl:analyze-string select="." regex="^(.{{1,39}}$|[^ ]{{40}}|.{{1,39}} )">
<xsl:matching-substring>
<StartString>
<xsl:value-of select="if (ends-with(., ' ')) then substring(., 1, string-length(.) - 1) else ."/>
</StartString>
</xsl:matching-substring>
<xsl:non-matching-substring>
<EndString>
<xsl:value-of select="."/>
</EndString>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
我有一个内容很长的 XML 元素,比方说
<LongString>Here we have a lengthy string which contains lots of words separated by spaces and it needs to be split</LongString>
我想根据以下规则将其拆分为两个元素(我们称它们为 StartString 和 EndString):
- StartString 将包含尽可能多的字符,但最多 40 个字符(如果 LongString 少于 40 个字符,则 StartString 将等于 LongString,而 EndString 将为空)
- 如果 LongString 的前 40 个字符有 space 个,则将在 space 处拆分 (如果前40个字符内没有space,那么无论如何都会在40个字符后拆分)
- StartString 不会以 space 结尾且 EndString 不会以 space 开头
所以上面的例子应该转化为:
<StartString>Here we have a lengthy string which</StartString>
<EndString>contains lots of words separated by spaces and it needs to be split</EndString>
鉴于
<LongString>Just a short string</LongString>
会变成
<StartString>Just a short string</StartString>
<EndString></EndString>
和
<LongString>12345678901234567890123456789012345678901234567890 spaces only at the end</LongString>
会变成
<StartString>1234567890123456789012345678901234567890</StartString>
<EndString>1234567890 spaces only at the end</EndString>
如何使用 XSLT 完成此操作?
在 XSLT 2 或更高版本中,如果您想 process/analyze 文本和创建节点,xsl:analyze-string
可以提供帮助:
<xsl:template match="LongString">
<xsl:analyze-string select="." regex="^(.{{1,39}}$|[^ ]{{40}}|.{{1,39}} )">
<xsl:matching-substring>
<StartString>
<xsl:value-of select="if (ends-with(., ' ')) then substring(., 1, string-length(.) - 1) else ."/>
</StartString>
</xsl:matching-substring>
<xsl:non-matching-substring>
<EndString>
<xsl:value-of select="."/>
</EndString>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>