472,954 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

xsltproc vs XPath evaluate() method

I'm trying to figure out how XPath expressions work, and how I can use them
to extract data into a particular format. I can extract the data I want
using an XPath expression, but not with an XSLT stylesheet. Here's the
Java/XPath I'm using:
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.xpath.*;

public class XpathEval
{
public static void main(String []args)
{
try {
XpathEval loader = new XpathEval("src.xml");
loader.evaluateXpath();
}
catch(Exception e) {
System.out.println(e);
}
}
public XpathEval(String filename)
throws javax.xml.parsers.ParserConfigurationException,
SAXException,
IOException
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
m_doc = db.parse(new File(filename));
}

public void evaluateXpath(String xpathExpr)
throws XPathExpressionException
{
XPathFactory f = XPathFactory.newInstance();
XPath p = f.newXPath("/nuc2/Exer/ExerciseNickname");
NodeList nl = (NodeList)p.evaluate(xpathExpr, m_doc,
XPathConstants.NODESET);
for (int i = 0; i < nl.getLength(); i++)
System.out.println(nl.item(i).getNodeName() + " -" +
nl.item(i).getTextContent());
}

private Document m_doc;
}
However, if I try to extract & format the data via xsltproc it doesn't
extract the data. Here's the command I use:

xsltproc xsltStyleSheet.xsl src.xml

with the following for xsltStyleSheet.xsl:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:template match="/">
EXER/<xsl:value-of
select="/nuc2/Exer/ExerciseNickname"/>/NICK:<xsl:value-of
select="/nuc2/Exer/ExerciseAdditionalIdentifier"/>//
</xsl:template>

</xsl:stylesheet>

Instead of printing out what I expect (the odd format is a requirement for
what I'm trying to convert it to):

EXER/pushups/NICK:whatever//

it prints this:

EXER//NICK://

If I change the XPath expression in my XSLT stylesheet to
select="//ExerciseNickname" then it works. But in the full XML file there
are places where that won't work - there's /a/b/fieldname
and /x/y/fieldname and I want exactly one of them. I suppose I could make
that work, but I'd like to know what I'm doing wrong.

The XML source (src.xml, above) is:

<ns2:nuc2 xsi:schemaLocation="nuc2.xsd" xmlns:ns1="Exer_Sets"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="nuc2">

<ns1:Exer SetSequence="1" SetDescription="Exercise Identification">
<ExerciseNickname>pushups</ExerciseNickname>
<ExerciseAdditionalIdentifier>whatever</ExerciseAdditionalIdentifier>
</ns1:Exer>

</ns2:nuc2>
I'm giving it the same XPath expression in both cases
("/nuc2/Exer/ExerciseNickname") - one finds what I'm looking for, the other
doesn't. I'm using Java 1.6.0_05 on Fedora 8. xsltproc comes from the
libxslt package, version 1.1.24.

Any clues?

thanks!

--
Al Dunstan, Software Engineer
OptiMetrics, Inc.
3115 Professional Drive
Ann Arbor, MI 48104-5131
Jun 27 '08 #1
2 2995
A. W. Dunstan wrote:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
For XPath stuff you should always use a namespace aware factory/document
builder so make sure you call the method to enable that.
The XML source (src.xml, above) is:

<ns2:nuc2 xsi:schemaLocation="nuc2.xsd" xmlns:ns1="Exer_Sets"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="nuc2">

<ns1:Exer SetSequence="1" SetDescription="Exercise Identification">
<ExerciseNickname>pushups</ExerciseNickname>
<ExerciseAdditionalIdentifier>whatever</ExerciseAdditionalIdentifier>
</ns1:Exer>

</ns2:nuc2>
So your XML uses namespaces, to cater for that the XSLT stylesheet needs
to bind prefixes to the namespaces used:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:ns2="nuc2"
xmlns:ns1="Exer_Sets"
exclude-result-prefixes="ns1 ns2">
then you need to use those prefixes in your XPath expressions and in
your XSLT match patterns:

<xsl:value-of select="/ns2:nuc2/ns1:Exer/ExerciseNickname"/>

You just got lucky with your Java program as you used a not namespace
aware DOM I think, if you use a namespace aware DOM then you need
prefixes in your XPath expressions too and need to manage them in your
Java program too.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
Martin Honnen wrote:
A. W. Dunstan wrote:
> DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();

For XPath stuff you should always use a namespace aware factory/document
builder so make sure you call the method to enable that.
>The XML source (src.xml, above) is:

<ns2:nuc2 xsi:schemaLocation="nuc2.xsd" xmlns:ns1="Exer_Sets"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns2="nuc2">

<ns1:Exer SetSequence="1" SetDescription="Exercise Identification">
<ExerciseNickname>pushups</ExerciseNickname>
<ExerciseAdditionalIdentifier>whatever</ExerciseAdditionalIdentifier>
</ns1:Exer>

</ns2:nuc2>

So your XML uses namespaces, to cater for that the XSLT stylesheet needs
to bind prefixes to the namespaces used:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:ns2="nuc2"
xmlns:ns1="Exer_Sets"
exclude-result-prefixes="ns1 ns2">
then you need to use those prefixes in your XPath expressions and in
your XSLT match patterns:

<xsl:value-of select="/ns2:nuc2/ns1:Exer/ExerciseNickname"/>

You just got lucky with your Java program as you used a not namespace
aware DOM I think, if you use a namespace aware DOM then you need
prefixes in your XPath expressions too and need to manage them in your
Java program too.
It works! Much thanks. I'll go read up on namespaces.

--
Al Dunstan, Software Engineer
OptiMetrics, Inc.
3115 Professional Drive
Ann Arbor, MI 48104-5131
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: gael.pegliasco | last post by:
Hi, How are you dear and nice helper :) ? I'm trying to test xpath with this simple program : import xml.dom.minidom from xml.xpath.Context import Context import xml.xpath
2
by: Neil Zanella | last post by:
Hello, I would like to know whether the mozilla web browser has built in support for searching XML documents via XPath expressions as with IE's xmlobject's and xmlDoc's function selectNodes() or...
4
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL Transforming. If possible, How?? Because I'm not a...
1
by: Loudin | last post by:
Hello, I`ve got Problems with Xpath and xsltproc/libxslt. I have got a variable with tags in my Style sheet and later in the same Style sheet i want to work with this tags using a...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
9
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
3
by: abcd_68 | last post by:
Hi there, I'm using, for the first time, the JDK1.5 Xpath API. I need to find elements in a Hibernate-generated .hbm.xml file. These files come with a <!DOCTYPE header mentioning a remote URL....
6
by: Hoss | last post by:
Hello. Because IE and Mozilla have such completely different XML implementations, I have created a class to handle general XML tasks, such as iterating over nodes given an xpath, evaluating an...
1
by: Arndt Jonasson | last post by:
The way I read the XPath 1.0 specification, queries like "//*/ text()/.." and "//*/child::text()" should be valid. xmllint seems to agree with me, but xsltproc gives syntax errors for those...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.