1

I want to get all the namespaces in the xml file for example

[http://www.utilities-online.info/xpath/?save=a4ac7360-1994-44c2-9c26-c9f741dc2e44-xpath#.VeRWSvmqpBc][1]

basically problem is to to search using xpath based on namespaces and for that purpose i have to use setNamespaceContext and don't know namespaces value because it will be on runtime while parsing file.

xpath.setNamespaceContext(namespaces);

public class SimpleNamespaceContext implements NamespaceContext {

    private final Map<String, String> PREF_MAP = new HashMap<String, String>();

    public SimpleNamespaceContext(final Map<String, String> prefMap) {
        PREF_MAP.putAll(prefMap);       
    }

    public String getNamespaceURI(String prefix) {
        return PREF_MAP.get(prefix);
    }

    public String getPrefix(String uri) {
        throw new UnsupportedOperationException();
    }

    public Iterator getPrefixes(String uri) {
        throw new UnsupportedOperationException();
    }

}
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
HashMap<String, String> prefMap = new HashMap<String, String>() {{
    put("main", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
    put("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
}};
SimpleNamespaceContext namespaces = new SimpleNamespaceContext(prefMap);
xpath.setNamespaceContext(namespaces);
XPathExpression expr = xpath
        .compile("/main:workbook/main:sheets/main:sheet[1]");
Object result = expr.evaluate(doc, XPathConstants.NODESET);

reference link [How to query XML using namespaces in Java with XPath?

To implement above mention reference i need to get all namespaces query i am using to get all namespaces using xpath is

    try {
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//namespace::*");
NodeList a = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); 
System.out.println(a.getLength());
} catch (Exception e) {
Utility.consoleInfo(e);
}

but this always return nodelist of size 0 . Am i going in wrong direction to achieve this? or what else would be the solution to this problem ?

Summary How should i get all namespaces so that it can be used in xpath ?

please help me in this regard

Community
  • 1
  • 1
Ali
  • 557
  • 1
  • 9
  • 30
  • If you don't know the namespace of the element you want to access in advance, how will you know that you've found the right element? – Alohci Aug 31 '15 at 14:21
  • i want to find all xmlns uri values in dom . without those namespaces values i will not be able to use xpath – Ali Aug 31 '15 at 14:22
  • My point is that knowing the xmlns *attributes* in the DOM won't help. You still won't know what they *mean*. What *elements* must your populated NamespaceContext help you find? – Alohci Aug 31 '15 at 14:28
  • Lets suppose i have all namespace uri including prefix than i can simple put them in map using above code and search using xpath .. am i going in wrong direction?? if yes than what other solution you will provide – Ali Aug 31 '15 at 14:31
  • Lets say your xml is `fredbill` which element should your xpath find? The one containing "fred", the one containing "bill", both, or neither? – Alohci Aug 31 '15 at 14:37
  • 1
    You're probably going in the wrong direction. I've been where you are right now with XML, thinking that you need to extract the namespaces at run time, and you almost never do. But without understanding your end goal, rather than your intermediate problem, I can't be sure. – Alohci Aug 31 '15 at 14:51
  • As you mentioned the foo bar namespace example than how would I search Foo: name. I would have to search the namespaces than I can be able to do it. – Ali Aug 31 '15 at 15:26
  • Simply extract namespaces values at runtime http://www.utilities-online.info/xpath/?save=a4ac7360-1994-44c2-9c26-c9f741dc2e44-xpath#.VeRWSvmqpBc][1] as working in this example – Ali Aug 31 '15 at 15:31
  • 2
    Let's try to clarify this further ... you want to extract both namespace prefixes and URIs, so that you can construct XPath queries to query an XML document? Then it sounds like you don't need the prefixes, you just need the namespace URIs. The namespace prefixes in an XPath expression bear no direct relation to the prefixes in the XML document being queried. – LarsH Aug 31 '15 at 17:44
  • 2
    And are you really trying to query an XML document without knowing what namespaces (URIs) its elements are in? If so, can you just use namespace-agnostic expressions like `"//*[local-name='name']"`? – LarsH Aug 31 '15 at 17:45
  • Christian Bale Liam Neeson Michael Caine Tom Waits B.B. King Ray Charles using this xml how can i query xpath to get foo:singers ? keypoint is i donot know the namespaces uri and prefixes ... – Ali Sep 01 '15 at 03:31
  • @Mark as mentioned by LarsH, you can do `//*[local-name() = 'singers']` since you don't know namepsace uri and the prefix – har07 Sep 01 '15 at 04:45

0 Answers0