• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A Beginner's Question

 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anybody know why the following stylesheet fails -

while the following works fine -

Cheers,
Dan
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Mapraputa for clarifying the issue!
Happy holidays,
Dan
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic