使用xsl输出纯文本 [英] use xsl to output plain text

查看:215
本文介绍了使用xsl输出纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用XSL来从XML生成简单的纯文本输出。由于我没有在网上找到任何好的,简明的例子,我决定在这里发布我的解决方案。任何链接引用一个更好的例子当然会赞赏:

 <?xml version =1.0encoding =UTF -8\" >?; 
< xsl:template match =/>
< / xsl:text>< / xsl:for-each>
< / xsl:template>
< / xsl:stylesheet>

一些重要的事情帮助我在这里:


  1. 使用xsl:output在输出文档的开始部分省略标准声明
  2. 使用xml:space =preserve 属性来保留我在xsl:for-each标签中写的任何空格。这也要求我在for-each标签内写入所有的代码,包括标签以及单行(除了换行符)。

  3. 用来插入换行符 - 再次,我不得不在这里省略标准的xml缩进。


    这个xslt的结果和期望的输出是:

    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



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






    后续2011-05-08:

    以下是我正在处理的xml类型:

     < script xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:noNamespaceSchemaLocation =script.xsd> 
    <命令>
    < username> alluser< / username>
    < startDate> 17.4.2010< / startDate>
    < / command>
    < / script>


    解决方案


    • 匹配脚本/命令,并消除 xsl:for-each

    • concat()可以用来缩短表达式并且避免显式插入这么多的< xsl:text> < xsl:value-of> 元素

    • 使用实体引用& ;#xA; 用于回车,而不是依靠保留< xsl:text> 元素之间的换行符更安全,因为代码格式不会混淆你的换行符。此外,对我来说,它读作为一个明确的换行符,并更容易理解的目的。


      $ hr

       <?xml version =1.0encoding =UTF-8?> 
      xmlns:fo =http:// www。 w3.org/1999/XSL/Format>

      < xsl:template match =script / command>
      ,username
      ,''
      ,startTime / @ hours
      ': '
      ,startTime / @分钟
      ,''
      ,startDate
      ,'& #xA;')/>
      < / xsl:template>

      < / xsl:stylesheet>


      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:

      1. the use of xsl:output to omit the standard declaration at the beginning of the output document
      2. 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).
      3. 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>
      

      解决方案

      • You can define a template to match on script/command and eliminate the xsl: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 &#xA; 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
                          ,'&#xA;')"/>
          </xsl:template>
      
      </xsl:stylesheet>
      

      这篇关于使用xsl输出纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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