Xpath:选择直接子元素 [英] Xpath: Select Direct Child Elements

查看:30
本文介绍了Xpath:选择直接子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的 XML 文档:

I have an XML Document like the following:

<parent>
<child1>
  <data1>some data</data1>
</child1>
<child2>
  <data2>some data</data2>
</child2>
<child3>
  <data3>some data</data3>
</child3>
</parent>

我希望能够获得父级(或我指定的元素)的直接子级,以便我拥有 child1、child2 和 child3 节点.

I would like to be able to get the direct children of parent (or the element I specify) so that I would have child1, child2 and child3 nodes.

可能吗?

推荐答案

甚至:

/*/*

这将选择所有元素 - XML 文档的顶部元素(在您的情况下名为 parent)的子元素.

this selects all element - children of the top element (in your case named parent) of the XML document.

基于 XSLT 的验证:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
  <xsl:copy-of select="/*/*"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<parent>
    <child1>
        <data1>some data</data1>
    </child1>
    <child2>
        <data2>some data</data2>
    </child2>
    <child3>
        <data3>some data</data3>
    </child3>
</parent>

计算 XPath 表达式并输出所选节点:

<child1>
   <data1>some data</data1>
</child1>
<child2>
   <data2>some data</data2>
</child2>
<child3>
   <data3>some data</data3>
</child3>

这篇关于Xpath:选择直接子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆