我正在使用此代码每 150 个字符中断一次 XML 字符串。
<xsl:variable name="val_cr_ln" select=" '
' " />
每次发生此变量时,我都需要进行计数。
有人能告诉我我会怎么做吗?
谢谢
要查找特定字符串在 XML 文档中出现的次数:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exslstr="http://exslt.org/strings"
>
<xsl:template match="/">
<xsl:variable name='xml_as_str'>
<xsl:apply-templates select='*' />
</xsl:variable>
<p>There are <xsl:value-of select='count(exslstr:split($xml_as_str, "string")) - 1' /> occurrences of "string" in there source XML.</p>
</xsl:template>
</xsl:stylesheet>
这是一个 XPath 单行:
string-length(/) - string-length(translate(string(/), $vNL, ''))
这计算为 XML 文档中所有文本节点的串联中 NL 字符的总数。
说明:
translate(string(/), $vNL, '')
从文档节点的字符串值(XML 文档中所有文本节点的串联)生成另一个字符串,其中任何 NL 字符都已替换为空字符串(已删除)。
然后,从总字符串长度中减去它,就可以准确地得到(删除的)NL 字符的数量。
这是一个完整的代码示例:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:variable name="vNL" select="'
'"/>
<xsl:template match="/">
<xsl:value-of select=
"string-length(/)
- string-length(translate(string(/), $vNL, ''))
"/>
</xsl:template>
</xsl:stylesheet>
当此转换应用于以下 XML 文档时:
<text>
Dec. 13 — As always for a presidential inaugural, security and surveillance were
extremely tight in Washington, DC, last January. But as George W. Bush prepared to
take the oath of office, security planners installed an extra layer of protection: a
prototype software system to detect a biological attack. The U.S. Department of
Defense, together with regional health and emergency-planning agencies, distributed
a special patient-query sheet to military clinics, civilian hospitals and even aid
stations along the parade route and at the inaugural balls. Software quickly
analyzed complaints of seven key symptoms — from rashes to sore throats — for
patterns that might indicate the early stages of a bio-attack. There was a brief
scare: the system noticed a surge in flulike symptoms at military clinics.
Thankfully, tests confirmed it was just that — the flu.
</text>
产生了想要的正确结果:
12