Jump to content

XSLT does not get data from XML


garfield

Recommended Posts

Hi,I made a XML-like file with the program GSAK that holds a bunch of data written in XML but with the extension GPX. I want to use this file for a personal overview. I also made a XSL file that should print a list of all the name elements.I have placed the XML and the XSL file in the same directory.What am I doing wrong? If you could show me how to get the 'name' element out the file, it would be very appreciatedI followed a tutorial regarding this issue and this is what I've done so far:founds.xsl:

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><body><h2>Geocaching</h2><table border="0"><tr><th align="left">Name</th></tr><xsl:for-each select="gpx/wpt">	<tr>	  <td><xsl:value-of select="name"/></td></tr>	</xsl:for-each>	</table>  </body>  </html></xsl:template></xsl:stylesheet>

A part of founds.gpx:

<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="founds.xsl"?><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 http://www.gsak.net/xmlv1/1 http://www.gsak.net/xmlv1/1/gsak.xsd"xmlns="http://www.topografix.com/GPX/1/0"> <desc>Geocache file generated by GSAK (HasChildren)</desc> <author>GSAK</author> <email>clyde@gsak.net</email> <time>2007-11-03T15:25:49.8125000-07:00</time> <keywords>cache, geocache, groundspeak</keywords> <bounds minlat="29.889766666" minlon="-95.51086666" maxlat="51.46265" maxlon="107.72246666"/> <wpt lat="51.43895" lon="4.467517">  <time>2007-10-28T00:00:00.0000000-08:00</time>  <name>GC14BED</name>  <desc>Het laatste gekraai uit de Wildertnis!(Halloween) by Silly Walker (2,5/2)</desc>  <url>http://www.geocaching.com/seek/cache_details.aspx?wp=GC14BED</url>  <urlname>Het laatste gekraai uit de Wildertnis!(Halloween) by Silly Walker</urlname>  <sym>Geocache Found</sym>  <type>Geocache|Multi-cache|Found</type>  <gsak:wptExtension xmlns:gsak="http://www.gsak.net/xmlv1/1">   <gsak:UserFlag>false</gsak:UserFlag>   <gsak:Lock>false</gsak:Lock>   <gsak:DNF>false</gsak:DNF>   <gsak:Watch>false</gsak:Watch>   <gsak:UserData> </gsak:UserData>   <gsak:UserFound>2007-11-03</gsak:UserFound>   <gsak:FirstToFind>false</gsak:FirstToFind>   <gsak:User2> </gsak:User2>  </gsak:wptExtension>  <groundspeak:cache id="642583" available="True" archived="False" xmlns:groundspeak="http://www.groundspeak.com/cache/1/0">   <groundspeak:name>Het laatste gekraai uit de Wildertnis!(Halloween)</groundspeak:name>   <groundspeak:placed_by>Silly Walker</groundspeak:placed_by>   <groundspeak:owner id="">Silly Walker</groundspeak:owner>   <groundspeak:type>Multi-cache</groundspeak:type>   <groundspeak:container>Regular</groundspeak:container>   <groundspeak:difficulty>2.5</groundspeak:difficulty>   <groundspeak:terrain>2</groundspeak:terrain>   <groundspeak:country>Belgium</groundspeak:country>   <groundspeak:state>Antwerpen</groundspeak:state>   <groundspeak:short_description html="False"></groundspeak:short_description>   <groundspeak:long_description html="False"></groundspeak:long_description>	<groundspeak:encoded_hints></groundspeak:encoded_hints>   <groundspeak:logs>  </groundspeak:logs>   <groundspeak:travelbugs>   </groundspeak:travelbugs></groundspeak:cache></wpt></gpx>

Link to comment
Share on other sites

The default namespace in your XML is

http://www.topografix.com/GPX/1/0

but in your XSLT, you don't explicitly declare it, so XSLT assumes the element it's looking for is in a null namespace.You need to explicitly declare a prefix in your XSLT pointing to that namespace URI, and use that prefix in your expressions i.e.

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:gpx="http://www.topografix.com/GPX/1/0"><xsl:template match="/"><html><body><h2>Geocaching</h2><table border="0"><tr><th align="left">Name</th></tr><xsl:for-each select="gpx:gpx/gpx:wpt">	<tr>	  <td><xsl:value-of select="gpx:name"/></td></tr>	</xsl:for-each>	</table>  </body>  </html></xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

First I tried your example, but still got errors, than I put xmlns:gsak="http://www.gsak.net/xmlv1/1" in header and got no errors, but also no data ... for the field Date, the Name field was properly filled in.I also removed the gpx: in front of everything because it wasn't working. Is the xmlns:gpx="http://www.topografix.com/GPX/1/0" namespace still obligated or could I run it without? (There is no element that had gpx: in front of it)this is what I have without errors (and data for Date field)

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:gpx="http://www.topografix.com/GPX/1/0"xmlns:gsak="http://www.gsak.net/xmlv1/1"><xsl:template match="/"><html><body><h2>Geocaching</h2><table border="0"><tr><th align="left">Name</th><th align="left">Date</th></tr><xsl:for-each select="gpx/wpt">	<tr>	  <td><xsl:value-of select="name"/></td>	  <td><xsl:element name="gsak:UserFound"/></td></tr>	</xsl:for-each>	</table>  </body>  </html></xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

It doesn't matter what you had in the document. All that matters is the namespace URI to which your elements are resolved to, so yes, the use of the "gpx" prefix is mandatory in your XSLT file, though you could use any other prefix as well, as long as it still matches this URI.See this topic for a detailed discussion on that. In it's middle, you'll also notice a stylesheet that will tell you the namespaces of each node in your document.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...