有关使用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="' '"/>添加换行
如下为实践的示例
<?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!
- 有关使用xsl输出csv格式文档的实践小结
- xsl排序有关的
- 有关文档注释的格式……
- ACM有关输出格式的总结
- 【codecs.BOM】使用Python程序输出csv格式数据
- 使用log4net将日志文件输出替csv格式
- 有关输出对齐格式
- PHP生成CSV格式文档
- ThinkPHP 导出csv格式文档
- php输出excel的简单方法(csv格式)
- 如何将xml格式的字符串结合xsl文件转换为html 格式字符串输出
- 使用XSL的value-of 显示XML文档
- 有关csv格式的数据转换成libsvm格式的数据
- 使用XSL将XML文档中的CDATA注释输出为HTML文本
- XML的格式问题XSL
- CSV格式文件的小结
- 文档输出格式
- CSV的格式
- 23种设计模式(11):责任连模式
- Codeforces Beta Round #93 (Div. 2 Only) A题
- @高三--我的一些语录,一些心情。上课无聊,写在草稿本上的
- POJ 3613 Cow Relays Floyd+快速幂加速
- sqoop错误解决
- 有关使用xsl输出csv格式文档的实践小结
- 跨平台调试,安卓真机无法显示个别图片的问题(一)
- 回调函数和函数指针
- VC++中用内存映射文件
- Flex 弹出窗派发事件主页面用于接收
- S5PV210的16bit硬件ECC校验
- Webkit内核探究
- ios epub电子书阅读器demo
- 恢复正在运行的脚本到文件