如何在XSLT中包装文本以适应窗口

时间:2011-10-21 13:37:48

标签: xml xslt xslt-2.0 textwrapping

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

是否可以在XSLT中使用?

2 个答案:

答案 0 :(得分:6)

您可以使用标准XSLT 2.0函数unparsed-text()直接在XSLT 2.0代码中读取文本文件。

然后使用

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

<强>解释

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

然后将规范化的结果用作标准XPath 2.0函数 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.

<强>更新

如果文本来自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>

这里我假设所有文本都在XML文档的顶部元素(名为text)的唯一文本节点子节点中:

<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文档时,会产生与第一个解决方案相同的结果。

答案 1 :(得分:2)

我认为可以使用tokenize()<xsl:analyze-string>来有效地执行此操作,使用允许最多(比方说)70个字符的正则表达式,并以破坏字符(例如空格)结束。

对于显式代码,请参阅xquery word wrap上的XPath和XSLT答案。