jahuer1
jahuer1

Reputation: 387

sort problem with <xsl:sort ...> in .NET 4

I have a problem when I compile my program with .NET 4 as target framework. The sorting with does not work.

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

namespace ExampleSortProblemServer
{
  class Program
  {
    static void Main(string[] args)
    {
        // argument is the filename
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(args[0]);

        XslCompiledTransform xslt = new XslCompiledTransform();
        XmlResourceResolver resolver = new XmlResourceResolver(typeof (IReport));
        xslt.Load(args[1]);

        String foStr = CreateFO(xmlDocument, xslt, resolver);

        Console.WriteLine(foStr);
    }

    public static string CreateFO(XmlNode doc, XslCompiledTransform xslt, XmlResolver resolver)
    {
        string fo;

        using (var stringReader = new StringReader(doc.OuterXml))
        {
            using (var xmlTextReader = new XmlTextReader(stringReader))
            {
                using (var stringWriter = new StringWriter())
                {
                    using (var xmlTextWriter = new XmlTextWriter(stringWriter))
                    {
                        try
                        {
                            xslt.Transform(xmlTextReader, null, xmlTextWriter, resolver);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                            throw;
                        }
                        stringWriter.Flush();
                        fo = stringWriter.ToString();
                    }
                }
            }
        }
        return fo;
    }
  }
}

The Data is "Daten.xml":

<daten>
<employees>
    <name>Knight</name>
    <name>Cook</name>
    <name>Superman</name>
    <name>Yoda</name>
    <name>Albright</name>
</employees>
</daten>

The Stylesheet is "Daten.xsl":

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:output method="xml" encoding="utf-8" indent="yes"/>

<xsl:template match="/">
    <xsl:variable name="daten" select="*[local-name()='daten']"/>
    <xsl:apply-templates select="$daten">
        <xsl:sort select="normalize-space(*[local-name()='employees']/*[local-name()='name']" order="ascending"/>
    </xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>

You can call the program with: ExampleSortProblemServer.exe Daten.xml Daten.xsl

The expected order should be:

Albright Cook Knight Superman Yoda

Any idea what is incorrect?

Upvotes: 1

Views: 309

Answers (2)

Daniel Haley
Daniel Haley

Reputation: 52858

I think this is because you are trying to sort daten and not the names.

For example, this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/">
    <xsl:apply-templates select="*[local-name()='daten']/*[local-name()='employees']/*[local-name()='name']">
      <xsl:sort order="ascending"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="name">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>

</xsl:stylesheet>

with your input "Daten.xml":

<daten>
  <employees>
    <name>Knight</name>
    <name>Cook</name>
    <name>Superman</name>
    <name>Yoda</name>
    <name>Albright</name>
  </employees>
</daten>

produces this output:

Albright Cook Knight Superman Yoda

EDIT

The stylesheet only needs a little updating to handle the XML input you added as an answer.

This input:

<daten>
  <employees>
    <employee>
      <name>Knight</name>
      <forename>Peter</forename>
    </employee>
    <employee>
      <name>Knight</name>
      <forename>Gilbert</forename>
    </employee>     
    <employee>
      <name>Cook</name>
      <forename>Thomas</forename>
    </employee>
    <employee>
      <name>Cook</name>
      <forename>Charles</forename>
    </employee>     
    <employee>
      <name>Superman</name>
      <forename>Kal-El</forename>
    </employee>
    <employee>
      <name>Yoda</name>
      <forename></forename>
    </employee>
    <employee>
      <name>Albright</name>
      <forename>Peter</forename>
    </employee>
    <employee>
      <name>Albright</name>
      <forename>Charles</forename>
    </employee> 
    <employee>
      <name>Albright</name>
      <forename>Mark</forename>
    </employee>     
  </employees>
</daten>

with the updated stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/">
    <xsl:apply-templates select="*[local-name()='daten']/*[local-name()='employees']/*[local-name()='employee']">
      <xsl:sort order="ascending" select="name"/>
      <xsl:sort order="ascending" select="forename"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="name">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>

  <xsl:template match="forename">
    <xsl:apply-templates/>
    <xsl:text>&#xA;</xsl:text>
  </xsl:template>

</xsl:stylesheet>

produces the output you wanted:

Albright Charles
Albright Mark
Albright Peter
Cook Charles
Cook Thomas
Knight Gilbert
Knight Peter
Superman Kal-El
Yoda 

Upvotes: 2

jahuer1
jahuer1

Reputation: 387

Here an extended xml file:

<daten>
<employees>
    <employee>
        <name>Knight</name>
        <forename>Peter</forename>
    </employee>
    <employee>
        <name>Knight</name>
        <forename>Gilbert</forename>
    </employee>     
    <employee>
        <name>Cook</name>
        <forename>Thomas</forename>
    </employee>
    <employee>
        <name>Cook</name>
        <forename>Charles</forename>
    </employee>     
    <employee>
        <name>Superman</name>
        <forename>Kal-El</forename>
    </employee>
    <employee>
        <name>Yoda</name>
        <forename></forename>
    </employee>
    <employee>
        <name>Albright</name>
        <forename>Peter</forename>
    </employee>
    <employee>
        <name>Albright</name>
        <forename>Charles</forename>
    </employee> 
    <employee>
        <name>Albright</name>
        <forename>Mark</forename>
    </employee>     
</employees>
</daten>

The result should be:

Albright Charles
Albright Mark
Albright Peter
Cook Charles
Cook Thomas
Knight Gilbert
Knight Peter
Superman Kal-El
Yoda

Upvotes: 0

Related Questions