0

I have a XML file that contains two elements: Project and Layer. I want to get attribute idLayer with the highest number using java. My code is not working properly:

public int GetMaxID() throws JAXBException {
    try {
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xPath = xPathFactory.newXPath();
        String expression = "//Project/Layer/@idLayer[not(. <=../preceding-sibling::Layer/@idLayer) and not(. <=../following-sibling::Layer/@idLayer)]";
        XPathExpression xPathExpression = xPath.compile(expression);
        InputSource doc = newInputSource(newInputStreamReader(newFileInputStream(newFile("Projects//asdad//ProjectDataBase.xml"))));
        NodeList elem1List = (NodeList) xPathExpression.evaluate(doc, XPathConstants.NODESET);
        int maxId = elem1List.getLength();//give me 0
    } catch (Exception e) {
        e.printStackTrace();
    }
    return -1;
}  

My XML code:

<tns:Project xmlns:tns="http://www.example.org/ProjectDataBase" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/ProjectDataBase ProjectDataBase.xsd ">
  <tns:Layer idLayer="1">
    <tns:LayerName>tns:LayerName1</tns:LayerName> 
  </tns:Layer>
  <tns:Layer idLayer="2">
    <tns:LayerName>tns:LayerName2</tns:LayerName> 
  </tns:Layer>
  <tns:Layer idLayer="3">
    <tns:LayerName>tns:LayerName3</tns:LayerName> 
  </tns:Layer>
 </tns:Project>

Can you point me to the right direction?

MohammadTofi
  • 365
  • 1
  • 7
  • 22
  • You could always iterate through all the `` elements and keep track of the one with the highest `idLayer` attribute. Not very elegant, though... – NWard Apr 08 '14 at 21:26
  • XSLT 2.0 knows the `max` keyword, so your XPATH could look like this: `max(//Project/Layer/@idLayer)`. For an equivalent XSLT 1.0 solution have a look at this [SO post](http://stackoverflow.com/questions/12835990/how-to-find-min-and-max-value-using-xsl-1-0). – keenthinker Apr 08 '14 at 21:30
  • Your XPath expression does match the `idLayer` attribute of the last `` element in your sample code - which is what you wanted. If the problem isn't in somewhere else, then could this be a namespace issue? Does your XML have any (default or prefixed) namespace? – jasso Apr 08 '14 at 21:44
  • yes content default namespace, I am sure my code is work because when I change instruction xpath to (".") then my code is work when i but instruction xpath ("same my code") is not give me any answer. – MohammadTofi Apr 09 '14 at 08:23
  • *I change instruction xpath to (".") then my code is work*, this does not rule out a namespace issue, `.` matches regardless of the namespace. – Tobias Klevenz Apr 09 '14 at 09:58
  • I add default namespace but my code not give me max id.please can you help me – MohammadTofi Apr 09 '14 at 10:22

1 Answers1

0

Your problem is the tns namespace. You don't use it in your XPath expression, therefore it cannot select anything.

There are countless examples of how to register XML namespaces with JDOM, for example this one.

Also, your XPath is way too complicated.

Use //tns:Project/tns:Layer[not(@idLayer < ../tns:Layer/@idLayer)]/@idLayer.

Mind that this does not give the maximum node, but all the maximum nodes - there could be more than one.

Community
  • 1
  • 1
Tomalak
  • 332,285
  • 67
  • 532
  • 628