gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:13:34 25 4
gpt4 key购买 nike

下面是用作源的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com