xslt - 使用 xsl 处理 GPX 文件(可能是命名空间问题)

标签 xslt xslt-1.0

这个问题看起来像 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”,但我得到的是一个空列表。

我错过了什么?

最佳答案

默认命名空间为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>

关于xslt - 使用 xsl 处理 GPX 文件(可能是命名空间问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17708263/

相关文章:

html - 使用 XSLT 将斜体/粗体格式设置为 HTML

XSLT 删除属于命名空间的标签/属性

xml - 用于多个字符串的 XPath/XSLT contains()

xslt - 如何处理链接到另一个xml的xml,并在给定元素下复制整个xml结构

xml - 如何使用 Perl 和 XSLT 生成 XML?

java - 如何通过搜索某些节点并替换值来转换 xml 文件

xml - xsl :for-each Getting the current() node attribute

xml - 每个节点的最大不同子元素数

xml - 无法在 XSL-FO 中设置初始页码

xpath - 使用XSLT从XHTML删除评论