1

For my application I only need to determine the namespace of the root node, so ideally I would like to execute a single operation to get this namespace.

I have code that uses an XPath to get all of the namespaces declared (/*/namespace::*), but then I have to go through them all and do vn.toRawString for each one to compare it to my prefix, which I would like to avoid.

I'm sure I'm missing something obvious here, but what is the best way to get the namespace of a given element, since xmlns: attributes are not considered in getAttrValNS ?

String elementName = vn.toRawString(vn.getCurrentIndex());

String prefix = prefix(elementName);
String localName = localName(elementName);

QName rootQName;
if (prefix == null) {
    rootQName = new QName(localName);
} else {
    int nsIndex = vn.getAttrValNS("xmlns", prefix);

    // Can't find index because xmlns attributes are not included

    String namespace =  vn.toRawString(nsIndex);
    rootQName = new QName(namespace, localName);
}

Is there a simple XPath that will just give me the namespace of the root node, not ALL namespaces DECLARED on the root node?

Rick Barkhouse
  • 1,186
  • 2
  • 10
  • 15

1 Answers1

1
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("namespace-uri(.)");
String ns = ap.evalXPathToString();
Rick Barkhouse
  • 1,186
  • 2
  • 10
  • 15