如何选择不同级别的多个节点? [英] How to select multiple nodes in different levels?

查看:33
本文介绍了如何选择不同级别的多个节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拥有这个(简化的)XML:

Having this (simplified) XML:

<?xml version="1.0" encoding="UTF-8"?>
<kml>
<Document>
        <Placemark>
            <name>Poly 1</name>
            <Polygon>
                        <coordinates>
                            -58.40844625779582,-34.60295278618136,0
                        </coordinates>
            </Polygon>
        </Placemark>
        <Placemark>
            <name>Poly 2</name>
            <Polygon>
                        <coordinates>
                            -58.40414334150432,-34.59992445476809,0
                        </coordinates>
            </Polygon>
        </Placemark>
</Document>
</kml>

如何选择每个地标的名称和坐标?现在我可以使用以下 XPath 表达式选择他们的名字:

How can I select the name and coordinates of each Placemark? Right now I can select their name with the following XPath expression:

//Document//Placemark//name

如何在没有任何其他数据的情况下选择两者?

How can I select both without any other data?

推荐答案

您可以在 XPath 表达式中使用联合.只需使用运算符:|

You can use a union in your XPath expression. Just use the operator: |

//Document/Placemark/name | //Document/Placemark/Polygon/coordinates

如果不需要,请不要使用 //(后代 轴).使用 //,这也可以: //name |//坐标.指定确切路径在性能方面会更好.

Don't use the // (descendant axis) if you don't need to. Using //, this would also work: //name | //coordinates. It's better performance-wise to specify the exact path.

这篇关于如何选择不同级别的多个节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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