为什么此XPath表达式不能在xmlstarlet中返回正确的值? [英] Why does this XPath expression not return the correct value in xmlstarlet?

查看:138
本文介绍了为什么此XPath表达式不能在xmlstarlet中返回正确的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用xmlstarlet提取此KML文件中坐标"节点的内容.

I'm trying to extract the contents of the 'coordinates' node in this KML file using xmlstarlet.

使用xmlstarlet本身可以很好地验证KML文件.

The KML file validates fine using xmlstarlet itself.

我将其缩减为一个包含以下内容的小型测试文件:

I've whittled it down to a small test file containing:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<Placemark>
    <name>eurovelo-5 690</name>
    <Snippet></Snippet>
    <description><![CDATA[&nbsp;]]></description>
    <styleUrl>#style390</styleUrl>
    <LineString>
      <tessellate>1</tessellate>
      <coordinates>
        10.146948,44.790592,97.500000
        10.146958,44.790562,97.599998
        10.147018,44.790497,97.699997
        10.147083,44.790466,97.699997
      </coordinates>
    </LineString>
  </Placemark>
  </Document>
</kml>

运行此查询失败,但是:

Running this query fails though:

xmlstarlet sel -t -c "//coordinates/text()" test.kml

这似乎可以使用在线路径工具正确解析- http://www.qutoric .com/xslt/analyser/xpathtool.html

This appears to parse correctly using the online path tool - http://www.qutoric.com/xslt/analyser/xpathtool.html

我在这里想念东西吗?

推荐答案

您需要为http://earth.google.com/kml/2.2 test.kml定义并使用名称空间前缀-类似于这样:

You need to define and use a namespace prefix for http://earth.google.com/kml/2.2 test.kml - something like this:

xmlstarlet sel -t -c "//kml:coordinates/text()" -N kml=http://earth.google.com/kml/2.2 test.kml

XPath没有默认的名称空间-如果XPath中的名称未指定名称空间前缀,则假定该名称位于空名称空间中;否则,将使用默认名称空间.因此,在尝试匹配名称与空名称不同的名称空间中的节点时(在这种情况下),必须始终指定一个名称空间前缀.

XPaths do not have a default namespace - if a name in a XPath does not specify a namespace prefix it is assumed to be in the null namespace; hence it is necessary to always specify a namespace prefix when trying to match nodes whose names are in a namespace different from the null one (as in this case).

这篇关于为什么此XPath表达式不能在xmlstarlet中返回正确的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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