Sorting XML to XML using XSLT from within Windows Form.

Hi all,

As the above title suggest, I am writing a Windows .NET application which uses a list box to read (bind) an XML transformed (say XFileB.xml) file. The original XML (say XFileA.xml) file contains element (NAME) that need to be sorted.

Currently I am not yet coding the C# part into the application to read, transform and bind the file. I am just testing the output using my MS IE browser version 6 under XP Pro.


Original XML File (XFileA.xml)

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="OrgYos.xsl"?>
<School>
  <Year>
    <Name>Form 1</Name>
  </Year>
  <Year>
    <Name>Form 3</Name>
  </Year>
  <Year
    <Name>Standard 5</Name>
  </Year>
  <Year
    <Name>Standard 3</Name>
  </Year>
  <Year>
    <Name>Standard 6</Name>
  </Year>
</School>

XSL stylesheet file (OrgYos.xsl)

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

<xsl:output method="xml" omit-xml-declaration="no" media-type="text/xml"/>

<xsl:template match="School">
    <xsl:copy>
      <xsl:apply-templates>
         <xsl:sort select="Name"/>
         </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

The problem is, when I click on XFileA.xml, the IE6.0 browser will only output the following single line.

Form 1Form 3Standard 5Standard 3Standard 6


I would expect to see the following output (where Standard 3 and Standard 5 re-ordered).

Transformed XML File (XFileB.xml)

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="OrgYos.xsl"?>
<School>
  <Year>
    <Name>Form 1</Name>
  </Year>
  <Year>
    <Name>Form 3</Name>
  </Year>
  <Year
    <Name>Standard 3</Name>
  </Year>
  <Year
    <Name>Standard 5</Name>
  </Year>
  <Year>
    <Name>Standard 6</Name>
  </Year>
</School>

What happen? Is there anyone who can help me out with this???
mafendeeAsked:
Who is Participating?
 
anthonywjones66Commented:
>>
So, if the header "<?xml version="1.0" encoding="utf-8"?>"
is exactly the same in those two files (Listing A and Listing C), why can't IE show
<<

Take a look at this XML:-

<?xml version="1.0" encoding="UTF-8" ?>
<html>
      <body>
            <b>Hello World</b>
      </body>
</html>

This is perfectly valid XML.  However if you were to save it as Test.htm and open it in IE it will render as HTML.  The point here is the presence of the <?xml ...> tag does not necessarily mean that the content is not html. Now rename the file as Text.xml and open it.  Now you see it transformed with the built-in XML to HTML transform.

The assumption made by IE is that a file with the extension .XML is not immediately renderable (is that a real word?) and needs to be transformed to HTML first.  IE looks for a <?xml-stylesheet ...> tag to find a reference to an XSL that will do this.  That is the purpose of this tag to give IE a reference to an XSL which will output HTML.  Hence IE is correct in assuming the output of the transform will be HTML.  The fact that the XSL outputs a valid XML complete with <?xml...> tag is in no way indicator that the result is not renderable HTML.  Hence IE simply attempts to directly render the output of the transform.

When there is no <?xml-stylesheet...> tag IE simply implies one:-

<?xml-stylesheet type="text/xsl" href="res://msxml.dll/defaultss.xml" ?>

then proceeds extactly as above.

Hope this is clear.

Anthony.
0
 
anthonywjones66Commented:
It is working ok.  It's just that IE only perform it's own transform of XML to HTML if the XML file doesn't already contain a stylesheet instruction.  Otherwise it just renders the output of the stylesheet as if it were HTML.

Anthony.
0
 
b1xml2Commented:
1. create an empty html page
2. copy and paste the following:
3. change the xml and xslt locations accordingly

<script langauge="javascript">
var oXml = new ActiveXObject("Msxml2.DOMDocument");
var oXslt = new ActiveXObject("Msxml2.DOMDocument");
var oResult = new ActiveXObject("Msxml2.DOMDocument");
var oStylesheet = new ActiveXObject("Msxml2.DOMDocument");
oXml.async = oXslt.async = oResult.async = false;
oStylesheet.async = true;
oStylesheet.load("res://msxml3.dll/defaultss.xsl");
oXml.load("May/data3.xml");
oXslt.load("May/data3.xslt");
window.onload = init;
function init()
{
      
      if (oStylesheet.readyState != "4")
      {
            window.setTimeout("init();",200);
      }
      else
      {
            oXml.transformNodeToObject(oXslt,oResult);
            document.write(oResult.transformNode(oStylesheet));
      }
}

</script>

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
b1xml2Commented:
That'll give you the transformed data within the same explorer.
0
 
BobSiemensCommented:
No, no.  The problem is that it is applying the default transformation rules which says, essentially, print out any text within tags.

I'm not sure what you want to print, but that is what you are telling the default rules to print.

What did you want the output to look like?
0
 
b1xml2Commented:
Bob, that's for him to see what is the transformation like. It's not for his app.
>>
I am just testing the output using my MS IE browser version 6 under XP Pro.
<<

So it's not NO, It's yes! This allows him to easily see what the output is without resorting to buying any software product.
0
 
BobSiemensCommented:
<<<So it's not NO, It's yes! This allows him to easily see what the output is without resorting to buying any software product.>>>

I understood that.  I'm saying his stylesheet is doing just what he asked.
0
 
b1xml2Commented:
kewl
0
 
mafendeeAuthor Commented:
Now, I am confused and I am not sure if I understood you all correct. Let me re-phrase my question. I have an XML file called XFileA.xml (as above). I would like to have it transformed into XFileB.xml (as above) so I can use later for my application, but this is not important for now. I have used  OrgYoS.xsl to get this done.

First question: Do you think this file OrgYoS.xsl does what I exactly want, to transform XFileA.xml to XFileB.xml? If not, please change it for me.

Second question: Say if OrgYoS.xsl is right. Why can't I see the output of the transformed file within IE 6.0 the way I have seen the XFileA.xml. (With expandable icon(+/-) and so on)

Still waiting for more explanation.
0
 
b1xml2Commented:
mafendee,
if you use the xml-stylesheet processing instruction to perform your transformation, IE will ignore the xsl:output element in your XSLT and output the transformation as HTML. This is by design. So from a Unit Testing perspective, you can't rely on the xml-stylesheet processing instruction to show you the transformed XML.

in such a case, you can write your own small C# console to output the results to a file and then view it from IE. That can be cumbersome, so you can used the HTML/Javascript code and just change the locations of the files to where they actually reside. By opening the HTML file, you get to see the transformed Xml in a nice UI, styled by the default stylesheet that is used whenever you open any XML documents.

Your XSLT does what it says it does. It's just that MSIE doesn't play by the rules.
0
 
BobSiemensCommented:
<<<Your XSLT does what it says it does. It's just that MSIE doesn't play by the rules.>>>

Sorry, yes.  This is the correct answer.
0
 
mafendeeAuthor Commented:
OK... and that means i can feed XFileB.xml to my C#-written windows application, right?
Still, I would like to know what exactly happen there. My thinking says, I have XFileA.xml which is beautifully shown within MS IE. But when it comes to transformed file XFileB.xml (with the same heading), IE shows different output.

I thought the XML parser will process both files and output the files with the same heading content to the IE.

<?xml version="1.0" encoding="utf-8"?>

But then why didn't it shown in XML format? Still in need of explanation.
0
 
b1xml2Commented:
The XFileA.xml is as you say beautifully shown within MSIE care of the DefaultSS.Xsl which you can see by typing: res://msxml3.dll/defaultss.xsl

But when you use the xml-stylesheet processing instruction, there is NO XFileB.xml.

It is content in memory that is rendered by the Xml engine to the UI layer of MSIE. All content will be rendered as HTML regardless using the xml-stylesheet processing instruction.
0
 
anthonywjones66Commented:
From you original question the content of XFileB.xml is:-

Transformed XML File (XFileB.xml)

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="OrgYos.xsl"?>
<School>
  <Year>
    <Name>Form 1</Name>
  </Year>
  <Year>
    <Name>Form 3</Name>
  </Year>
  <Year
    <Name>Standard 3</Name>
  </Year>
  <Year
    <Name>Standard 5</Name>
  </Year>
  <Year>
    <Name>Standard 6</Name>
  </Year>
</School>

Is this truely the case.  The transform should not also be including the <?xsl-stylesheet ...?>  line.  It certainly doesn't when I try it.  Remove it from XFileB.xml and you will find it renders fine.  Also with this same xml-stylesheet being present in XFileA.xml you should also just see text and not a HTML transform of the XML.

 As I understand it you were trying to add this line to XFileB.xml in order to get IE to automatically perform the transform for you.

As I and others have already stated.  What you normally see when you open an XML file in IE is it's own internal transform the the XML into a HTML output that lets you browse the file.  When the xml-stylesheet Processing instruction is present it doesn't do this.  It assumes the the stylesheet refered to will do a special transform to HTML so it simply attempts to render the output of the transform as if it were raw HTML.  It doesn't intelligently realise the final output still isn't HTML.


Get this tool:-

http://www.microsoft.com/downloads/details.aspx?FamilyId=D23C1D2C-1571-4D61-BDA8-ADF9F6849DF9&displaylang=en

This adds a 'View XSL Output' function to the context menu in IE.  If you open a raw XML file (no stylesheet instruction) and do a view source you just see the original XML in notepad.  If you open 'View XSL Output'  you see the that what IE is rendering is HTML.  IF now you add a xml-stylesheet to the raw XML  and open 'View XSL output' you will see exactly what your stylesheet has done.  It this that IE attempts to render, if it contains no HTML recognisable tags the tags are ignored and all you see is the textual content.

Anthony.
0
 
mafendeeAuthor Commented:
b1xml2,

****Listing A*****
<?xml version="1.0" encoding="utf-8"?>
<School>
  <Year>
    <Name>Form 1</Name>
  </Year>
  <Year>
    <Name>Form 3</Name>
  </Year>
  <Year
    <Name>Standard 5</Name>
  </Year>
  <Year
    <Name>Standard 3</Name>
  </Year>
  <Year>
    <Name>Standard 6</Name>
  </Year>
</School>

should show something like this within IE

****Listing B*****
<?xml version="1.0" encoding="utf-8"?>
- <Organisation>
   - <Year>
       <Name>Form 3</Name>
   + <Year>
   - <Year>
       <Name>Standard 5</Name>
   - <Year>
       <Name>Standard 3</Name>
   + <Year>
  </Organisation>

But then, when we add this line
<?xml-stylesheet type="text/xsl" href="OrgYos.xsl"?>
as a second line to XFileA.xml, this XFileA.xml will be transformed. I think I am right to this far.

I THINK, the output of transformed file, which is XFileB.xml looks like this

****Listing C*****
<?xml version="1.0" encoding="utf-8"?>
<School>
  <Year>
    <Name>Form 1</Name>
  </Year>
  <Year>
    <Name>Form 3</Name>
  </Year>
  <Year
    <Name>Standard 3</Name>
  </Year>
  <Year
    <Name>Standard 5</Name>
  </Year>
  <Year>
    <Name>Standard 6</Name>
  </Year>
</School>

Which is absolutely the same as the original XFileA.xml which is without
<?xml-stylesheet type="text/xsl" href="OrgYos.xsl"?>
but the sorted one. (Standard 3 and Standard 5). Again, I THINK, this is what will IE receive from the XML parser.

So, if the header "<?xml version="1.0" encoding="utf-8"?>"
is exactly the same in those two files (Listing A and Listing C), why can't IE show

<?xml version="1.0" encoding="utf-8"?>
- <Organisation>
   - <Year>
       <Name>Form 3</Name>
   + <Year>
   - <Year>
       <Name>Standard 3</Name>
   - <Year>
       <Name>Standard 5</Name>
   + <Year>
  </Organisation>

and NOT
  Form 1Form 3Standard 3Standard 5Standard 6

I know that I hardly able to understand you all. Actually I am quite new to XML, just started a few weeks ago.

So, Please expain step by step...
0
 
mafendeeAuthor Commented:
That means, the transformed file (XFileB.xml) is treated as .HTML and not .XML by IE browser. So that is why it was rendered differently. Am I right?
0
 
b1xml2Commented:
yep
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.