Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml XPath排除节点及其子节点_Xml_Xpath - Fatal编程技术网

Xml XPath排除节点及其子节点

Xml XPath排除节点及其子节点,xml,xpath,Xml,Xpath,我有这样一个XML: <Root xmlns:test1="http://www.test1.com" xmlns:test2="http://www.test2.com" Attr="root"> <test1:Child1 Attribute1="c1" Bttribute="c2" Cttribute="c3"> <child11 Attribute11="c11">Element11</child11> </test1

我有这样一个XML:

<Root xmlns:test1="http://www.test1.com" xmlns:test2="http://www.test2.com" Attr="root">
  <test1:Child1 Attribute1="c1" Bttribute="c2" Cttribute="c3">
    <child11 Attribute11="c11">Element11</child11>
  </test1:Child1>
  <test2:Child2 Attribute2="c2">
    <child21 Attribute21="c21">
        <child211 />
        <child212 />
        <child213 />
    </child21>
    <child22 Attribute22="c22">Element22</child22>
  </test2:Child2>
  <test2:Child3 Attribute3="c3">
    <child31>Element31</child31>
  </test2:Child3>
</Root>
这方面的xpath代码是什么


非常感谢XPath排除儿童21

/Root/*/*[not(local-name()='child21')]
结果如下所示

child11
child22
child31

根据您的要求修改此选项。

XPath从不修改它选择的节点,它只是选择它们。如果您的选择包括(比如)根元素,那么当序列化时,它将包括输入文档的所有元素,即使您只选择了单个元素

您可以通过以下方式迭代祖先轴或自轴中没有child21的所有元素

//*[not(ancestor-or-self::child21)]
但是,如果您想要生成显示的结果,那么这并不是很有用

使用xslt过滤掉该元素及其子元素是很简单的,只需拥有一个标识模板并添加一个模板即可

<xsl:template match="child21"/>


这会丢弃输入的该分支,但不能仅使用XPath来实现。

XPath会选择节点,但如果您想实际复制XML跳过某些元素,则需要XSLT,这将生成您想要的输出:

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- Template to copy everything-->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- Template to skip 'child21' elements -->
  <xsl:template match="child21">
  </xsl:template>

</xsl:stylesheet>

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- Template to copy everything-->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- Template to skip 'child21' elements -->
  <xsl:template match="child21">
  </xsl:template>

</xsl:stylesheet>