Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolving an Xpath query

Tags:

xml

xpath

Hi I have a doubt related to XPath.

My xml file looks as follows.

<?xml version="1.0" encoding="UTF-8"?>

<name xmlns="http://localhost/book" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://localhost/book books.xsd">
Java and XML
</name>

here is the xpath query and its result

/* - returns element "name"

/*/text() - returns text "Java and XML"

/name - no result

/name/text() - no result

Why specifying name is not giving any result?

like image 509
Ganesh P Avatar asked Nov 18 '25 01:11

Ganesh P


1 Answers

That is because element name is declared in http://localhost/book. Therefore in XPath query you should specify it. As a rule you should pass to your XML engine namespace and it prefix, then query your XML using full-qualified name, i.e.:

/ns:name/text()

However you can use other technique specifying namespace in query, i.e.:

/*[local-name() = 'name' and namespace-uri() = 'http://localhost/book']
like image 61
Kirill Polishchuk Avatar answered Nov 19 '25 15:11

Kirill Polishchuk