使用 xsl 输出纯文本 [英] use xsl to output plain text
问题描述
我需要使用 XSL 从 XML 生成简单的纯文本输出.由于我没有在网上找到任何好的、简洁的例子,我决定在这里发布我的解决方案.当然,任何引用更好示例的链接都会受到赞赏:
I needed to use XSL to generate simple plain text output from XML. Since I didn't find any good, concise example online, I decided to post my solution here. Any links referring to a better example would of course be appreciated:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" >
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
<xsl:template match="/">
<xsl:for-each select="script/command" xml:space="preserve">at -f <xsl:value-of select="username"/> <xsl:value-of select="startTime/@hours"/>:<xsl:value-of select="startTime/@minutes"/> <xsl:value-of select="startDate"/><xsl:text>
</xsl:text></xsl:for-each>
</xsl:template>
</xsl:stylesheet>
一些重要的事情对我有帮助:
A few important things that helped me out here:
- 使用 xsl:output 省略输出文档开头的标准声明
- 使用 xml:space="preserve" 属性来保留我在 xsl:for-each 标签中写入的任何空白.这也要求我将 for-each 标签中的所有代码,包括那个标签,写在一行上(换行符除外).
- 插入换行符的使用 - 我再次不得不在这里省略标准的 xml 缩进.
这个 xslt 的结果和期望输出是:
The resulting and desired output for this xslt was:
at -f alluser 23:58 17.4.2010
在-f ggroup67 7:58 28.4.2010
在 -f ggroup70 15:58 18.4.2010
在 -f alluser 23:58 18.4.2010
在-f ggroup61 7:58 22.9.2010
在-f ggroup60 23:58 21.9.2010
在 -f alluser 3:58 22.9.2010
at -f alluser 23:58 17.4.2010
at -f ggroup67 7:58 28.4.2010
at -f ggroup70 15:58 18.4.2010
at -f alluser 23:58 18.4.2010
at -f ggroup61 7:58 22.9.2010
at -f ggroup60 23:58 21.9.2010
at -f alluser 3:58 22.9.2010
正如我所说,任何有关如何更优雅地执行此操作的建议将不胜感激.
As I said, any suggestions of how to do this more elegantly would be appreciated.
跟进 2011-05-08:
FOLLOW-UP 2011-05-08:
这是我正在处理的 xml 类型:
Here's the type of xml I am treating:
<script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="script.xsd">
<command>
<username>alluser</username>
<startTime minutes="58" hours="23"/>
<startDate>17.4.2010</startDate>
</command>
</script>
推荐答案
- 您可以定义一个模板来匹配
script/command
并消除xsl:for-each
concat()
可用于缩短表达式并使您免于显式插入如此多的
和
元素.- 使用实体引用


进行回车,而不是依赖于保留
之间的换行符element 更安全一点,因为代码格式不会弄乱你的换行符.此外,对我来说,它读作明确的换行符,更容易理解意图. - You can define a template to match on
script/command
and eliminate thexsl:for-each
concat()
can be used to shorten the expression and save you from explicitly inserting so many<xsl:text>
and<xsl:value-of>
elements.- The use of an entity reference


for the carriage return, rather than relying on preserving the line-break between your<xsl:text>
element is a bit more safe, since code formatting won't mess up your line breaks. Also, for me, it reads as an explicit line-break and is easier to understand the intent.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format" >
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
<xsl:template match="script/command">
<xsl:value-of select="concat('at -f '
,username
,' '
,startTime/@hours
,':'
,startTime/@minutes
,' '
,startDate
,'
')"/>
</xsl:template>
</xsl:stylesheet>
这篇关于使用 xsl 输出纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!