Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The purpose of <code>getNamespaceURI</code> inside of the <code>NamespaceContext</code> is to associate each namespace in the source document with a unique prefix, so that the XPath engine knows which namespace an element belongs to whenever it encounters that prefix string in an XPath expression. The prefix here doesn't need to match the prefix (if any) for the same URI in the source XML; it just needs to provide a mapping from the prefix to the correct namespace. </p> <p>So, if you were to write the expression like this:</p> <pre><code>//p:QDTM_IN300301QD/p:controlActEvent/p:subject/p:registrationEvent/p:subject /p:testCodeIdentifier/p:playingTestCodeDetails/p:code /p:translation[@codeSystemName='DOSCATALOGNAMEHTMLENABLED'] /p:originalText/text()" </code></pre> <p>...then you'd write the corresponding <code>getNamespaceURI</code> like this:</p> <pre><code>public String getNamespaceURI(String prefix) { if ("p".equals(prefix)) { return "urn:hl7-org:v3"; } return null; } </code></pre> <p>This is how the engine knows to look for an element in the <code>urn:hl7-org:v3</code> namespace whenever it encounters the <code>p</code> prefix, which is the whole point. Othwerwise, how would the engine know that you didn't want some element named <code>QDTM_IN300301QD</code> in <em>no</em> namespace? Or an element with that name in some other namespace?</p> <p>Note that the prefix name is arbitrary; it can be anything you want, as long as it's unique. That is, if you have other namespaces in your document, then you'll need to modify <code>getNamespaceURI</code> to be aware of those namespaces and assign a unique prefix to each of them.</p> <p>Here is a complete (minimal) example:</p> <pre><code>DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse("Draft.xml"); XPath xpath = XPathFactory.newInstance().newXPath(); NamespaceContext ctx = new NamespaceContext() { public String getNamespaceURI(String prefix) { if ("p".equals(prefix)) { return "urn:hl7-org:v3"; } return null; } public String getPrefix(String uri) { throw new UnsupportedOperationException(); } public Iterator getPrefixes(String uri) { throw new UnsupportedOperationException(); } }; xpath.setNamespaceContext(ctx); XPathExpression expr = xpath.compile("//p:QDTM_IN300301QD/p:controlActEvent" + "/p:subject/p:registrationEvent" + "/p:subject/p:testCodeIdentifier/p:playingTestCodeDetails/p:code" + "/p:translation[@codeSystemName='DOSCATALOGNAMEHTMLENABLED']" + "/p:originalText/text()"); System.out.println("[" + expr.evaluate(doc, XPathConstants.STRING)); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload