0

I have an XML file - for testing:

<?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>

In the final file I will have many entries with the same "Week". So I followed a tutorial and am just testing at this stage. I came up with this:

<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>

I wrote a C# console application that does this:

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");
        }
    }
}

When I run it my output xml is empty. What have I overlooked? It should have written something out with the names grouped by week.

2
  • 1
    Ok. Where is you XMI that you are starting with? You have an xml and a xsd schema. I'm totally confused. Commented Jan 17, 2018 at 22:52
  • I am taking the first xml with the XSL to make a new xml. Eventually the new xml will have some othe stuff in it. I just wanted to try to get it working first. I am starting with the xml listed. Processing it with the XSL. I need to end up you see with a xml that has all items grouped in a dare node but I not got that far. Commented Jan 17, 2018 at 23:19

1 Answer 1

1

The first part is real simple using 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; }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

So you are basically suggesting that I don’t try to do a transform but programmatically read the XML and write a new. Yes that is simple. 😀
At least for the reading. You can't tag the same class for reading and writing if the XML formats are different between read and write.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.