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.