【问题标题】:Processing a GPX file with xsl (probably a namespace issue)使用 xsl 处理 GPX 文件(可能是命名空间问题)
【发布时间】:2013-07-18 03:23:44
【问题描述】:

这个问题看起来像XPath query for GPX files with namespaces? 的重复,但我一定遗漏了一些东西,因为我似乎无法获得一个相当简单的样式表来工作。我有这个输入:

<?xml version="1.0" encoding="utf-8"?>
<gpx xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" creator="Groundspeak Pocket Query" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.groundspeak.com/cache/1/0 http://www.groundspeak.com/cache/1/0/cache.xsd" xmlns="http://www.topografix.com/GPX/1/0">
  <name>Ottawa Pocket Query</name>
  <wpt lat="45.348517" lon="-75.825933">
    <name>GC3HXAZ</name>
    <desc>Craft maker box by FishDetective, Traditional Cache (2/2.5)</desc>
    <url>http://www.geocaching.com/seek/cache_details.aspx?guid=e86ce3f5-9e75-48a6-b47e-9415101fc658</url>
    <groundspeak:cache id="2893138" available="True" archived="False" xmlns:groundspeak="http://www.groundspeak.com/cache/1/0">
      <groundspeak:name>Craft maker box</groundspeak:name>
      <groundspeak:difficulty>2</groundspeak:difficulty>
      <groundspeak:terrain>2.5</groundspeak:terrain>
    </groundspeak:cache>
  </wpt>
</gpx>

还有一个看起来像这样的样式表:

<?xml version="1.0"?>
<!--  -->
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:groundspeak="http://www.groundspeak.com/cache/1/0"
        >

    <xsl:output method="html"/>

    <xsl:template match="/">
    Cache names:
        <xsl:apply-templates select="//wpt">
    </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="wpt">
    <li><xsl:value-of select="groundspeak:cache/groundspeak:name"/></li>
    </xsl:template>

</xsl:stylesheet>

而我期望是一个包含一个元素的列表,“Craft Maker Box”,但我得到的是一个空列表。

我错过了什么?

【问题讨论】:

    标签: xslt xslt-1.0


    【解决方案1】:

    默认命名空间是http://www.topografix.com/GPX/1/0。您应该添加它并使用前缀来匹配wpt

    类似这样的:(未经测试)

    <xsl:stylesheet version="1.0"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:groundspeak="http://www.groundspeak.com/cache/1/0"
            xmlns:gpx="http://www.topografix.com/GPX/1/0"
            >
    
        <xsl:output method="html"/>
    
        <xsl:template match="/">
        Cache names:
            <xsl:apply-templates select="//gpx:wpt">
        </xsl:apply-templates>
        </xsl:template>
    
        <xsl:template match="gpx:wpt">
        <li><xsl:value-of select="groundspeak:cache/groundspeak:name"/></li>
        </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      【解决方案2】:

      这确实是一个命名空间问题。你有

      xmlns="http://www.topografix.com/GPX/1/0"
      

      在 XML 中,因此无前缀元素名称位于此命名空间中。您需要将相同的 uri 绑定到样式表中的前缀,例如

      xmlns:g="http://www.topografix.com/GPX/1/0"
      

      然后在匹配和选择表达式中使用g:wpt

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多