views:

55

answers:

2

I've written a simple XML Document that I am trying to transform with an XSLT file, but I get no results when I run the code. Here is my XML document:

<?xml version="1.0" encoding="utf-8" ?>
<Employee xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns="XSLT_MVC.Controllers">
  <ID>42</ID>
  <Name>Russ</Name>
</Employee>

And here is the XSLT file:

<?xml version="1.0" encoding="utf-8"?>
   <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
   xmlns:ex="XSLT_MVC.Controllers" >
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="/">
    <xsl:copy>
      <xsl:value-of select="ex:Employee/Name"/>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

Here is the code (from a C# console app) I am trying to run to perform the transform:

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

namespace XSLT
{
class Program
{
    static void Main(string[] args)
    {
        Transform();
    }

    public static void Transform()
    {
        XPathDocument myXPathDoc = new XPathDocument(@"docs\sampledoc.xml");
        XslTransform myXslTrans = new XslTransform();
        myXslTrans.Load(@"docs\new.xslt");

        XmlTextWriter myWriter = new XmlTextWriter(
            "results.html", null);

        myXslTrans.Transform(myXPathDoc, null, myWriter);

        myWriter.Close();
    }
}
}

When I run the code I get a blank html file. I think I may have problems with the namespaces, but am not sure. Can anyone help with this?

A: 

Would you just want to match on Employee? And then select name from that? Also, I've noticed your XSLT specifies an output of XML rather than HTML.

Chris Aldrich
Chris, Thanks for the help. I changed the XSLT to match on Employee, and changed it to out HTML, and it dumps both of the properties out to the HTML file like this: 42Russ. If I try to do a select like this: <xsl:value-of select="Name"/> after the match="Employee", it doesn't seem to process that line.
Russ Clark
A: 

The solution to this problem turned out to be adding namespace to both elems: select="ex:Employee/ex:Name", as Steven Majewski suggested in his comment above. He hasn't posted this as an answer, so I am posting it.

Russ Clark