如何在 XSLT 中换行以适应窗口 [英] How to wrap text to fit window in XSLT

查看:31
本文介绍了如何在 XSLT 中换行以适应窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在 XSLT 中可以吗?

解决方案

您可以使用标准的 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中读取并格式化此文本:

十二月13 — 与总统就职典礼一样,安全和监视是去年 1 月,华盛顿特区非常紧张.但正如乔治·W·布什准备宣誓就职,安全规划人员安装了额外的保护层:用于检测生物攻击的原型软件系统.美国商务部mod与地区卫生和应急计划机构一起分发了一个特殊的病人查询表,用于军事诊所、平民医院甚至援助沿游行路线和就职舞会的车站.软件快速分析了七个主要症状的投诉——从皮疹到喉咙痛——可能表明生物攻击早期阶段的模式.有一个简短的恐慌:系统注意到军队诊所的流感样症状激增.值得庆幸的是,测试证实这就是流感.

XSLT 代码:

<xsl:output method="text"/><xsl:variable name="vText" select="unparsed-text('file:///c:/temp/delete/text.txt')"/><xsl:template match="/"><xsl:序列选择="替换(concat(normalize-space($vText),' '),'(.{0,60}) ','$1​​&#xA;')"/></xsl:模板></xsl:stylesheet>

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

十二月13 - 与总统就职典礼一样,安全华盛顿特区的监视非常严密,去年一月.但是当乔治·W·布什准备采取就职誓言,安全规划师安装了一个额外的层保护:一个原型软件系统,用于检测生物攻击.美国mod联合与区域卫生和应急计划机构合作,向军方分发了一份特殊的患者查询表沿线的诊所、平民医院甚至援助站游行路线和就职舞会.软件快速分析了七个主要症状的投诉——从皮疹到喉咙痛——对于可能表明早期的模式生物攻击的阶段.有一个短暂的恐慌:系统注意到军队诊所的流感样症状激增.值得庆幸的是,测试证实这就是流感.

更新:

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

<xsl:output method="text"/><xsl:template match="/"><xsl:序列选择=替换(连接(标准化空间(文本),''),'(.{0,60}) ','$1​​&#xA;')"/></xsl:模板></xsl:stylesheet>

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

12 月 13 日 - 与总统就职典礼一样,安全和监视是去年 1 月,华盛顿特区非常紧张.但正如乔治·W·布什准备宣誓就职,安全规划人员安装了额外的保护层:用于检测生物攻击的原型软件系统.美国商务部mod与地区卫生和应急计划机构一起分发了一个特殊的病人查询表,用于军事诊所、平民医院甚至援助沿游行路线和就职舞会的车站.软件快速分析了七个主要症状的投诉——从皮疹到喉咙痛——可能表明生物攻击早期阶段的模式.有一个简短的恐慌:系统注意到军队诊所的流感样症状激增.值得庆幸的是,测试证实这就是流感.

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

I am extracting data from XML using XSLT 2.0. The data has long lines and I want to fit them into window size by automatically breaking lines.

Is it possible in XSLT?

解决方案

You can use the standard XSLT 2.0 function unparsed-text() to read a text file directly in your XSLT 2.0 code.

Then just use:

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

Explanation:

This first normalizes the white space, deleting the leading and trailing sequences of whitespace-only characters and replacing any inner such sequence with a single space.

Then the result of the normalization is used as the first argument to the standard XPath 2.0 function replace().

The match pattern is any (longest possible sequence of maximum 61 characters that ends with a space.

The replacement argument specifies that any such sequence found should be replaced by the string before the ending space, concatenated with a NL character.

Here is a complete solution, reading and formatting this text from the file 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.

The XSLT code:

<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>

The result is a set of lines, each of which doesn't exceed a fixed length of 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:

In case the text comes from an XML file, this can be done with a minimal change to the above solution:

<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>

Here I suppose that all the text is in the only text node child of the top element (named text) of the XML document:

<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>

When this transformation is applied to the XML document above, the same result as with the first solution is produced.

这篇关于如何在 XSLT 中换行以适应窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆