2

I have XML document that I parse with jDOM and then try to take all nodes that satisfies XPath expression, but XPath always returns no elements. Here is my XML file:

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

<!-- Using aggregate instance. Should pass -->

<pnml xmlns="http://www.pnml.org/version-2009/grammar/pnml">
    <net id="rootAgregate" type="http://www.cs.stu.cn.ua/jess/enets">
        <page id="main">
            <transition id="agregate1">
                <name>
                    <text>agregate1</text>
                    <graphics>
                        <offset x="22" y="-14"/>
                    </graphics>
                </name>
                <definition type="aggregate" subType="myAggregate"/>
                <graphics>
                    <position x="950" y="484"/>
                </graphics>
            </transition>
            <transition id="Input1">
                <name>
                    <text>agregate1</text>
                    <graphics>
                        <offset x="22" y="-14"/>
                    </graphics>
                </name>
                <definition type="input"/>
                <graphics>
                    <position x="950" y="484"/>
                </graphics>
            </transition>
            <transition id="Output1">
                <name>
                    <text>agregate1</text>
                    <graphics>
                        <offset x="22" y="-14"/>
                    </graphics>
                </name>
                <definition type="output"/>
                <graphics>
                    <position x="950" y="484"/>
                </graphics>
            </transition>
        </page>
    </net>
</pnml>

Here is my XPath code:

private List<String> getChildNames(Document parsedDom) throws JDOMException {
        ArrayList<String> childNames = new ArrayList<String>();
        XPath childCounter = XPath.newInstance("//definition[@type=\"aggregate\"]");
        ListIterator<Element> listIterator = childCounter.selectNodes(parsedDom).listIterator();

        while (listIterator.hasNext()) {
            Element definitionElement = listIterator.next();
            childNames.add(definitionElement.getAttributeValue("subType"));
        }

        return childNames;
    }

I checked my XPath expression here: http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm and it works.

Why it doesn't work in my code?

Thank you in advance.

1 Answer 1

2

You need the namespace in your xpath expression.

How to use XPath on xml docs having default namespace

Parse XML with XPath & namespaces in Java

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.