具有命名空间的 GPX 文件的 XPath 查询? [英] XPath query for GPX files with namespaces?

查看:40
本文介绍了具有命名空间的 GPX 文件的 XPath 查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够通过 xpath 表达式访问节点 当 GPX 文件具有以下简单结构时:

I am able to access the<trkpt></trkpt>nodes by the xpath expression<xsl:for-each select='gpx/trk/trkseg/trkpt'> when the GPX file has the following simple structure:

<gpx>
  <trk>
    <trkseg>
      <trkpt lat="50.5324906" lon="7.0842605">
        <ele>105.8824463</ele>
        <time>2010-07-11T08:50:16Z</time>
      </trkpt>
      <trkpt lat="50.5323745" lon="7.0843524">
        <ele>108.7662354</ele>
        <time>2010-07-11T08:50:44Z</time>
      </trkpt>
      ...
    </trkseg>
  </trk>
</gpx>

当涉及到命名空间时,我怎样才能达到同样的效果,例如:

How can i achieve the same effect when namespaces are involved, e.g.:

<gpx xmlns="http://www.topografix.com/GPX/1/1" 
     creator="MapSource 6.15.11" 
     version="1.1" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.topografix.com/GPX/1/1 
                         http://www.topografix.com/GPX/1/1/gpx.xsd">

推荐答案

在 XSLT 1.0 中:

In XSLT 1.0:

<xsl:apply-templates 
    select="/g:gpx/g:trk/g:trkseg/g:trkpt"
    xmlns:g="http://www.topografix.com/GPX/1/1"/> 

在 XSLT 2.0 中:

In XSLT 2.0:

<xsl:apply-templates 
    select="/gpx/trk/trkseg/trkpt" 
    xpath-default-namespace="http://www.topografix.com/GPX/1/1"/> 

因此,您需要在样式表中声明命名空间(前缀、URI),并将此命名空间添加到 XPath 表达式的 QName 测试中.

So, you need to declare the namespace (prefix, URI) in your stylesheet and add this namespace in your QName test of XPath expression.

例如,这个 XSLT 1.0 样式表:

As example, this XSLT 1.0 stylesheet:

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:g="http://www.topografix.com/GPX/1/1">
    <xsl:output method="text"/>
    <xsl:template match="g:trkpt">
       <xsl:text>Found 'trkseg' element&#xA;</xsl:text>
    </xsl:template>
</xsl:stylesheet>

还有这个 XSLT 2.0 样式表:

And this XSLT 2.0 stylesheet:

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xpath-default-namespace="http://www.topografix.com/GPX/1/1">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="trkpt">
        <xsl:text>Found 'trkseg' element&#xA;</xsl:text>
    </xsl:template>
</xsl:stylesheet>

使用此输入:

<gpx xmlns="http://www.topografix.com/GPX/1/1"
     creator="MapSource 6.15.11"
     version="1.1"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.topografix.com/GPX/1/1
                         http://www.topografix.com/GPX/1/1/gpx.xsd">
    <trk>
        <trkseg>
            <trkpt lat="50.5324906" lon="7.0842605">
                <ele>105.8824463</ele>
                <time>2010-07-11T08:50:16Z</time>
            </trkpt>
            <trkpt lat="50.5323745" lon="7.0843524">
                <ele>108.7662354</ele>
                <time>2010-07-11T08:50:44Z</time>
            </trkpt>
        </trkseg>
    </trk>
</gpx>

两个输出:

Found 'trkseg' element
Found 'trkseg' element

这篇关于具有命名空间的 GPX 文件的 XPath 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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