Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


xsltproc vs XPath evaluate() method

From: "A. W. Dunstan" <no@----.------>
To: NULL
Date: 6/3/2008 12:20:00 PM

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


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent