【发布时间】:2016-11-15 08:21:41
【问题描述】:
我想在我的 XML 中每 10 个字符添加一个新标签 <br\>。 (不包括内部节点)
例如,如果我的输入是:
<main>
01234567890123
<a>
link
</a>
45678901234567901234
</main>
我希望是这样的:
<main>
0123456789<br\>0123
<a>
link
</a>
456789<br\>012345679<br\>01234
</main>
我写了这个函数:
<!-- Add new "HTML Break" Every X chars -->
<xsl:template name="Add-HTML-Break-After-X-Chars">
<xsl:param name="text" />
<xsl:param name="max-length" />
<xsl:choose>
<xsl:when test="string-length($text) > $max-length">
<!-- Show the X length of the text -->
<xsl:copy-of select="substring($text,1,$max-length)" />
<!-- Adding the special tag -->
<br/>
<!-- continue to add the rest of the text -->
<xsl:call-template name="Add-HTML-Break-After-X-Chars">
<xsl:with-param name="text" select="substring($text, $max-length + 1)" />
<xsl:with-param name="max-length" select="$max-length" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!-- Show the text (length is less then X) -->
<xsl:copy-of select="$text" />
</xsl:otherwise>
</xsl:choose>
问题是我得到的输出没有内部标签:
<main>
0123456789<br\>0123
link
45<br\>6789012345<br\>67901234
</main>
我正在使用此代码来调用模板:
<xsl:call-template name="Add-HTML-Break-After-X-Chars">
<xsl:with-param name="text" select="main" />
<xsl:with-param name="max-length" select="10" />
</xsl:call-template>
我也尝试过“价值”而不是“复制”。
有什么方法可以保留标签吗?
如果是这样,我怎样才能保留它们并“安全”插入新标签?
【问题讨论】:
-
这实际上取决于调用者模板,请分享您的完整xsl以查找实际原因。
-
你把
<a>标签算作一个字符吗? -
@MananSheth 我添加了 XSL 代码,Tim C - No
标签: html xml xslt tags xslt-1.0