简体   繁体   English

XML 到 CSV 使用 XSLT 用于单个标签中由空格分隔的多个记录

[英]XML to CSV using XSLT for multiple records separated by space in single tag

We are trying to convert XML to CSV using XSLT.我们正在尝试使用 XSLT 将 XML 转换为 CSV。 I tried this link XML to CSV Using XSLT and Converting XML to CSV (using XSLT) . I tried this link XML to CSV Using XSLT and Converting XML to CSV (using XSLT) .

  • It is converting normal xml to csv but in my Case I am unable to get the desired output.它将正常的 xml 转换为 csv 但在我的情况下,我无法获得所需的 output。
  • In my case I have multiple records which are separated by space in a single tag as mentioned in below xml input.在我的情况下,我有多个记录,这些记录在单个标记中用空格分隔,如下面的 xml 输入中所述。

Eg:- Project and Rating tag contains the multiple records separated by space.例如:- Project 和 Rating 标记包含由空格分隔的多个记录。

Input XML:输入 XML:

<?xml version = "1.0"?> 
<?xml-stylesheet type = "text/xsl" version="2.0" href = "csvconverted.xsl"?> 
<TestData>
<project>Project-1 Project-2 Project-3</project>
<rating>2 3 5</rating>
<date>21-12-2018 21-06-2020 21-12-20</date>
</TestData>

XSL:- XSL:-

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" encoding="utf-8" />

  <xsl:param name="delim" select="','" />
  <xsl:param name="quote" select="'&quot;'" />
  <xsl:param name="break" select="'&#xA;'" />

  <xsl:template match="/">
    <xsl:apply-templates select="TestData" />
  </xsl:template>

  <xsl:template match="TestData">
    <xsl:apply-templates />
    <xsl:if test="following-sibling::*">
      <xsl:value-of select="$break" />
    </xsl:if>
  </xsl:template>

  <xsl:template match="*">
    <!-- remove normalize-space() if you want keep white-space at it is --> 
    <xsl:value-of select="concat($quote, normalize-space(), $quote)" />
    <xsl:if test="following-sibling::*">
      <xsl:value-of select="$delim" />
    </xsl:if>
  </xsl:template>

  <xsl:template match="text()" />
</xsl:stylesheet>

Expected Output:预期 Output:

project,rating,date
Project-1,2,21-12-2018
Project-2,3,21-06-2020
Project-3,5,21-12-20

Actual Output using the above code:-实际 Output 使用上面的代码:-

"Project-1 Project-2 Project-3","2 3 5","21-12-2018 21-06-2020 21-12-20"

Will be grateful if you could guide me further.如果您能进一步指导我,将不胜感激。

I am going to assume you are using an XSLT 1.0 processor with no support for extensions.我假设您使用的是不支持扩展的 XSLT 1.0 处理器。

I will also assume that the structure of the input XML is known in advance and the only thing that varies is the number of values in the 3 space-delimited strings.我还将假设输入 XML 的结构是预先知道的,唯一不同的是 3 个以空格分隔的字符串中的值的数量。

XSLT 1.0 XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/TestData">
    <!-- header -->
    <xsl:text>project,rating,date&#10;</xsl:text>
    <xsl:call-template name="generate-rows">
        <xsl:with-param name="project" select="project"/>
        <xsl:with-param name="rating" select="rating"/>
        <xsl:with-param name="date" select="date"/>
    </xsl:call-template>
</xsl:template>
 
<xsl:template name="generate-rows">
    <xsl:param name="project"/>
    <xsl:param name="rating"/>
    <xsl:param name="date"/>
    <xsl:param name="delimiter" select="' '"/>
    <!-- generate row -->
    <xsl:value-of select="substring-before(concat($project, $delimiter), $delimiter)" />
    <xsl:text>,</xsl:text>
    <xsl:value-of select="substring-before(concat($rating, $delimiter), $delimiter)" />
    <xsl:text>,</xsl:text>
    <xsl:value-of select="substring-before(concat($date, $delimiter), $delimiter)" />
    <xsl:text>&#10;</xsl:text>
    <xsl:if test="contains($project, $delimiter)">
        <!-- recursive call -->
        <xsl:call-template name="generate-rows">
            <xsl:with-param name="project" select="substring-after($project, $delimiter)"/>
            <xsl:with-param name="rating" select="substring-after($rating, $delimiter)"/>
            <xsl:with-param name="date" select="substring-after($date, $delimiter)"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>
 
</xsl:stylesheet>

Demo : https://xsltfiddle.liberty-development.net/3MEdvhL演示https://xsltfiddle.liberty-development.net/3MEdvhL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM