如何筛选XML中符合特定属性与子元素条件的<children>元素
XML元素筛选解决方案
原始XML内容
<children xmi:type="notation:General" xmi:id="1"> <children xmi:type="notation:Shape" xmi:id="2"> <element xmi:type="uml:Package" href="krZQ"/> </children> <children xmi:type="notation:Shape" xmi:id="3"> <element xmi:type="uml:Package" href="wtFm"/> </children> <children xmi:type="notation:Shape" xmi:id="4"/> </children>
需求1:筛选带指定属性且包含<element>子元素的<children>
用XPath表达式就能精准匹配:
//children[@xmi:type='notation:Shape' and element]
匹配结果:
<children xmi:type="notation:Shape" xmi:id="2"> <element xmi:type="uml:Package" href="krZQ"/> </children> <children xmi:type="notation:Shape" xmi:id="3"> <element xmi:type="uml:Package" href="wtFm"/> </children>
需求2:根据<element>的href属性筛选特定<children>
如果要匹配指定href值(比如href="wtFm"),用以下XPath表达式:
//children[@xmi:type='notation:Shape' and element/@href='wtFm']
匹配结果:
<children xmi:type="notation:Shape" xmi:id="3"> <element xmi:type="uml:Package" href="wtFm"/> </children>
内容的提问来源于stack exchange,提问作者KeepGoing
相关产品推荐
相关产品推荐

