Domanda

I have following 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>

I want to select the "Latitude" value. I am using the below XPath expression which is not returning anything:

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

I am new in XPath, I will really appreciate it if someone could help me with this

È stato utile?

Soluzione

You have a default namespace xmlns="http://www.gbxml.org/schema".

In your XSLT, just add another namespace, say xmlns:text="xmlns="http://www.gbxml.org/schema" (you are creating a shortcut of the default namespace).

Add also exclude-result-prefixes="text" to omit the text prefix in the output.

Then, change:

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

to

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

Altri suggerimenti

You are missing the namespace declaration in xpath. Declare the namespace in the stylesheet and then use the prefix in the 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> 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top