使用xsl输出纯文本
我需要使用 XSL 从 XML 生成简单的纯文本输出。由于我在网上没有找到任何好的、简洁的示例,因此我决定在这里发布我的解决方案。当然,任何引用更好示例的链接都会受到赞赏:
<?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>
这里帮助我的一些重要事情:
- 使用 xsl:output 省略输出文档开头的标准声明
- 使用 xml:space="保留”属性来保留我在 xsl:for-each 标记中编写的任何空格。这还要求我将 for-each 标记内的所有代码(包括该标记)写在一行上(换行符除外)。
- 使用 插入换行符 - 我再次不得不在这里省略标准 xml 缩进。
此 xslt 的预期输出为:
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
at -f alluser 3:58 22.9.2010
正如我所说,任何关于如何更优雅地做到这一点的建议将不胜感激。
跟进 2011-05-08:
这是我正在处理的 xml 类型:
<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>
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:
- the use of xsl:output to omit the standard declaration at the beginning of the output document
- the use of the xml:space="preserve" attribute to preserve any whitespace I wrote within the xsl:for-each tag. This also required me to write all code within the for-each tag, including that tag as well, on a single line (with the exception of the line break).
- the use of to insert a line break - again I had to omit standard xml indenting here.
The resulting and desired output for this xslt was:
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.
FOLLOW-UP 2011-05-08:
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
script/command
并消除xsl:for-each
concat()
可用于缩短表达式并避免显式插入如此多的
和
元素。作为回车符,而不是依赖于保留
元素之间的换行符更安全一点,因为代码格式化不会弄乱你的换行符。另外,对我来说,它读起来就像一个明确的换行符,更容易理解其意图。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.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 文档时:
生成所需的正确结果: strong>
注意:如果要输出的所有数据都包含在文本节点中(而不是属性中),则这种通用方法最适用。
Just for fun: this can be done in a very general and compact way:
when applied on this XML document:
the wanted, correct result is produced:
Note: This genaral approach is best applicable if all the data to be output is contained in text nodes -- not in attributes.