You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by "Koes, Derrick" <De...@Smith-Nephew.com> on 2002/06/06 16:07:44 UTC

XPathAPI class question with Xalan 2.2

I am trying to get the value of the userId attribute for each of the
"users".

What follows is the sample xml and java code.

The selectNodeList call seems to work fine.

However, the select selectSingleNode calls do not work.

Can anyone help?

 

 

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

<List>

            <User userId="1"/>

            <User userId="2"/>

</List>

 

import java.io.File;

import java.io.FileInputStream;

 

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.apache.xpath.XPathAPI;

 

public class TestXPath

{

    public TestXPath()

    {

    }

 

    private void test()

    {

        try

        {

            File file = new File("D:/Users.xml");

            FileInputStream fis = new FileInputStream(file);

            javax.xml.parsers.DocumentBuilderFactory dbf =

            javax.xml.parsers.DocumentBuilderFactory.newInstance();

            javax.xml.parsers.DocumentBuilder docBuilder =
dbf.newDocumentBuilder();

            Document doc = docBuilder.parse(fis);

            NodeList list = XPathAPI.selectNodeList( doc, "/List/User" );

            for (int i = 0; i < list.getLength(); i++)

            {

                Node user = list.item(i);

                Node userId = XPathAPI.selectSingleNode(user,
"./User/@userId");

                System.out.println(userId.getNodeValue());

            }

        }

        catch (Exception e)

        {

            System.out.println("Oh no!  Your xpath expression did not
succeed.");

            e.printStackTrace();

        }

    }

 

    public static void main(String[] args)

    {

        TestXPath testXPath1 = new TestXPath();

 

        testXPath1.test();

    }

}


Re: XPathAPI class question with Xalan 2.2

Posted by Christian Geuer-Pollmann <ma...@nue.et-inf.uni-siegen.de>.
> <List>
>             <User userId="1"/>
> </List>
>
> NodeList list = XPathAPI.selectNodeList( doc, "/List/User" );
> Node userId = XPathAPI.selectSingleNode(user, "./User/@userId");

Wrong, you're already on the User element. Try this

NodeList list = XPathAPI.selectNodeList( doc, "/List/User" );
Node userId = XPathAPI.selectSingleNode(user, "./@userId");

Or this

NodeList list = XPathAPI.selectNodeList( doc, "/List/User/@userId" );