3
votes

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?

2
It is possible because that the name node missed the namespace. How do you use the XPath query, in XSL file or application? Can you provide more information?Edward Zhu

2 Answers

1
votes

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']
0
votes