This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes A Beginner's Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "A Beginner Watch "A Beginner New topic
Author

A Beginner's Question

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I have a XML file and I am going to write a XSLT; say, xxx.xsl, to render the XML file. This is a rather straightfoward XML file. To my undertanding, if I add a reference to the xxx.xsl to the XML document
<?xml-stylesheet type="text/xsl" href="xxx.xsl"?>
the browser will transform the XML document into HTML. What bothers me is the second line of the XML file:

Q1. What is <rdf:RDF> </rdf:RDF>? Does it make the XML transformation task different?
Q2. When the XML file does not have a reference to the XSL file, it indicates that an XML file on the server could be transformed using many different languages. Do you advise to do the transformation in Java? If the answer is "yes", is there standardized Java coding to perform such a task?

[ December 13, 2002: Message edited by: JiaPei Jen ]
[ December 13, 2002: Message edited by: JiaPei Jen ]
[ December 13, 2002: Message edited by: JiaPei Jen ]
[ December 13, 2002: Message edited by: JiaPei Jen ]
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1164
Please read here http://www.w3.org/RDF/ about RDF.
This line of code defines a namespace prefix 'rdf', which is not used in this document. It also defines the default namespace to be http://purl.org/rss/1.0.
It shouldn't impact your XSLT transformation.
I just tried the following stylesheet with your XML data.

Cheers,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Dan, thanks for your answer. I have read many of your answers to questions posted at the "XML Certification" forum. Thank you for sparing time to answer questions.
Refering to the XML file I posted, the XML file does not have a reference to the XSL file. It indicates that an XML file could be transformed using many different languages.
In fact, I was asked to transform this XML into HTML using Java. Do you advise to do the transformation using Java? Is there standardized Java coding to perform such a task?
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
My XSLT (stylesheet) works fine to transform the XML file "without the RDF tag" to HTML.
If I keep the RDF tag in, my XSLT cannot render the XML to HTML.
Did you really try the XML with the RDF tag in? How come it worked for you but not for me?
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I have written a XSLT to transform the posted XML file. This XSLT only works if I leave out the
opening and closing <rdf:RDF> tags:

and the XSLT does not work if those <rdf:RDF> tags are in. Could anybody point out the problem? My XSLT code can be found below:
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1164
JiaPei,
Thanks for posting this interesting question!
You asked -
In fact, I was asked to transform this XML into HTML using Java. Do you advise to do the transformation using Java?

I think XSLT is built exactly for this type of transformations. But it can also be done with Java. If you are in a Java shop, which doesn't use XSLT, it even makes sense ;-)
If I keep the RDF tag in, my XSLT cannot render the XML to HTML.

Which XSLT processor do you use?
I tried my little XSLT stylesheet with XML Spy 4.4, which is quite lenient at times.
Actually, my stylesheet should not work, unless we change it to -

I added xmlns=http://purl.org/rss/1.0/ and dropped the <br/> tag, because <br/> doesn't belong to the http://purl.org/rss/1.0/ namespace.
This technique will work as long as the input and output vocabularies are the same.
Your case, however, is different since the input vocabulary is XML while the output vocabulary is HTML.
In this case my template might look like -

I hope it helps,
Dan
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1164
JiaPei,
Xalan-j 2.3.1 and XML Spy 4.4 behave correctly in your case - They don't produce the desired output when the rdf tag is present in the input XML.
Cheers,
Dan
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1164
Does anybody know why the following stylesheet fails -

while the following works fine -

Cheers,
Dan
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
The problem seems to be in
<xsl:template match="tst:/channel/description">
element -- this is incorrect syntax. It should be
<xsl:template match="tst:channel/tst:description">
[ December 17, 2002: Message edited by: Mapraputa Is ]

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Map is pointing to the right direction. Would you give some further advice? While

did output something to the console, the code below failed to pick up the value of the selected elements (did not complain about the code, but the code returns nothing):

[ December 17, 2002: Message edited by: JiaPei Jen ]
[ December 17, 2002: Message edited by: JiaPei Jen ]
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
This is because of differences how XSLT processor deals with expressions in
<xsl:template match="...">
and
<xsl:value-of select="..."/>
XSLT processor starts its work from the root element of XML document and then walk down the tree, each time looking at what templates can be applied. So if you have <xsl:template match="title"> then this temlate will be applied every time there is an element named "title", regardless of where in an XML documents it happened. And how many title elements are there, so many times the template will work.
When template is "applied", then XSLT instructions inside it, included <xsl:value-of select="..."/>, are executed. An expression in "select" clause of <xsl:value-of> is calculated as relative to the expression in "match" clause of the template. So if you have
<xsl:template match="title">
....
<xsl:value-of select="subtitle"/>
then this <xsl:value-of> will get all <subtitle> elements that are children of the current <title> element.
In your situation, you have
<xsl:template match="/">
and this means "the root of the XML document"
Now when XSLT processor see
<xsl:value-of select="purl:channel/purl:description"/>
it tries to get you channel/description elements, but there is one more element between the root and <channel> - rdf:RDF! That's why the processor cannot "find" anyhing.
You can either write
<xsl:value-of select="rdf:RDF/purl:channel/purl:description"/>
every time, or perhaps it's easier to change yout template "match" clause to "rdf:RDF"
<xsl:template match="rdf:RDF">
and this rdf namespace must be declared:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:purl="http://purl.org/rss/1.0/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
Then the whole stylesheet will be
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1164
Thanks a lot Mapraputa for clarifying the issue!
Happy holidays,
Dan
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Thanks a lot, Map, for your explanation.
After reading your post, I went back to review what I have read from the books. You have helped me to:
1. know the difference between <xsl:template match="...">
and
<xsl:value-of select="..."/>
2. pay attention to namespace
It is a good exercise.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A Beginner's Question
 
Similar Threads
Namepace problem with Xalan/XSLT
schema and namespaces
JSTL Date Range Question
DTD and validation - RSS 0.91
Java XML DOM Parsing