【问题标题】:Trying to transform XMl to XML using XSL with C# Console App尝试使用带有 C# 控制台应用程序的 XSL 将 XMl 转换为 XML
【发布时间】:2018-01-17 22:47:19
【问题描述】:

我有一个 XML 文件 - 用于测试:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<StudentHistory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Item>
        <Week>1988-05-12</Week>
        <Name>A</Name>
        <Counsel>1</Counsel>
        <Completed>1</Completed>
        <Description>XX</Description>
    </Item>
    <Item>
        <Week>1988-05-12</Week>
        <Name>AA</Name>
        <Counsel>2</Counsel>
        <Completed>1</Completed>
        <Description>XX</Description>
    </Item>
    <Item>
        <Week>1988-05-13</Week>
        <Name>B</Name>
        <Counsel>2</Counsel>
        <Completed>1</Completed>
        <Description>XX</Description>
    </Item>
    <Item>
        <Week>1988-05-14</Week>
        <Name>C</Name>
        <Counsel>3</Counsel>
        <Completed>1</Completed>
        <Description>XX</Description>
    </Item>
    <Item>
        <Week>1988-05-15</Week>
        <Name>D</Name>
        <Counsel>4</Counsel>
        <Completed>1</Completed>
        <Description>XX</Description>
    </Item>
    <Item>
        <Week>1988-05-16</Week>
        <Name>E</Name>
        <Counsel>5</Counsel>
        <Completed>1</Completed>
        <Description>XX</Description>
    </Item>
    <Item>
        <Week>1988-05-17</Week>
        <Name>F</Name>
        <Counsel>6</Counsel>
        <Completed>1</Completed>
        <Description>XX</Description>
    </Item>
    <Item>
        <Week>1988-05-18</Week>
        <Name>G</Name>
        <Counsel>7</Counsel>
        <Completed>0</Completed>
        <Description>XX</Description>
    </Item>
    <Item>
        <Week>2018-01-16</Week>
        <Name>H</Name>
        <Counsel>-1</Counsel>
        <Completed>1</Completed>
        <Description>XX</Description>
    </Item>
</StudentHistory>

在最终文件中,我将有许多具有相同“周”的条目。所以我遵循了一个教程,只是在这个阶段进行测试。我想出了这个:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="items-by-week" match="Item" use="Week" />
  <xsl:template match="StudentHistory/Item">
    <xsl:for-each select="Item[count(. | key('items-by-week', Week)[1]) = 1]">
      <xsl:sort select="Week" />
      <xsl:value-of select="Week" />
      <xsl:for-each select="key('items-by-week', Week)">
        <xsl:sort select="Name" />
        <xsl:value-of select="Name" /> (<xsl:value-of select="Counsel" />)
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

我编写了一个 C# 控制台应用程序来执行此操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Xsl;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var myXslTrans = new XslCompiledTransform();
            myXslTrans.Load(@"d:\TestTransform2.xsl");
            myXslTrans.Transform(@"d:\Test-Students2.xml", @"d:\Test-Students2-Final.xml");
        }
    }
}

当我运行它时,我的输出 xml 是空的。我忽略了什么?它应该用按周分组的名称写出一些东西。

【问题讨论】:

  • 好的。你从哪里开始的 XMI?您有一个 xml 和一个 xsd 架构。我完全糊涂了。
  • 我正在使用 XSL 制作第一个 xml 来制作一个新的 xml。最终,新的 xml 中会包含一些其他的东西。我只是想先让它工作。我从列出的 xml 开始。使用 XSL 处理它。我需要最终让您看到一个 xml,该 xml 将所有项目分组在一个敢节点中,但我还没有走到那一步。

标签: c# xml xslt-1.0


【解决方案1】:

第一部分使用 xml linq 非常简单:

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XNamespace ns = doc.Root.GetDefaultNamespace();

            List<Student> students = doc.Descendants(ns + "Item").Select(x => new Student() {
                week = (DateTime)x.Element(ns + "Week"),
                name = (string)x.Element(ns + "Name"),
                counsel = (int)x.Element(ns + "Counsel"),
                completed = (int)x.Element(ns + "Completed") == 1 ? true : false,
                description = (string)x.Element(ns + "Description")
            }).ToList();

            var weeks = students.GroupBy(x => x.week).ToList();
        }
    }
    public class Student
    {
        public DateTime week { get; set;}
        public string name { get; set; }
        public int counsel { get; set; }
        public Boolean completed { get; set; }
        public string description { get; set; }
    }
}

【讨论】:

  • 所以您基本上是在建议我不要尝试进行转换,而是以编程方式读取 XML 并编写新的。是的,这很简单。 ?
  • 至少是为了阅读。如果读写的 XML 格式不同,则不能标记同一个类进行读写。
猜你喜欢
  • 2012-02-01
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 2019-03-22
  • 2022-01-07
  • 1970-01-01
  • 2014-09-10
  • 1970-01-01
相关资源
最近更新 更多