35

我需要使用 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>

一些对我有帮助的重要事情:

  1. 使用 xsl:output 省略输出文档开头的标准声明
  2. 使用 xml:space="preserve" 属性来保留我在 xsl:for-each 标记中编写的任何空格。这还要求我将所有代码都写在 for-each 标记中,包括该标记在内,都写在一行上(换行符除外)。
  3. 使用 插入换行符 - 我不得不在这里省略标准 xml 缩进。

此 xslt 的结果和所需输出是:

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

正如我所说,任何关于如何更优雅地做到这一点的建议都将不胜感激。


跟进 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>
4

2 回答 2

28
  • 您可以定义一个模板来匹配script/command并消除xsl:for-each
  • concat()可用于缩短表达式并使您免于显式插入如此多<xsl:text><xsl:value-of>元素。
  • 使用实体引用&#xA;作为回车符,而不是依赖于保留<xsl:text>元素之间的换行符更安全一些,因为代码格式不会弄乱换行符。此外,对我来说,它读作明确的换行符,更容易理解其意图。

<?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
                    ,'&#xA;')"/>
    </xsl:template>

</xsl:stylesheet>
于 2011-05-06T11:18:25.517 回答
9

只是为了好玩:这可以以非常通用和紧凑的方式完成

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="*">
        <xsl:apply-templates select="node()|@*"/>
        <xsl:text> </xsl:text>
    </xsl:template>

    <xsl:template match="username">
       at -f <xsl:apply-templates select="*|@*"/>
    </xsl:template>
</xsl:stylesheet>

应用于此 XML 文档时

<script>
 <command>
  <username>John</username>
  <startTime hours="09:" minutes="33"/>
  <startDate>05/05/2011</startDate>

  <username>Kate</username>
  <startTime hours="09:" minutes="33"/>
  <startDate>05/05/2011</startDate>

  <username>Peter</username>
  <startTime hours="09:" minutes="33"/>
  <startDate>05/05/2011</startDate>
 </command>
</script>

产生了想要的正确结果:

   at -f 09:33 05/05/2011 
   at -f 09:33 05/05/2011 
   at -f 09:33 05/05/2011  

注意:如果要输出的所有数据都包含在文本节点中 - 而不是属性中,则此一般方法最适用。

于 2011-05-06T13:24:12.820 回答