0

I need to join two XPath queries that refer to different nodes to get all possible combinations of countries and their statuses from the xml.

Is there any method to extract several attributes from different elements not separately?

What have I tried?

A note: I have * before [@Country] and [@Status] in all my queries so the code below with * works. For some reason * is not displayed correctly within the text below.

  1. Just for a check I separated the code below to two different XPath "//*[@Country]" and "//[@Status]" and duplicating code => the correct data is exported for each query so I can get two lists of data for countries (7000 entries) and statuses (7000 entries) however I need to extract this data using one request.
  2. Changing "//[@Country] | //[@Status]") to ("//[@Country] and //[@Status]") does not work -> XPathExpressionException appears: Can not convert #BOOLEAN to a NodeList!

If try using the code below, countryStatusNodes.getLength() has 14000 while it should have had 7000 entries.

So it seems that the mistake is still in ("//[@Country] | //[@Status]".

        XPathExpression countryStatusExpr = XPathFactory.newInstance().newXPath().compile("//*[@Country] | //*[@Status]");
        Object countryStatusResult = countryStatusExpr.evaluate(document, XPathConstants.NODESET);
        NodeList countryStatusNodes = (NodeList) countryStatusResult;
        for (int i = 0; i < countryStatusNodes.getLength(); i++) {
            NamedNodeMap attr = countryStatusNodes.item(i).getAttributes();
            Node country = attr.getNamedItem("Country");
            Node status = attr.getNamedItem("Status");
            nodes.add(country.getNodeValue() + "," + status.getNodeValue());
        }

xml:

<Document>
  <Info>
    <Name FullName=".." ShortName="..." />
  </Info>
  <Location>
    <Adress Code=".." Country=".." FullAdress="..." IncorporationDate="..." />
  </Location>
  <Registration>
    <License Status=".." LicenseDate=".." />
  </Registration>
</Document>
JuliaN
  • 1
  • 1
  • If you want to selext all attributes named `Country` then I would expect `//@Country`, for all attributes named `Status` I would expect `//Status`, and you can of course form the union `//@Country | //@Status`. You haven't shown the input sample structure and sample data and your text and code are presenting different expressions (`//[@Country] | //[@Status]` versus `//*[@Country] | //*[@Status]`). – Martin Honnen May 30 '22 at 08:39
  • @MartinHonnen thank you for your comment. I have updated my question: added xml and the note that the code with * is correct while * is not displayed correctly in the text for some reason, I have * in all my queries before *[@Country] and *[@Status] – JuliaN May 30 '22 at 09:30
  • If you want the elements you can of course use e.g. `//*[@Country | @Status]` but given the input sample those attributes are present on different elements so your code iterating the elements returned from XPath but trying to read both attributes for the same elements might fall over with an exception. So it is still not clear which result data you want. – Martin Honnen May 30 '22 at 09:46
  • @MartinHonnen, thank you, you are right, this is exactly what happens. My code sees only attributes similar to Country but not for Status. Finally, I need to create a table with 3 columns (all countries possible, all statuses possible in that countries, #companies in that countries with that status). For that I decided to extract all countries and statuses with one request and to calculate the 3rd column after that. – JuliaN May 30 '22 at 09:57

0 Answers0