Having this (simplified) XML:
有这个(简化的)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>
How can I select the name and coordinates of each Placemark? Right now I can select their name with the following XPath expression:
如何选择每个Placemark的名称和坐标?现在我可以用以下XPath表达式选择它们的名称:
//Document//Placemark//name
How can I select both without any other data?
我如何在没有其他数据的情况下选择它们?
3 个解决方案
#1
38
You can use a union in your XPath expression. Just use the operator: |
可以在XPath表达式中使用联合。使用运算符:|
//Document/Placemark/name | //Document/Placemark/Polygon/coordinates
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.
如果不需要,不要使用//(后代轴)。使用//,这也可以工作://命名| //坐标。最好在性能上指定确切的路径。
#2
5
Use:
使用:
/*/*/Placemark/name | /*/*/Placemark/*/coordinates
This specifies the union of the results of two separate XPath expressions -- the standard XPath union operator |
is used. Selected are all name
elements that are children of a Placemark
element that is a grandchild of the top element of the XML document, plus all coordinates
elements that are grand-children of a Placemark
element that is a grandchild of the top element of the XML document.
它指定两个单独XPath表达式的结果的联合——使用标准XPath联合运算符|。所选的所有名称元素都是Placemark元素的子元素,它是XML文档顶部元素的外孙女,加上所有的坐标元素,这些元素都是Placemark元素的后代元素,它们是XML文档顶部元素的外孙女。
The selected elements come in document order (although no normative W3C document specifies the order), which means that in the result of the evaluation (usually of type XmlNodeList) any name
element is directly followed by its corresponding coordinates
element.
所选的元素按文档顺序排列(尽管没有标准的W3C文档指定顺序),这意味着在计算结果(通常是XmlNodeList类型)中,任何名称元素都直接跟随相应的坐标元素。
#3
0
Resolved: //Placemark/*[self::name or descendant::coordinates]
解决:/ /地标/ *[自我::名称或后代:坐标):
#1
38
You can use a union in your XPath expression. Just use the operator: |
可以在XPath表达式中使用联合。使用运算符:|
//Document/Placemark/name | //Document/Placemark/Polygon/coordinates
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.
如果不需要,不要使用//(后代轴)。使用//,这也可以工作://命名| //坐标。最好在性能上指定确切的路径。
#2
5
Use:
使用:
/*/*/Placemark/name | /*/*/Placemark/*/coordinates
This specifies the union of the results of two separate XPath expressions -- the standard XPath union operator |
is used. Selected are all name
elements that are children of a Placemark
element that is a grandchild of the top element of the XML document, plus all coordinates
elements that are grand-children of a Placemark
element that is a grandchild of the top element of the XML document.
它指定两个单独XPath表达式的结果的联合——使用标准XPath联合运算符|。所选的所有名称元素都是Placemark元素的子元素,它是XML文档顶部元素的外孙女,加上所有的坐标元素,这些元素都是Placemark元素的后代元素,它们是XML文档顶部元素的外孙女。
The selected elements come in document order (although no normative W3C document specifies the order), which means that in the result of the evaluation (usually of type XmlNodeList) any name
element is directly followed by its corresponding coordinates
element.
所选的元素按文档顺序排列(尽管没有标准的W3C文档指定顺序),这意味着在计算结果(通常是XmlNodeList类型)中,任何名称元素都直接跟随相应的坐标元素。
#3
0
Resolved: //Placemark/*[self::name or descendant::coordinates]
解决:/ /地标/ *[自我::名称或后代:坐标):