wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes XML / XSL problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML / XSL problem" Watch "XML / XSL problem" New topic
Author

XML / XSL problem

Neha Purohit
Greenhorn

Joined: Sep 05, 2003
Posts: 5
Hello All,
I have just started learning XML. After reading initial chapters from the book, I tried writing a program. Below is the XML file I wrote:
--------------------------------Class.xml-----------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="ClassStudents.xsl"?>
<Class>
<Student>
<Name>Student1</Name>
<FavSubject>English</FavSubject>
</Student>

<Student>
<Name>Student2</Name>
<FavSubject>Hindi</FavSubject>
</Student>
</Class>
--------------------------------------------------------------------------
After this I wrote 'ClassStudents.xsl' file as below:
===============================ClassStudents.xsl===========================
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">
<xsl:template match="/">
<HTML>
<TITLE> My First XML Program</TITLE>
<BODY BGCOLOR='LIGHTGREY'>
<H1> Favourite Subjects of Students </H1>
<TABLE BORDER='1' BORDERCOLOR='BLUE' BGCOLOR='LIGHTBLUE'>
<TR>
<TH ALIGN="CENTER">NAME</TH>
<TH ALIGN="CENTER">FAVOURITE SUBJECT</TH>
</TR>
<xsl:for-each select="Class/Student">
<TR>
<TD><xsl:value-of select="Name"/></TD>
<TD><xsl:value-of select="FavSubject"/></TD>
</TR>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
===========================================================================
I stored both the files in the directory - C:\XMLPractice\
Now I opend IE 6.0+. In the Address field of IE, I entered the location of the xml file - C:\XMLPractice\Class.xml and pressed enter.
The page was displayed. However the table in the page only displayed the Header row -i.e. 'Name' and 'Favourite Subject'. The <xsl:for-each> part of xsl did not get executed and hence the data rows didn't appear in the table.
Can somebody please clarify me where is the problem?
Thanks in advance
Neha
[ September 05, 2003: Message edited by: Neha Purohit ]
[ September 05, 2003: Message edited by: Neha Purohit ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
It works with my IE 5.5 after I've fixed the namespace to "http://www.w3.org/1999/XSL/Transform" and added the missing closing tags for <xsl:template> and <xsl:stylesheet>. I can take a look at it when I get home (I've got IE 6.0 installed there).


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Neha Purohit
Greenhorn

Joined: Sep 05, 2003
Posts: 5
Thanks Lasse,
Yes its working now, the problem was with the URL I specified for xmlns:xsl, as you pointed out, it should be "http://www.w3.org/1999/XSL/Transform"> the 'XSL'and 'T' of Transform should be in Caps, which I had written in Small letters earlier.

Thanks a lot
Neha
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Glad to hear that (besides, I forgot to test it with IE 6.0...)
Neha Purohit
Greenhorn

Joined: Sep 05, 2003
Posts: 5
Hi Lasse,
Now I am trying to define a schema for my above xml file.I referred www.xml.com site to read about schemas.....the schema file I wrote is as below:
--------------------------------ClassStudents.xsd--------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Class">
<xs:complexType>
<xs:sequence>
<xs:element name="Student" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string">
<xs:element name="FavSubject" type="xs:string">
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
---------------------------------------------------------------------------
However I am now stuck on how to associate this schema file with my xml file.
The site doesn't seem to have covered this part.
Can you please help me into this!!
Thanks
Neha
[ September 08, 2003: Message edited by: Neha Purohit ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Here's an example I extracted from a IBM DeveloperWorks' tutorial:
Neha Purohit
Greenhorn

Joined: Sep 05, 2003
Posts: 5
Thanks Lasse,
I think I am troubling you a lot. In the Class.xml file, I copied the xmlns:xsi.... part to my <Class> element, replacing the .xsd file name with the name of the xsd file I am using. Then I added a <Title> Tag under <Student> tag and saved my Class.xml file. Below is how the file looks now:
---------------------------Class.xml --------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="ClassStudents.xsl"?>
<Class xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ClassStudentsSchema.xsd">
<Student>
<Name>Student1</Name>
<FavSubject>English</FavSubject>
<Title>title1</Title>
</Student>

<Student>
<Name>Student2</Name>
<FavSubject>Hindi</FavSubject>
</Student>
</Class>
---------------------------------------------------------------------
The files 'Class.xml','ClassStudents.xsl' and 'ClassStudentsSchema.xsd' are stored in C:\XMLPractice\ directory of my local PC.
Now I open my C:\XMLPractice\Class.xml in IE expecting an error as <Title> element is not defied in the schema. However I am not getting any error and the page is displaying the table ignoring the <Title> element.
Does using Schema needs a Web-server or what? Note that I am not using any Web server.

#####
I feel I should have entered this schema problem as a new Topic, however since I was using the same example, I thought this way it would be easier to explain the problem.
Sorry for the trouble Lasse and thanks for your help....
Neha
[ September 09, 2003: Message edited by: Neha Purohit ]
Gayathri Prasad
Ranch Hand

Joined: Jun 25, 2003
Posts: 116
Hi Neha,
Ya when I tried with IE6.0 I DID not get any error and got the output as I wrote the elements including the <title>. But when the same document is being validated by XML Spy it is giving me an error and stating me that <title> is an unexpected child which is very mu ch true. I think IE is not validating the XML file against the Schema.Neha try using some good XML editor like XML Spy.
Cheers,
Gaya3.
-------------------------------------------------------------------------
Life is a Process of Continuous Learning...
Neha Purohit
Greenhorn

Joined: Sep 05, 2003
Posts: 5
Thanks Gaya3,
Actually I don't have XML SPY or any other XML parser for that matter. And because of some constraints, I cann't download any such thing from my machine.
Why this doesn't work with IE, I mean why IE doesn't throw any error. I belive IE6.0 must be using an XML parser implementing the most recent XML specifications.
Can somebody let us know why this doesn't work with IE?
Thanks
Neha
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1850
Why this doesn't work with IE, I mean why IE doesn't throw any error.

Yes I tried it it behaves as said above. I am using IE 5.5
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1850
Strange
Netscape 7.1 gives me the following error
Error loading stylesheet: Parsing a XSLT stylesheet failed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XML / XSL problem