Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First off: You can't use XPath to get an XML document "down to something". You can use it to <em>select</em> nodes, that's all. If you want to change the XML document, use XSLT.</p> <p>This expression:</p> <pre><code>//item/name/parent::item </code></pre> <p>does not select "the name nodes, and its parent", it selects the parent nodes of <code>&lt;name&gt;</code> nodes, and nothing else. </p> <p>Strictly speaking, it selects all <code>&lt;item&gt;</code> nodes that happen to be parent of a <code>&lt;name&gt;</code> node that is itself child of an <code>&lt;item&gt;</code> node. Which is equivalent to using just <code>"//item"</code>, when you think about it.</p> <p>There is no way to select a structure of nodes. You can only select a list of nodes - a node set. You could then traverse those nodes and find out about their position in the document, but the node set itself is flat.</p> <p>I think you need to explain more closely what you are trying to do. I could write an XSL transformation that does what you seem to intend, but unless I am sure what you intend... ;-)</p> <p>EDIT:</p> <p>Here is one minimalistic XSLT 1.0 approach that would do it.</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:template match="root | item | name | timestamp"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="*" /&gt; &lt;xsl:if test="count(*) = 0"&gt; &lt;xsl:value-of select="text()" /&gt; &lt;/xsl:if&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="* | text()" /&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Output for your sample (indentation mine):</p> <pre><code>&lt;root&gt;e &lt;item&gt; &lt;name&gt;item name&lt;/name&gt; &lt;timestamp&gt;47398743598&lt;/timestamp&gt; &lt;/item&gt; &lt;item&gt; ... &lt;/item&gt; &lt;/root&gt; </code></pre>
 

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