 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 30th, 2007, 06:07 AM
|
|
Authorized User
|
|
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
retrieving values with xsl
Hello everyone,
I was wondering if there is any way to retrieve the highest value of a node.What I mean is that i dont want to create another xml file from the input one, I only want to obtain this value.
I'm using:
XSL processor:SAXON
XSL 2.0
More precisely my xml input file has basically the following structure:
<situation id=1 date="2007-03-05T07:43:00+02:00">
</situation>
<situation id=1 date="2007-03-05T09:43:00+02:00">
</situation>
<situation id=1 date="2007-03-05T05:43:00+02:00">
</situation>
<situation id=1 date="2007-03-05T010:43:00+02:00">
</situation>
<situation id=1 date="2007-03-05T011:43:00+02:00">
</situation>
What I want to obtain is the maximum date in the xml to use it afetrwards in my java program:
I would need something like this:
String date = d2lxml=XslTransform(xml_input)
Is this possible, or I would need to parse it with a java program.
Thanks for the help.
Tomi
|
|

March 30th, 2007, 09:08 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
|
|
|
|

March 31st, 2007, 05:28 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You don't need XSLT for this, you can do it in XPath. The XPath 2.0 expression you need is max(//situation/date/xs:date(.)). See
http://www.saxonica.com/documentatio...api/intro.html
for how to invoke Saxon's XPath engine from Java. Remember that you will need to bind the namespace prefix "xs" to the namespace "http://www.w3.org/2001/XMLSchema".
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

April 2nd, 2007, 06:34 AM
|
|
Authorized User
|
|
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I've tried the following code:
String result = new String();
try
{
XPathFactory factory =
javax.xml.xpath.XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expression = xpath.compile("fn:max(//situation/situationRecord/situationRecordVersionTime)");
result = expression.evaluate(new org.xml.sax.InputSource(xml));
}
catch(Exception e)
{
System.out.println("Error Eval(): "+ e.getMessage());
}
System.out.println(result);
And I get the following error:
java.net.MalformedURLException: no protocol: <the xml file>
It looks like it is expecting a dtd.
I cant figure out what the problem is.
Any ideas?
Thank you very much
|
|

April 2nd, 2007, 07:15 AM
|
|
Authorized User
|
|
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I forgot to tell you that the variable 'xml' is a String that contains the xml file that I want to query.
Thanks in advance.
Tomi
|
|

April 2nd, 2007, 07:51 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
new InputSource(String s) expects s to be a URI identitying the location of your XML.
If you want to supply the actual XML, use
new InputSource(new StringReader(s))
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

April 4th, 2007, 04:23 AM
|
|
Authorized User
|
|
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
My new code is:
String result = new String();
XPathFactory factory =
javax.xml.xpath.XPathFactory.newInstance();
XPath xpath = factory.newXPath();
try
{
XPathExpression expression = xpath.compile("fn:max(//datex:situation/datex:situationRecord/datex:situationRecordVersionTime/xs:dateTime(.) )");
result = expression.evaluate(new InputSource(new StringReader(xml)));
}
catch(Exception e)
{
System.out.println("Error Eval(): "+ e.getMessage());
}
return result;
I get the following error:
javax.xml.transform.TransformerException: expected ,, but found: (
I think it doesnt recognize the function max, do I need to do something before like xpath.setXPathFunctionResolver(new org.apache.xalan.extensions.XPathFunctionResolverI mpl());????
I really appreciate the help
|
|

April 4th, 2007, 04:58 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You need to use a processor such as Saxon that supports XPath 2.0. This won't work with Xalan.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

April 4th, 2007, 05:07 AM
|
|
Authorized User
|
|
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I think I do use saxon, I do so in the other parts of my application.How can I found this out?
Thanks for your time.
|
|

April 4th, 2007, 06:49 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Your mention of
org.apache.xalan.extensions.XPathFunctionResolverI mpl()
certainly suggested you were using Xalan!
Try doing System.err.println(factory.getClass().getName()) to see what you've loaded. Setting the system property jaxp.debug="1" can give you useful diagnostics. To be honest though if you want to load Saxon the best way is to ignore the JAXP factory completely and just do
XPath xpath = new net.sf.saxon.xpath.XPathEvaluator();
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|
 |