2

I have this xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:cf="http://AAA"
                xmlns="http://AAA"
                exclude-result-prefixes="cf">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/cf:Content">
    <html>
      <head>
        <title>AAA</title>
      </head>
      <body>
        Hello everybody
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

this xml:

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet href="myxsl.xslt" type="text/xsl"?>
<cf:Content xmlns:cf="http://AAA"
            xmlns="http://AAA">

  Hello.

</cf:Content>

The namespace referenced by the xml is an xsd of mine (validation is correct).

Well, is I open the xml file with a browser, the xslt works.

Now, I have this code:

string xml = "THE SAME XML OF THE XML FILE";

XslCompiledTransform transform = new XslCompiledTransform();
using (XmlReader xr = 
      XmlReader.Create("myxsl.xslt")) { 
   transform.Load(xr); 
}

try {
   using (StringWriter sw = new StringWriter())
   using (StringReader sr = new StringReader(xml))
   using (XmlReader xr = XmlReader.Create(sr)) {

      transform.Transform(xr, new XsltArgumentList(), sw);
      string html = sw.ToString();
      this.Preview_Literal.Text = html;

   }
} catch (Exception ex) {
throw ex;
}

Of course it get an exception:

Error: Data at the root level is invalid. Line 1, position 1. - Type: System.Xml.XmlException

What is the problem?

5
  • 2
    Does it work if you remove the <?xml-stylesheet ... line? Commented Jul 2, 2011 at 10:25
  • Which statement exactly gives you the exception? Commented Jul 2, 2011 at 10:56
  • Some comments: if you want your stylesheet to create HTML output then using <xsl:output method="html" .../> instead of method="xml" is the right approach. And if you want to create HTML elements then remove the xmlns="http://AAA" from the xsl:stylesheet as HTML elements don't belong in that namespace. None of that explains the error you get but I suspect that is not caused by XSLT at all, rather simply happening when the input markup is parsed as XML. Can you post a stack trace? Where/how is the xml variable populated? Commented Jul 2, 2011 at 11:43
  • @Martin: You are right, removing namespaces was the irght choice, now it works. Thankyou :) Make that your answer, I'll check it as this question answer :) Thanks again Commented Jul 2, 2011 at 12:34
  • @martin: perhaps you'd like to answer? Commented Feb 25, 2013 at 12:01

1 Answer 1

1

Martin's answer:

remove the default namespace, xmlns="http://AAA", from the xsl:stylesheet as HTML elements don't belong in that namespace. For example, <head> is actually <cf:head> by default.

Sign up to request clarification or add additional context in comments.

4 Comments

Question: if Martin has answered it already, then why did you answer it again?
@John, since I found the question in the "unanswered" section. Since it had an answer, and a good one, I figured it should be "answered". Nothing more.
Why not let Martin answer the question?
If you check the dates - Martin commented the answer in 2011. I found this question at 2012. I figured he is not coming back to answer it. John, I can assure you I placed an "answer" just for the protocol - to mark this question answered.

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.