如何在 XSLT 中将文本换行以适合窗口

2024-01-12

我正在使用 XSLT 2.0 从 XML 中提取数据。数据有很长的行,我想通过自动换行来使它们适合窗口大小。

XSLT 中可以吗?


您可以使用标准 XSLT 2.0 函数unparsed-text() http://www.w3.org/TR/2007/REC-xslt20-20070123/#function-unparsed-text直接在 XSLT 2.0 代码中读取文本文件。

然后只需使用:

replace(concat(normalize-space($text),' '),
                '(.{0,60}) ',
                '$1
')

解释:

这首先标准化空白,删除仅空白字符的前导和尾随序列,并用单个空格替换任何内部此类序列。

然后标准化的结果用作标准 XPath 2.0 函数的第一个参数replace() http://www.w3.org/TR/xpath-functions/#func-replace.

匹配模式是任何(以空格结尾的最多 61 个字符的最长可能序列)。

replacement 参数指定找到的任何此类序列都应替换为结尾空格之前的字符串,并与 NL 字符连接。

这是一个完整的解决方案,从文件中读取并格式化此文本C:\temp\delete\text.txt:

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.

XSLT 代码:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output method="text"/>

 <xsl:variable name="vText" select=
 "unparsed-text('file:///c:/temp/delete/text.txt')"/>

 <xsl:template match="/">
  <xsl:sequence select=
   "replace(concat(normalize-space($vText),' '),
            '(.{0,60}) ',
            '$1&#xA;')
   "/>
 </xsl:template>
</xsl:stylesheet>

结果是一组行,每行不超过固定长度 60:

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.

Update:

如果文本来自 XML 文件,只需对上述解决方案进行最小的更改即可完成此操作:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output method="text"/>

 <xsl:template match="/">
  <xsl:sequence select=
   "replace(concat(normalize-space(text),' '),
            '(.{0,60}) ',
            '$1&#xA;')
   "/>
 </xsl:template>
</xsl:stylesheet>

在这里,我假设所有文本都位于顶部元素的唯一文本节点子节点中(名为text) 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>

当将此转换应用于上面的 XML 文档时,将产生与第一个解决方案相同的结果。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 XSLT 中将文本换行以适合窗口 的相关文章

随机推荐