xml - 如何避免文件末尾的新行 - xml to csv through xslt
问题描述
我正在尝试将以下 xml 的 xml 转换为 csv。输入xml的结构如下所示。我的 xslt 没有在新行中打印第二个“标题”。
在我的 XSLT 中,我使用了 for-each 并使用 if 条件来检查它是否是最后一个节点,但它正在考虑单个块的最后一个节点,而不是整个 xml 文件的最后一个节点。在现有的 xslt 中需要调整什么?
<?xml version="1.0" encoding="UTF-8"?>
<document>
<businessobjects>
<entry>
<HeaderLine>
<HeaderRow>H</HeaderRow>
<HeaderBusinessUnit>BU1</HeaderBusinessUnit>
</HeaderLine>
<LineDetails>
<Line>L</Line>
<Amount>100.00</Amount>
</LineDetails>
</entry>
<entry>
<HeaderLine>
<HeaderRow>H</HeaderRow>
<HeaderBusinessUnit>BU2</HeaderBusinessUnit>
</HeaderLine>
<LineDetails>
<Line>L</Line>
<Amount>200.00</Amount>
</LineDetails>
</entry>
</businessobjects>
</document>
Expected Output
===============
H|BU1
L|100.00
H|BU2
L|200.00
XSLT
====
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="newline" select="' '"/>
<xsl:template match="/">
<xsl:apply-templates select="//entry"/>
</xsl:template>
<xsl:template match="entry">
<xsl:for-each select="HeaderLine">
<xsl:value-of select="concat(HeaderRow,'|',HeaderBusinessUnit,$newline)"/>
</xsl:for-each>
<xsl:for-each select="LineDetails">
<xsl:if test="position()!=last()">
<xsl:value-of select="concat(Line,'|',Amount,$newline)"/>
</xsl:if>
<xsl:if test="position()=last()">
<xsl:value-of select="concat(Line,'|',Amount)"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output with above xslt
======================
H|BU1
L|100.00H|BU2
L|200.00
解决方案
<xsl:if test="position()!=last()">
<xsl:value-of select="$newline"/>
</xsl:if>
<xsl:if test="position()=last()"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Adding the above lines towards the end of the xslt solved the issue.
推荐阅读
- json - org.apache.kafka.common.errors.SerializationException 与 org.w3c.dom.Element 字段
- python - 在运行时重置/重新加载嵌套类的类属性
- spring-boot - 如何将包含 spring-hateoas 版本 1.0.0.M1 的 spring-boot-hateoas-starter 包含到我的项目中
- functional-programming - 简单的 HTML 代码并将 CSS、Python 和 JavaScript 与现有的 HTML 代码集成
- marklogic - 如何根据 MarkLogic 中元素或其他属性的关联值按属性进行选择性排序
- c++ - 使用 ROS2 环境变量创建 C++ 项目
- dc.js - 从 dc.js 中的用户输入构建折线图
- sql - 如何按条件分组?
- react-native - 如何将库的本地副本添加和编译到 Android React Native 模块中?
- javascript - 如何将 args[1] 转换为日期以输入 MySQL DB(Javascript,DiscordJS)?