开发者

Grabbing nodes that aren't descendants of other nodes, excluding the current context

So, I have no idea how to really word this question. I want to have a list of nodes, and only select the one node, but not the nested node. For example:

<root>
    <my_element>
        <my_element>
            Some Text
        </my_element>
    </my_element>
</root>

I know I can already do some of what I want by using this xpath:

Context: /
xPath: descendant::my_element[not(ancestor::my_element)]

Which would return this result set:

<root>
    [<my_element>]
        <my_element>
            Some Text
        </my_element>
    [</my_element>]
</root>

That's the expected behaviour of what I want. But I want to be able to change the context to:

/my_element

And get this result set:

<root>
    <my_element>
        [<my_element>]
            Some Text
        [</my_element>]
    </my_element>
</root>

I've tried my best at looking at xPath documents and I haven't come up with much of anything. Perchance someone on here could provide some insight?

Thanks!

  • edit - I want to be able to select a my_element descendant which isn't an ancestor of my_element excluding the context node.

  • edit again - To further explain.

I want to have an xpath query that selects nodes of my_element so long as the node isn't a child of my_element. But, if the xpath context is set to a my_element node, then I don't want that node to count in the expression. So the xpath would then match the next my_element node, even though it is actually a child of my_element.

  • edit again -

Here are some more examples.

<root>
    <a>
        <a>
            <b>
                <a>
                    Hello!
                </a>
            </b>
            <a>
                <b>
                    Hello Again
                    <a>
                        Sub
                    </a>
                </b>
            </a>
        </a>
    </a>
</root>

Context: /root/
Desire: Want to grab all A nodes, so long as they aren't a descendant of A

Result:
<root> == Context
    [<a>]
        <a>
            <b>
                <a>
                    Hello!
                </a>
            </b>
            <a>
                <b>
                    Hello Again
                    <a>
                        Sub
                    </a>
                </b>
            </a>
        </a>
    [</a>]
</root>

Context: /root/a/
Desire: Want to grab all A nodes, so long as they aren't a descendant of A, not including the context /root/a/

Result:
<root>
    <a> == Context
        [<a>]
            <b>
                <a>
                    Hello!
                </a>
            </b>
            <a>
                <b>
                    Hello Again
                    <a>
                        Sub
                    </a>
                </b>
            </a>
        [</a>]
    </a>
</root>

Context: /root/a/a/
Desire: Want to grab all A nodes, so long as they aren't a descendant of A, not including the context /root/a/a/

Result:
<root>
    <a>
        <a> == Context
            <b>
                [<a>]
                    Hello!
                [</a>]
            </b>
            [<a>]
                <b>
                    Hello Again
                    <a>
                        Sub
                    </a>
                </b>
         开发者_StackOverflow社区   [</a>]
        </a>
    </a>
</root>

Context: /root/a/a/a/
Desire: Want to grab all A nodes, so long as they aren't a descendant of A, not including the context /root/a/a/a/

Result:
<root>
    <a>
        <a>
            <b>
                <a>
                    Hello!
                </a>
            </b>
            <a> == Context
                <b>
                    Hello Again
                    [<a>]
                        Sub
                    [</a>]
                </b>
            </a>
        </a>
    </a>
</root>

I hope this makes my desires clearer. Thank you everyone who is trying!


Use:

//my_element[not(.//my_element)]

This selects all elements named my_element that don't have any my_element descendents.


I think you may be falling into a common trap. The XPath expression /root/my_element, on your data, will select only one element - the outermost my_element node. But that node is still attached to its parents, siblings, and children. When you display the result of the selection, the node will often be displayed together with its children (indeed, all descendants) - not because the XPath selected the children, but because that's a friendly way to display the single node that was selected.

On the other hand, I've read the question again, and I might be wrong - I guessed that from the peculiar notation you were using to show the results of your XPath expression.

The expression /my_element will select my_element only if its parent is the document node at the root of the tree, which will never be the case with your input, whatever your context node. Of course, you can copy the subtree rooted at my_element into a new document, in which case this expression will work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜