A Beginner's Question
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
<?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 ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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:
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
while the following works fine -
Cheers,
Dan
William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
<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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
<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
Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Happy holidays,
Dan
William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
| Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too. The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |






