0

I am using the following XPath expression to loop through a XML document:

/templatedatabase/datatypes/integers/integer[index]/@defaultvalue

Method header:

private static String getGetDefaultValueByIndex(Document doc, XPath xpath, int index) 

"index" is a parameter in the method. The problem is the following: The query doesn´t return a value if I put a number in the method header as parameter.(e.g. getGetDefaultValueByIndex(doc, xpath, 1)).

But if I, for instance put a number like 1,2,... in place of index in the query I get the data I want from the XML file while executing the same method. The XPath query is correct and the method is working if I put in the number manually in the query like this:

/templatedatabase/datatypes/integers/integer[1]/@defaultvalue

--> This returns a value.

However:

getGetDefaultValueByIndex(doc, xpath, 1)
/templatedatabase/datatypes/integers/integer[index]/@defaultvalue

--> This doesn´t return a value.

I don´t really understand why this is not working. I can´t imagine that you can´t loop through a XPath Query using a variable.

The complete code of the method:

private static String getGetDefaultValueByIndex(Document doc, XPath xpath, int index) {
         String DefaultValue = null;
         try {
                String expression = "/templatedatabase/datatypes/integers/integer" + "[index]" + "/@defaultvalue";
                DefaultValue = (String) xPath.evaluate(expression, doc, XPathConstants.STRING);     
                System.out.println(DefaultValue);
     } catch(XPathExpressionException e) {
         e.printStackTrace();
     }
         return DefaultValue;
     }

}

2
  • Do you mean: "/templatedatabase/datatypes/integers/integer" + [index] + "/@defaultvalue"; ? Commented Apr 1, 2015 at 9:32
  • Yes, but the query still doesn´t return a value when using index. It is still working when I use a number instead of index though. The query: /templatedatabase/datatypes/integers/integer" + "[index]" + "/@defaultvalue --> Still returns no value.(Index is 1 in method header.) "/templatedatabase/datatypes/integers/integer" + "[1]" + "/@defaultvalue" --> Returns value. Commented Apr 1, 2015 at 9:48

1 Answer 1

1

You've got the quotes in the wrong place, try:

String expression = "/templatedatabase/datatypes/integers/integer[" + index + "]/@defaultvalue";

This will include the value of the index parameter into the XPath expression, rather than the literal string "index".

A word of warning though - constructing XPath expressions dynamically using string concatenation is fine when you're dealing with numeric variables like index, but if you want to provide arbitrary user-specified string parameters to the expression it's safer to use a static XPath expression including XPath variables like

"/templatedatabase/datatypes[@name=$targetName]"

and define an XPathVariableResolver to inject the actual values.

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.