有关使用xsl输出csv格式文档的实践小结

来源:互联网 发布:configparser python 编辑:程序博客网 时间:2024/05/29 17:59

一般使用xsl样式文件显示xml文件内容,输出html文件格式(用于展示)

但可能有时候我们还是有输出csv格式的需求,实现方案应该是多元化的,其中利用xsl样式文件是一种不错的选择

注:问题的解决核心是每行中的元素之间添加“,“ 号

以下是个人实践过程的总结,供大家讨论与学习

1)需添加 <xsl:ouput ....>元素,否则生成的文档中第一行为有 <?xml version=...>

2)利用<xsl:text></xsl:text>添加文本

3)利用<xsl:value-of select="'&#10;'"/>添加换行

如下为实践的示例

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output omit-xml-declaration="yes" indent="no" method="text"/><xsl:template match="/">  <xsl:text>priority,rank,type,instanceHash,category,className,sourcePath,lineStart,lineEnd,shortMessage,longMessage </xsl:text>  <xsl:value-of select="''"/>  <xsl:for-each select="/BugCollection/BugInstance"><xsl:sort select="@priority" data-type="number" /> <xsl:sort select="@rank" data-type="number"/><xsl:choose>  <xsl:when test="@priority = 1">High</xsl:when>  <xsl:when test="@priority = 2">Medium</xsl:when>  <xsl:when test="@priority = 3">Low</xsl:when>  <xsl:otherwise>Unknown</xsl:otherwise></xsl:choose><xsl:text>,</xsl:text><xsl:value-of select="@rank" /><xsl:text>,</xsl:text><xsl:value-of select="@type" /><xsl:text>,</xsl:text><xsl:value-of select="@instanceHash" /><xsl:text>,</xsl:text><xsl:value-of select="@category" /><xsl:text>,</xsl:text><xsl:value-of select="Class/SourceLine/@classname" /><xsl:text>,</xsl:text><xsl:value-of select="Class/SourceLine/@sourcepath" /><xsl:text>,</xsl:text><xsl:value-of select="Class/SourceLine/@start" /><xsl:text>,</xsl:text><xsl:value-of select="Class/SourceLine/@end" /><xsl:text>,</xsl:text><xsl:value-of select="ShortMessage" /><xsl:text>,</xsl:text><xsl:value-of select="LongMessage" /><xsl:value-of select="''"/>  </xsl:for-each></xsl:template></xsl:stylesheet>


注:利用xmlstart sel -C 选项来学习xsl转化xml到文本格式来学习

如下示例:

xmlstart sel -C的使用

xmlstarlet sel -C -T -t -m /BugCollection/BugInstance -s A:N:U "@priority" -s A:N:U "@rank" -v "concat(@instanceHash, '|', @priority, '|', @rank, '|',  @type, '|',  @category, '|', Class/SourceLine/@classname, '|',  Class/SourceLine/@sourcepath, '|',  Class/SourceLine/@start, '|', Class/SourceLine/@end, '|', ShortMessage,'|', LongMessage)" -n

对应的xsl

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common" version="1.0" extension-element-prefixes="exslt">  <xsl:output omit-xml-declaration="yes" indent="no" method="text"/>  <xsl:template match="/">    <xsl:for-each select="/BugCollection/BugInstance">      <xsl:sort order="ascending" data-type="number" case-order="upper-first" select="@priority"/>      <xsl:sort order="ascending" data-type="number" case-order="upper-first" select="@rank"/>      <xsl:call-template name="value-of-template">        <xsl:with-param name="select" select="concat(@instanceHash, '|', @priority, '|', @rank, '|',  @type, '|',  @category, '|', Class/SourceLine/@classname, '|',  Class/SourceLine/@sourcepath, '|',  Class/SourceLine/@start, '|', Class/SourceLine/@end, '|', ShortMessage,'|', LongMessage)"/>      </xsl:call-template>      <xsl:value-of select="''"/>    </xsl:for-each>  </xsl:template>  <xsl:template name="value-of-template">    <xsl:param name="select"/>    <xsl:value-of select="$select"/>    <xsl:for-each select="exslt:node-set($select)[position()>1]">      <xsl:value-of select="."/>      <xsl:value-of select="''"/>    </xsl:for-each>  </xsl:template></xsl:stylesheet>


注:可以使用vs2005/2008/2010来调试xsl!