nclark
nclark

Reputation: 1082

How to define XML namespace in Saxon CLI such that I can query on things in that namespace?

I'm trying to read the version of a Maven POM file using the Saxon CLI as follows:

java -cp Saxon-HE-9.7.0-4.jar net.sf.saxon.Query -s:./pom.xml -qs:/project/version \!method=text

The file pom.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>
    <groupId>foo</groupId>
    <artifactId>bar</artifactId>
    <packaging>pom</packaging>
    <name>baz</name>
    <version>0.0.1-SNAPSHOT</version>
</project>

I'm getting an error because I'm specifying a default namespace. If I omit that namespace declaration (xmlns="http://maven.apache.org/POM/4.0.0"), the query works. I gather from the Saxon documentation that I need to do something like maybe define a namespace handler, but I find I'm suddenly diving into XML namespace esoterica. Anybody know how to do this with a simple option on the Saxon command line?

Upvotes: 1

Views: 136

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167436

I would be inclined to use -qs:/*:project/*:version from the command line as that works with any namespace.

Otherwise you would need to use https://www.w3.org/TR/xquery-30/#id-default-namespace but it is probably a pain to with quotes and the command line interpreter.

Upvotes: 3

Related Questions