Note that there are some explanatory texts on larger screens.

plurals
  1. POXPath - Get id attribute from parent element
    text
    copied!<p>i have following xml file:</p> <pre><code>&lt;diagnostic version="1.0"&gt; &lt;!-- diagnostic panel 1 --&gt; &lt;panel xml:id="0"&gt; &lt;!-- list controls --&gt; &lt;control xml:id="0_0"&gt; &lt;settings description="text 1"/&gt; &lt;/control&gt; &lt;control xml:id="0_1"&gt; &lt;settings description="text 2"/&gt; &lt;/control&gt; &lt;/panel&gt; &lt;panel xml:id="1"&gt; &lt;!-- list controls --&gt; &lt;control xml:id="1_0"&gt; &lt;settings description="text 3"/&gt; &lt;/control&gt; &lt;control xml:id="1_1"&gt; &lt;settings description="text 4"/&gt; &lt;/control&gt; &lt;/panel&gt; &lt;/diagnostic&gt; </code></pre> <p>and definition XPath: </p> <pre>//*[not(@description='-')]/@description</pre> <p>and Java code:</p> <pre><code>DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse("diagnostic.xml"); XPath xpath = XPathFactory.newInstance().newXPath(); // XPath Query for showing all nodes value XPathExpression expr = xpath.compile("//*[not(@description='-')]/@description"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i &lt; nodes.getLength(); i++) { System.out.println(i + ": " + nodes.item(i).getParentNode() + nodes.item(i).getNodeValue()); } </code></pre> <p>This definition of XPath would return all attribute values <strong>​​description</strong> where the value of this attribute is not '-'. </p> <p>Output:</p> <pre><code>text 1 text 2 text 3 text 4 </code></pre> <p>But I need to find this attribute description also attribute <strong>xml:id</strong> element <strong>control</strong>.</p> <p>Output:</p> <pre><code>0_0 text 1 0_1 text 2 1_0 text 3 1_1 text 4 </code></pre> <p>How to do that in my description also returns a <strong>xml:id</strong> element of <strong>control</strong>? I need to know that the description given element is <strong>control</strong>.</p>
 

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