Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old March 30th, 2007, 06:07 AM
Authorized User
 
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

 
Old March 30th, 2007, 09:08 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

For Date time functions, try here:

http://www.w3schools.com/xpath/xpath...s.asp#datetime



 
Old March 31st, 2007, 05:28 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old April 2nd, 2007, 06:34 AM
Authorized User
 
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





 
Old April 2nd, 2007, 07:15 AM
Authorized User
 
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old April 2nd, 2007, 07:51 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old April 4th, 2007, 04:23 AM
Authorized User
 
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default


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

 
Old April 4th, 2007, 04:58 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old April 4th, 2007, 05:07 AM
Authorized User
 
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

 
Old April 4th, 2007, 06:49 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
retrieving comma separated values from column abhishekkashyap27 SQL Server 2005 6 April 21st, 2008 09:27 AM
Pass link values as xsl:parameter to php5 then xsl pauljr8 XSLT 1 July 2nd, 2007 10:32 PM
values comparison in xsl sani723 XSLT 2 April 3rd, 2007 04:05 AM
delay in retrieving values through session mrjoka ASP.NET 2.0 Basics 2 March 14th, 2007 08:02 AM
Need help in retrieving values from a loop ! back2grave XSLT 0 June 27th, 2006 05:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.