[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Sorting XML to XML using XSLT from within Windows Form.

Posted on 2005-05-10
17
Medium Priority
?
264 Views
Last Modified: 2013-11-19
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???
0
Comment
Question by:mafendee
  • 7
  • 4
  • 3
  • +1
17 Comments
 
LVL 8

Expert Comment

by:anthonywjones66
ID: 13966439
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 13966537
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 13967709
That'll give you the transformed data within the same explorer.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 10

Expert Comment

by:BobSiemens
ID: 13969464
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 13969490
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
 
LVL 10

Expert Comment

by:BobSiemens
ID: 13969763
<<<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
 
LVL 23

Expert Comment

by:b1xml2
ID: 13969797
kewl
0
 

Author Comment

by:mafendee
ID: 13975689
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 13976055
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
 
LVL 10

Expert Comment

by:BobSiemens
ID: 13978644
<<<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
 

Author Comment

by:mafendee
ID: 13984492
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 13985414
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
 
LVL 8

Expert Comment

by:anthonywjones66
ID: 13985566
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
 

Author Comment

by:mafendee
ID: 13993119
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
 
LVL 8

Accepted Solution

by:
anthonywjones66 earned 1000 total points
ID: 13993773
>>
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
 

Author Comment

by:mafendee
ID: 14057915
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 14058198
yep
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question