0

我有以下 XML:

<?xml version="1.0" encoding="UTF-8"?>

<gbXML version="0.37" useSIUnitsForResults="true" volumeUnit="CubicMeters" areaUnit="SquareMeters" lengthUnit="Meters" temperatureUnit="C" xmlns="http://www.gbxml.org/schema" xsi:schemaLocation="http://www.gbxml.org/schema xsi.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Campus id="cmps-1">
<Location>
<Name>Trois-Rivieres, PQ Canada</Name>
<Latitude>46.210000</Latitude>
<Longitude>-72.350000</Longitude>
</Location>
</Campus>
</gbXML>

我想选择“纬度”值。我正在使用以下 XPath 表达式,它不返回任何内容:

<p><code><xsl:value-of select="gbXML/Campus/Location/Latitude"/></code></p>

我是 XPath 的新手,如果有人能帮我解决这个问题,我将不胜感激

4

2 回答 2

0

您有一个默认命名空间xmlns="http://www.gbxml.org/schema"

在您的 XSLT 中,只需添加另一个名称空间,例如xmlns:text="xmlns="http://www.gbxml.org/schema"(您正在创建默认名称空间的快捷方式)。

还要添加exclude-result-prefixes="text"以省略text输出中的前缀。

然后,改变:

<p><code><xsl:value-of select="gbXML/Campus/Location/Latitude"/></code></p>

<p><code><xsl:value-of select="text:gbXML/text:Campus/text:Location/text:Latitude"/></code></p>
于 2014-02-06T07:22:56.573 回答
0

您缺少 xpath 中的命名空间声明。在样式表中声明命名空间,然后在 xpath 中使用前缀:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:gb="http://www.gbxml.org/schema">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
    <p><code><xsl:value-of select="gb:gbXML/gb:Campus/gb:Location/gb:Latitude"/></code></p>
</xsl:template>

</xsl:stylesheet> 
于 2014-02-06T07:23:18.303 回答