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

一般使用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!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值