如何找到嵌套在两个不同标签中的两个同名节点的位置?
我有以下 XML:
<employees>
<employee> <!--forgot to include an attribute-->
<name>John</name>
<jobs>
<job>Writer</job>
<job>Artist</job>
</jobs>
</employee>
<employee>
<name>John</name>
<jobs>
<job>Engineer</job>
<job>Editor</job>
</jobs>
</employee>
</employees>
如果我想获取 name="John" 的人员的工作,XPath 返回属于一个“John”的所有四个工作。我想要 2+2 种不同的工作,分别由两个不同的“John”完成。
我使用 XPath 表达式
"//employees/employee[name='John']/jobs/job/text()"
java 中的 XPath 有没有办法使用 count 或其他一些函数来执行此操作?
I have the following XML:
<employees>
<employee> <!--forgot to include an attribute-->
<name>John</name>
<jobs>
<job>Writer</job>
<job>Artist</job>
</jobs>
</employee>
<employee>
<name>John</name>
<jobs>
<job>Engineer</job>
<job>Editor</job>
</jobs>
</employee>
</employees>
If I want to get the jobs of people with name="John" XPath returns all four jobs belonging to one "John". I want 2+2 different jobs done by two different "John"s each.
I use the XPath expression
"//employees/employee[name='John']/jobs/job/text()"
Is there a way in XPath in java using count or some other function to do this??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XPath(仅)是 XML 文档的查询语言——XPath 表达式的评估结果永远不会是修改的节点——XPath 本身不会更改任何节点的结构和/或内容。
您想要返回的是修改后的
元素,仅包含其
子元素,而仅使用 XPath 无法实现这一点。最接近您想要的结果是:
这选择以下内容:
我猜您想要的结果可以通过此 XSLT 转换绝对轻松地生成 strong>:
当将此转换应用于提供的 XML 文档时:
生成所需的正确结果:
XPath is (only) a query language for XML documents -- the result of evaluation an XPath expression is never a modified node -- XPath alone doesn't change the structure and/or contents of any node.
What you want returned is modified
<employee>
elements with only their<jobs>
child and this cannot be achieved with XPath alone.The closest to what you want is:
This selects the following:
What I guess is the result wanted by you can be produced absolutely easy with this XSLT transformation:
when this transformation is applied to the provided XML document:
the wanted, correct result is produced:
无论节点在哪里找到,XPath 总是会返回一个平面列表,这就是为什么您只得到 4 段文本的原因。如果要按其父节点对它们进行分组,则需要首先搜索 John,并使用这些结果执行嵌套循环 - 对于每个 John,从该节点开始为作业执行 XPath。您希望如何在 Java 中对它们进行分组取决于您 - 也许是字符串(名称)到字符串列表(作业)的映射。
XPath is always going to return a flat list, no matter where the nodes are found, which is why you simply get 4 pieces of text. If you want to group them by their parent nodes, you need to first search for the Johns and with those results, do a nested loop - for each John, do an XPath for the jobs, starting at that node. How you wish to group them in Java is up to you - perhaps a Map of String (name) to List of String (job).