XPath - XML 文档节点选择问题
XPath - XML document nodes selection problem
需要有关 XPATH 的帮助。
我使用这个库 jcabi (see samples) 来处理 XML 文档。我遇到了一个问题(所有名为“member”的节点都被检索了,我只想要第一级“member”节点而不是嵌套节点。
XML myXml = new XMLDocument(stringXMLFormat);
List<XML> members = myXml.xpath("//struct/*"); // the problem is "//stuct/*" I can't find appropriate expression to query only first level of member nodes.
Instead having members.size() = 4, I have members.size() = 6.
I dont want the two nested member (Member Level 2)
<struct> +------------------------------------
<member>| Exclude this part from the result
<struct>
| <member>
+------------------------------------
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<methodResponse>
<params>
<param>
<value>
<struct> <!-- NOT THIS : I apply XPATH query on this : myXml.xpath("//struct/*") ==> Retrieve all member nodes in the document; but I just want the firs level of member nodes -->
<member> <!-- Member -> Level 1 : Must be retrive -->
<name>attribut1</name>
<value>
<int>0</int>
</value>
</member>
<member> <!--Member -> Level 1 : Must be retrive -->
<name>attribut2</name>
<value>
<struct>
<member> <!-- Member Level 2 : I don't want to retrive this -->
<name>attribut2-attr1</name>
<value>
<boolean>1</boolean>
</value>
</member>
</struct>
</value>
</member>
<member> <!--Member -> Level 1 : Must be retrive -->
<name>attribut3</name>
<value>
<array>
<data>
<value>
<struct> <!--NOT THIS -->
<member> <!--Member -> Level 2 : I don't want to retrive this -->
<name>attiribut3-attr-1</name>
<value>
<int>1</int>
</value>
</member>
</struct>
</value>
</data>
</array>
</value>
</member>
<member> <!-- Member -> Level 1 : Must be retrive -->
<name>attribut4</name>
<value>
<string>10</string>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodResponse>
我怎么能select只是一级会员WITHOUTselect编辑文档中的所有成员 ?
感谢您的帮助。
而不是
"//struct/*"
使用
"//param/value/struct/*"
UPD
说明:
您只是 selecting struct
标记元素下面的所有元素,而我的表达式将 select //param/value/struct
.
下面的所有元素
这意味着:
查找直接跟在 value
后面的 param
元素,直接跟在 struct
后面。现在找到该节点下的所有元素。
这将 select 仅相关 member
节点,因为也有内部 member
节点,但它们位于此节点序列下方://data/value/struct
.
kjhughes 在这里给出的第二个解决方案也很好。
这个 XPath,
//member[not(ancestor::member)]
将 select 所有 member
没有 member
元素祖先的元素。
需要有关 XPATH 的帮助。
我使用这个库 jcabi (see samples) 来处理 XML 文档。我遇到了一个问题(所有名为“member”的节点都被检索了,我只想要第一级“member”节点而不是嵌套节点。
XML myXml = new XMLDocument(stringXMLFormat);
List<XML> members = myXml.xpath("//struct/*"); // the problem is "//stuct/*" I can't find appropriate expression to query only first level of member nodes.
Instead having members.size() = 4, I have members.size() = 6.
I dont want the two nested member (Member Level 2)
<struct> +------------------------------------
<member>| Exclude this part from the result
<struct>
| <member>
+------------------------------------
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<methodResponse>
<params>
<param>
<value>
<struct> <!-- NOT THIS : I apply XPATH query on this : myXml.xpath("//struct/*") ==> Retrieve all member nodes in the document; but I just want the firs level of member nodes -->
<member> <!-- Member -> Level 1 : Must be retrive -->
<name>attribut1</name>
<value>
<int>0</int>
</value>
</member>
<member> <!--Member -> Level 1 : Must be retrive -->
<name>attribut2</name>
<value>
<struct>
<member> <!-- Member Level 2 : I don't want to retrive this -->
<name>attribut2-attr1</name>
<value>
<boolean>1</boolean>
</value>
</member>
</struct>
</value>
</member>
<member> <!--Member -> Level 1 : Must be retrive -->
<name>attribut3</name>
<value>
<array>
<data>
<value>
<struct> <!--NOT THIS -->
<member> <!--Member -> Level 2 : I don't want to retrive this -->
<name>attiribut3-attr-1</name>
<value>
<int>1</int>
</value>
</member>
</struct>
</value>
</data>
</array>
</value>
</member>
<member> <!-- Member -> Level 1 : Must be retrive -->
<name>attribut4</name>
<value>
<string>10</string>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodResponse>
我怎么能select只是一级会员WITHOUTselect编辑文档中的所有成员 ?
感谢您的帮助。
而不是
"//struct/*"
使用
"//param/value/struct/*"
UPD
说明:
您只是 selecting struct
标记元素下面的所有元素,而我的表达式将 select //param/value/struct
.
下面的所有元素
这意味着:
查找直接跟在 value
后面的 param
元素,直接跟在 struct
后面。现在找到该节点下的所有元素。
这将 select 仅相关 member
节点,因为也有内部 member
节点,但它们位于此节点序列下方://data/value/struct
.
kjhughes 在这里给出的第二个解决方案也很好。
这个 XPath,
//member[not(ancestor::member)]
将 select 所有 member
没有 member
元素祖先的元素。