Xpath:过滤掉孩子 [英] Xpath: filter out childs

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

问题描述

我正在寻找一个xpath表达式来过滤掉某些孩子。



来源:

 < AAA> 
< BBB1>
< CCC> A< / CCC>
< / BBB1>
< BBB2>
< CCC> A< / CCC>
< / BBB2>
< BBB3>
< CCC> B< / CCC>
< / BBB3>
< BBB4>
< CCC> B< / CCC>
< / BBB4>
< / AAA>

这应该是结果:

  
< AAA>
< BBB3>
< CCC> B< / CCC>
< / BBB3>
< BBB4>
< CCC> B< / CCC>
< / BBB4>
< / AAA>

希望有人能帮助我。 $ b

Jos

解决方案

XPath是XML文档的查询语言。因此,它只能从现有的XML文档中选择节点 - 不能修改XML文档或创建新的XML文档



使用XSLT来转换XML文档,并从中创建一个新的XML文档。

特殊情况

 < xsl:stylesheet version =1.0
xmlns:xsl = http://www.w3.org/1999/XSL/Transform\">
< xsl:output omit-xml-declaration =yesindent =yes/>
< xsl:strip-space elements =*/>

< xsl:template match =node()| @ *>
< xsl:copy>
< xsl:apply-templates select =node()| @ */>
< / xsl:copy>
< / xsl:template>

< xsl:template match =/ * / * [not(CCC ='B')]/>
< / xsl:stylesheet>

在所提供的XML文档上应用此转换时:

 < AAA> 
< BBB1>
< CCC> A< / CCC>
< / BBB1>
< BBB2>
< CCC> A< / CCC>
< / BBB2>
< BBB3>
< CCC> B< / CCC>
< / BBB3>
< BBB4>
< CCC> B< / CCC>
< / BBB4>
< / AAA>

想要的,正确的结果会产生:
$ b

 < AAA> 
< BBB3>
< CCC> B< / CCC>
< / BBB3>
< BBB4>
< CCC> B< / CCC>
< / BBB4>
< / AAA>


I'm looking for a xpath expression that filters out certain childs. A child must contain a CCC node with B in it.

Source:

<AAA>
    <BBB1>
        <CCC>A</CCC>
    </BBB1>       
    <BBB2>
        <CCC>A</CCC>
    </BBB2>
    <BBB3>
        <CCC>B</CCC>
    </BBB3>
    <BBB4>
        <CCC>B</CCC>
    </BBB4>
</AAA>

This should be the result:


<AAA>
    <BBB3>
        <CCC>B</CCC>
    </BBB3>
    <BBB4>
        <CCC>B</CCC>
    </BBB4>
</AAA>

Hopefully someone can help me.

Jos

解决方案

XPath is a query language for XML documents. As such it can only select nodes from existing XML document(s) -- it cannot modify an XML document or create a new XML document.

Use XSLT in order to transform an XML document and create a new XML document from it.

In this particular case:

<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="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="/*/*[not(CCC = 'B')]"/>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

<AAA>
    <BBB1>
        <CCC>A</CCC>
    </BBB1>
    <BBB2>
        <CCC>A</CCC>
    </BBB2>
    <BBB3>
        <CCC>B</CCC>
    </BBB3>
    <BBB4>
        <CCC>B</CCC>
    </BBB4>
</AAA>

the wanted, correct result is produced:

<AAA>
   <BBB3>
      <CCC>B</CCC>
   </BBB3>
   <BBB4>
      <CCC>B</CCC>
   </BBB4>
</AAA>

这篇关于Xpath:过滤掉孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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