xmlparsingxpathsaxparser

XPath - XML document nodes selection problem


Need help with XPATH.

I use this library jcabi (see samples) to process XML document. I'm facing a problem (all nodes named "member" are retrived, I just want the first level of "member" nodes not the nested ones.

    XML myXml = new XMLDocument(stringXMLFormat);
    List<XML> members = myXml.xpath("//struct/*"); // the problem is "//stuct/*" I can't find appropriate expression to query only first level of member nodes.

    Instead having members.size() = 4, I have members.size() = 6.
    
    I dont want the two nested member (Member Level 2)
    
    <struct>     +------------------------------------
         <member>| Exclude this part from the result
                 <struct>
                 |     <member>
                 +------------------------------------
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<methodResponse>
    <params>
        <param>
            <value>
                <struct>   <!-- NOT THIS : I apply XPATH query on this :   myXml.xpath("//struct/*") ==> Retrieve all member nodes in the document; but I just want the firs level of member nodes --> 
                    <member> <!-- Member -> Level 1 : Must be retrive --> 
                        <name>attribut1</name>
                        <value>
                            <int>0</int>
                        </value>
                    </member>
                    <member> <!--Member -> Level 1 : Must be retrive --> 
                        <name>attribut2</name>
                        <value>
                            <struct>
                                <member> <!-- Member Level 2 : I don't want to retrive this --> 
                                    <name>attribut2-attr1</name>
                                    <value>
                                        <boolean>1</boolean>
                                    </value>
                                </member>
                            </struct>
                        </value>
                    </member>
                    <member> <!--Member -> Level 1 : Must be retrive --> 
                        <name>attribut3</name>
                        <value>
                            <array>
                                <data>
                                    <value>
                                        <struct> <!--NOT THIS --> 
                                            <member> <!--Member -> Level 2 : I don't want to retrive this --> 
                                                <name>attiribut3-attr-1</name>
                                                <value>
                                                    <int>1</int>
                                                </value>
                                            </member>
                                        </struct>
                                    </value>
                                </data>
                            </array>
                        </value>
                    </member>
                    <member> <!-- Member -> Level 1 : Must be retrive --> 
                        <name>attribut4</name> 
                        <value>
                            <string>10</string>
                        </value>
                    </member>
                </struct>
            </value>
        </param>
    </params>
</methodResponse>

How can I select just the first level of member WITHOUT selected all member in the document ?

Thank you for your help.


Solution

  • This XPath,

    //member[not(ancestor::member)]
    

    will select all member elements that do not have member element ancestors.