0

I want to get the value{namevalue1} from the xPath for the below xml.

<?xml version="1.0" encoding="UTF-8" ?>
<properties>
    <entry key="name1">namevalue1</entry>
    <entry key="name2">namevalue2</entry>
</properties>

What is the xPath for the value1?

3
  • 1
    /properties/entry[@key='name1']... Commented Sep 15, 2015 at 14:44
  • @JeffMercado : I just want the value i.e. 'namevalue1' . But, above solution gives me whole entry '<entry key="name1">namevalue1</entry>' Commented Sep 15, 2015 at 14:45
  • Are you doing this in a particular programming language? This would help us give you a more focused answer. Commented Sep 15, 2015 at 14:54

2 Answers 2

1

To expand on what Jeff proposed, you'll need to append a text() to get just the text node:

% < in.xml 
<?xml version="1.0" encoding="UTF-8" ?>
<properties>
    <entry key="name1">namevalue1</entry>
    <entry key="name2">namevalue2</entry>
</properties>
% xpquery '/properties/entry[@key="name1"]/text()' in.xml
namevalue1
%
Sign up to request clarification or add additional context in comments.

2 Comments

I am not supposed to use any function e.g. text()
text() is not a function, it is a node-type test. The only purpose of the empty parens is to indicate to XPath that you are not looking for a node called <text>,
1

Try using this:

/properties/entry[@key='name1']/text()

3 Comments

I am not supposed to use any function like text(). Is there any way to do it without function?
Not that I know of ... why can you not use functions? They are a part of the Xpath specification.
The text() function is listed in section 2.5 of the Xpath spec at w3.org/TR/xpath/#path-abbrev ... why can it not be used?

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.