c# - XSLT 将 xml 转换为 csv 并为每条记录重复同级

标签 c# xml xslt

下面是用作源的 XML 文件:

<?xml version="1.0" encoding="UTF-8" ?>
<billing-log>
  <log-start-date>2012-08-17T00:00:00-05:00</log-start-date>
  <player-name>Player1</player-name>
  <schema-version>1</schema-version>
  <player-uuid>12345</player-uuid>
  <log-end-date>2012-08-17T23:59:59.999-05:00</log-end-date>
  <entry>
     <page>Page1</page>
     <path>Path1</path>
     <in>2012-08-16T23:59:52.170-05:00</in>
     <out>2012-08-17T00:00:00.186-05:00</out>
  </entry>
  <entry>
     <page>Page2</page>
     <path>Path2</path>
     <in>2012-08-17T00:00:00.186-05:00</in>
     <out>2012-08-17T00:00:08.561-05:00</out>
  </entry>
</billing-log>

我使用的xsl文件是这样的:

<?xml version="1.0"?>
<xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
    <xsl:template match="log-start-date"></xsl:template>
    <xsl:template match="player-name"></xsl:template>
    <xsl:template match="schema-version"></xsl:template>
    <xsl:template match="player-uuid"></xsl:template>
    <xsl:template match="log-end-date"></xsl:template>
    <xsl:template match="//entry">
        <xsl:text>"</xsl:text>
        <xsl:value-of select="normalize-space(page)"/><xsl:text/>
        <xsl:text>","</xsl:text>
        <xsl:value-of select="normalize-space(path)"/><xsl:text/>
        <xsl:text>","</xsl:text>
        <xsl:value-of select="normalize-space(in)"/><xsl:text/>
        <xsl:text>","</xsl:text>
        <xsl:value-of select="normalize-space(out)"/><xsl:text/>
        <xsl:text>"&#10;&#13;</xsl:text>
<xsl:text disable-output-escaping = "yes" >
</xsl:text>
</xsl:template>
</xsl:stylesheet>

我将这些输入到以下 c# 控制台应用程序以将其转换为 CSV 并将其写入文件。:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Xsl;

namespace xml2csv
{
    class Program
    {
        static int Main(string[] args)
        {
            if (args.Length != 3)
            {
                System.Console.WriteLine("Wrong number of aruguments");
                return -1;
            }

            string xmlFile = args[0];
            string xslFile = args[1];
            string outputFile = args[2];

            //Create a new XML document and load the XML file
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFile);
            //Create an XSLT object and load the XSLT file            
            XslCompiledTransform xslTran = new XslCompiledTransform();
            xslTran.Load(xslFile);
            // This is for generating the output
            XmlTextWriter writer = new XmlTextWriter(outputFile, System.Text.Encoding.ASCII);
            //Apply the transformation and write disk
            xslTran.Transform(xmlDoc, null, writer);
            //Close the writer
            writer.Close();

            return 1;

        }
    }
}

它产生的结果是:

"Page1","Path1","2012-08 16T23:59:52.170-05:00","2012-08-17T00:00:00.186-05:00"
"Page2","Path2","2012-08-17T00:00:00.186-05:00","2012-08-17T00:00:08.561-05:00"

我想要的是将日志结束日期的兄弟节点值添加到入口节点的记录中。所以输出看起来像这样:

"2012-08-17T23:59:59.999-05:00","Page1","Path1","2012-08 16T23:59:52.170-05:00","2012-08-17T00:00:00.186-05:00"
"2012-08-17T23:59:59.999-05:00","Page2","Path2","2012-08-17T00:00:00.186-05:00","2012-08-17T00:00:08.561-05:00"

如有任何帮助,我们将不胜感激。

最佳答案

只需添加:

<xsl:value-of select="concat('&quot;',normalize-space(../log-end-date),'&quot;')"/>

作为 entry 模板的第一个 child 。

关于c# - XSLT 将 xml 转换为 csv 并为每条记录重复同级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13314820/

相关文章:

c# - 在 C# 中使用 WMI 查询通过盘符获取硬盘序列号

c# - 当另一个类中的先决条件属性发生更改时,为依赖属性提高 PropertyChanged?

python - 警告 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

xml - xpath:测试节点是否是除指定ok元素之外的唯一子节点

xslt排序输出xml

c# - 是否有一个 C# 类用于跟踪服务方法调用,如堆栈跟踪

c# - 从调试中检索远程调用失败的 AD 组成员资格

java - 如何在 Android Canvas 上将文本中心向左对齐

xml - 更改XML文件中的元素值

sql-server - XSL - GML 到 json