如何编写 XSLT 将 XML 转换为 CSV?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/952365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:33:02  来源:igfitidea点击:

How do I write an XSLT to transform XML to CSV?

xmlxsltcsv

提问by mad_typist

I'm trying to write an XSLT that will transform an XML document that I have to a CSV file. Here's a sample of the XML:

我正在尝试编写一个 XSLT,它将我拥有的 XML 文档转换为 CSV 文件。这是 XML 的示例:

<?xml version="1.0" encoding="utf-8"?>
<Import>
    <Users>
        <User ID="user_1" EmailAddress="[email protected]">
            <Contact FirstName="John" LastName="Doe" />
            <Address Street1="808 Elm St" City="Anywhere" State="NY" />
        </User>

        <User ID="user_2" EmailAddress="[email protected]">
            <Contact FirstName="Jane" LastName="Noone" />
            <Address Street1="123 Some Rd" City="Anywhere" State="NY" />
        </User>     
    </Users>
</Import>

What I want is an XSLT that will output like so:

我想要的是一个 XSLT,它将像这样输出:

John,Doe,808 Elm St,Anywhere,NY
Jane,Noone,123 Some Rd,Anywhere,NY

I think I have the C# code correct to initiate the transform, but just in case I don't, here's that code as well:

我想我有正确的 C# 代码来启动转换,但以防万一,这里也是代码:

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

namespace UserTransform
{
    class Program
    {
        static void Main(string[] args)
        {
            string oldXML = ConfigurationSettings.AppSettings["XMLToBeTransformed"];
            string xsltLocation = ConfigurationSettings.AppSettings["XSLTfile"];
            string newCSV = ConfigurationSettings.AppSettings["NewCSVLocation"];

            XslCompiledTransform transform = new XslCompiledTransform();
            transform.Load(xsltLocation);
            transform.Transform(oldXML, newCSV);
        }
    }
}

回答by Welbog

Create a template that matches all users, then pull out the information you need in the order you want:

创建一个匹配所有用户的模板,然后按照你想要的顺序拉出你需要的信息:

<xsl:template match="//User">
  <xsl:value-of select="Contact/@FirstName"/>,
  <xsl:value-of select="Contact/@LastName"/>,
  <!--etc-->
</xsl:template>

Obviously you'll need to make sure whitespace is handled the way you want it to be, with newlines in the right places. I'll leave this as an exercise to the reader.

显然,您需要确保按照您希望的方式处理空格,并在正确的位置使用换行符。我将把它作为练习留给读者。

回答by mad_typist

I always prefer to let the browser process the XML relieving the server to carry on with more demanding job. That said, here's a sample XSLT that should translate your XML and present it in a CSV format as shown above.

我总是更喜欢让浏览器处理 XML 以减轻服务器的负担来进行要求更高的工作。也就是说,这里有一个示例 XSLT,它可以翻译您的 XML 并以 CSV 格式显示它,如上所示。

Hope this sample code helps, if not, let me know.

希望此示例代码有所帮助,如果没有,请告诉我。

<xsl:stylesheet version="1.0">
    <xsl:template match="/">
        <table>
            <xsl:for-each select="//User">
                <tr>
                    <td>
                        <xsl:value-of select="conat('[', @ID, ']')"/>
                        <xsl:value-of select="','"/>
                        <xsl:value-of select="Contact/@FirstName"/>
                        <xsl:value-of select="','"/>
                        <xsl:value-of select="Contact/@LastName"/>
                        <xsl:value-of select="','"/>
                        <xsl:value-of select="Address/@Street1"/>
                        <xsl:value-of select="','"/>
                        <xsl:value-of select="Address/@City"/>
                        <xsl:value-of select="','"/>
                        <xsl:value-of select="Address/@State"/>
                    </td>
                </tr>
             </xsl:for-each>
        </table>
     </xsl:template>
</xsl:stylesheet>

回答by Andrew G. Johnson

In my experience I've always used XSLT on the client side of things not server side which is what you seem to be attempting with C#

根据我的经验,我一直在客户端而不是服务器端使用 XSLT,这似乎是您尝试使用 C#