Xpath help again
Hi:
I have try to transform with this xml file with my stylesheet.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Help</title>
</head>
<body>
<table>
<tr>
<td>Demo</td>
<td>Hello</td>
</tr>
</table>
</body>
</html>
and my xsl stylesheet is :
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xsl: output version="1.0" indent="yes" encoding="UTF-8" omit-xml-declaration="no" method="xml"/>
<xsl:template match="/xhtml:html">
<Demo>
<Example>
<xsl:apply-templates/>
</Example>
</Demo>
</xsl:template>
<xsl:template match="text()"></xsl:template>
<xsl:template match="table/tr[starts-with(normalize-space(td/text()),'Demo')]">
<GREETING>
<xsl:value-of select="td[2]/text()"/>
</GREETING>
</xsl:template>
</xsl:stylesheet>
but my transformation totally fail. Is it that the table i must also specify with xhtml:table , xhtml:tr?, but when i run, it give me exception.
thanks
thanks
# 1 Re: Xpath help again
but my transformation totally fail.
Please don't say that, I have been telling time and time again to be specific. You could easily post the result you get. Which would make it much easier for me to figure out where it fails.
Is it that the table i must also specify with xhtml:table , xhtml:tr?
Yes, I thought we had already covered that once :), you ofcourse also need the namespace prefix on the td element. If you do it correctly this is all you need to make it work.
but when i run, it give me exception.
Again please don't say that, tell us what exception you are getting, and show us the stylesheet that causes the exception. I can only assume you made a syntax error somewhere, but I'am not going to help you unless you post what you are doing wrong.
One other thing, When you post code you can wrap the code in [code] tags.
This makes it much easier to read the code. Note that the end tag must contain /code instead of just the word code. Try clicking the code buttum above the text box on the 'Post New thread' or 'Reply to thread' page.
khp at 2007-11-10 3:27:30 >

# 2 Re: Xpath help again
Dear khp,
this is my xml code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Help</title>
</head>
<body>
<table class="help">
<tr>
<td>Demo</td>
<td>Hello</td>
</tr>
</table>
</body>
</html>
and my td[1] which is the Demo is actually change frequently so my xsl styleshteet is:
<xsl: output version="1.0" indent="yes" encoding="UTF-8" omit-xml-declaration="no" method="xml"/>
<xsl:template match="/xhtml:html">
<xsl:param name="title"/>
<Demo>
<Example>
<xsl:apply-templates/>
</Example>
</Demo>
</xsl:template>
<xsl:template match="text()"></xsl:template>
<xsl:template match="xhtml:table[starts-with(normalize-space(xhtml:tr/xhtml:td/text()),'$title')]">
<CLASS>
<xsl:value-of select="xhtml:@class"/>
</CLASS>
</xsl:template>
</xsl:stylesheet>
when i try to run this:
the exception which i got:
javax.xml.transform.TransformerConfigurationException: javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: A node test that match has either NCName:* or QName was expected
thanks
# 3 Re: Xpath help again
Sigh I have been telling you time and time again, to please post vaild xml code, and yet the stylesheet begin tag is missing. I don't understand why you continue to do this, it's not that difficult.
Your syntax for setting the namespace prefix on the class attribute is wrong. The correct syntax is @xhtml:class, but by default attributes don't have a namespace, only when the explicitly declared does attribues have a namespace. So you should only write @class.
There are a couple of other problems, in particular with your use of the param, but I'll let you try to work them out.
khp at 2007-11-10 3:29:23 >

# 4 Re: Xpath help again
Dear khp,
ops, next time i use preview before i submit the post.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xsl: output version="1.0" indent="yes" encoding="UTF-8" omit-xml-declaration="no" method="xml"/>
<xsl:template match="/xhtml:html">
<xsl: param name="title"/>
<Demo>
<Example>
<xsl:apply-templates/>
</Example>
</Demo>
</xsl:template>
<xsl:template match="text()"></xsl:template>
<xsl:template match="xhtml:table[starts-with(normalize-space(xhtml:tr/xhtml:td/text()),'$title')]">
<CLASS>
<xsl:value-of select="@class"/>
</CLASS>
</xsl:template>
</xsl:stylesheet>
i didnt get any exceptions this time, but i fail to get any match. can you please give me some hints what's wrong with my xsl: param?
thanks
# 5 Re: Xpath help again
As I told you in this thread http://www.dev-archive.com/forum/showthread.php?t=347704 The xsl:param must not be declared inside a template, params declared inside templates are for passing parameters between template.
And when you refer to the param in an xpath expression it should not be wrapped in ' ' characters.
This is better
xhtml:table[starts-with(normalize-space(xhtml:tr/xhtml:td/text()),$title)]
khp at 2007-11-10 3:31:32 >

# 6 Re: Xpath help again
Dear khp:
Thanks for replying me. I have tried but it give me this exception:
[Fatal Error]:22:8: Element or attribute do not match QName production: QName::=(NCName':')?NCName.
and my xsl stylesheet is like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xsl: output version="1.0" indent="yes" encoding="UTF-8" omit-xml-declaration="no" method="xml"/>
<xsl: param name="title"/>
<xsl:template match="/xhtml:html">
<Demo>
<Example>
<xsl:apply-templates/>
</Example>
</Demo>
</xsl:template>
<xsl:template match="text()"></xsl:template>
<xsl:template match="xhtml:table[starts-with(normalize-space(xhtml:tr/xhtml:td/text()),$title)]">
<CLASS>
<xsl:value-of select="@class"/>
</CLASS>
</xsl:template>
</xsl:stylesheet>
any idea?
thanks
# 7 Re: Xpath help again
That's very curius, I assume the whitespace in xsl: param and xsl: output above, where added by the forum (this seems to happen occationally). After removing those the code ran perfectly using Xalan-j.
The error message seems to say that there's an syntax error in an element name. I assume the 22:8 mark is a line:collum reference. Which by my count would be the </CLASS> tag, assuing the problem is with the XSL file.
Are you still using the same sample file as above ?. Could you try removing the CLASS tags from the stylesheet ?, just to check it they are the problem (which seems very strange to me).
khp at 2007-11-10 3:33:27 >

# 8 Re: Xpath help again
Dear khp:
I try with these:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xsl: output version="1.0" indent="yes" encoding="UTF-8" omit-xml-declaration="no" method="xml"/>
<xsl: param name="title"/>
<xsl:template match="/xhtml:html">
<Demo>
<Example>
<xsl:apply-templates/>
</Example>
</Demo>
</xsl:template>
<xsl:template match="text()"></xsl:template>
<xsl:template match="xhtml:table[starts-with(normalize-space(xhtml:tr/xhtml:td/text()),$title)]">
<CLASS>
success
</CLASS>
</xsl:template>
</xsl:stylesheet>
and it successfully give me
<Demo>
<Example>
<CLASS>
success
</CLASS>
</Example>
</Demo>
but how come i cannot get the attribute class?
and i try removing the <CLASS> with only:
<xsl:template match="xhtml:table[starts-with(normalize-space(xhtml:tr/xhtml:td/text()),$title)]">
<xsl: value-of select="@class"/>
</xsl:template>
same error message again.
# 9 Re: Xpath help again
Sorry ignore this post, I posted before I saw your last post.
khp at 2007-11-10 3:35:33 >

# 10 Re: Xpath help again
Instead of <xsl:value-of select="@class"/>
could you try writing <xsl:value-of select="attribute::class"/>
If that doesn't work could you try renaming the class attribute to something else in both the XML and the XSL, just to see if it's the word 'class' that's causing problems.
khp at 2007-11-10 3:36:34 >

# 11 Re: Xpath help again
Dear khp:
it works now!!
THANKS A LOT LOT!!!
but, can you please explain why "@class" fail to work??
again, thanks so much!
sharon
# 12 Re: Xpath help again
but, can you please explain why "@class" fail to work??
I regret to admit that I don't know. I would suspect it's a bug in the XSL engine.
khp at 2007-11-10 3:38:42 >

# 13 Re: Xpath help again
Dear khp,
still thanks a lot for your help.
thanks
sharon
# 14 Re: Xpath help again
Dear khp:
hi, i face Xpath problems again. :(
<?xml version="1.0" encoding="ISO-8859-1" ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Help</title>
</head>
<body>
<table>
<tr>
<td>Demo</td>
<td>Hello</td>
<td>
<img alt="help again" src="help.jpg"/>
</td>
</tr>
</table>
</body>
</html>
and my xsl stylesheet to get the alt attribute is:
<xsl:template match="tr[starts-with(normalize-space(td[3]/img/@alt/text()), 'help again')]">
<Demo>
success
</Demo>
</xsl:template>
is it anything wrong with my xpath expression? i do not understand why this xpath transformation fail to get any result?
thanks
sharon
# 15 Re: Xpath help again
There are two problems first you should not write @alt/text(), it's just @alt to get the value of an attribute. And assuming that you haven't declared the default namespace in the stylesheet to "http://www.w3.org/1999/xhtml" you also need an xhtml namespace prefix on the tr,td and img tags.
khp at 2007-11-10 3:41:39 >
