开发者

Create a "sub-tree" NodeList given an explicit Node reference

I need to generate a NodeList that represents the entire sub-tree (including attributes, comments, and namespace declarations) of a given Node in my Document.

Let's assume my document looked like this:

<?xml version="1.0"?>
<my:root xmlns:my="http://www.my.com/#">
  <my:element type="1">
    <my:content green="true">
      <xx:content xmlns:xx="http:://www.xx.com/#"/>
    </my:content>
  </my:element>
</my:root>

Now what I want is to select all the sub-nodes of including that element itself, so that my resulting selection would represent the sub-tree

<my:element type="1">
  <my:content green="true">
    <xx:content xmlns:xx="http:://www.xx.com/#"/>
  </my:content>
</my:element>

a) How can I achieve this if e.g. the type attribute could be assumed to be unique? I'm currently using an XPath expression like

/descendant-or-self::node()[@type='1']/descendant-or-self::node()

Is there a better way to do this?

b) In another scenario I'm just given explicit Node references, without any knowledge of their context in the DOM, which means that I have no means to cleverly select the 'anchor node' using any describing characteristic. Is there a way to derive an XPath expression from the Node refere开发者_开发百科nce that would select the sub-tree of that particular node? Or is the only chance that I have in this case generating the NodeList manually, by iterating over the node and its children?


Maybe Views tree + Views Datasource would help? I'd recommend experimenting with those.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜