Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

transforming XML (formerly KML) using XSLT

Tags:

xml

xslt

kml

Hi I'm trying to use the w3schools XSLT Tryit editor to transform a KML file (saved as an XML file), but I can't seem to get it working. Here is a snippet of my XML file:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
 <Document>
  <name>Bnsf RR cut</name>
  <open>1</open>
  <Style>
   <ListStyle>
    <ItemIcon>
     <href>kmzicon.png</href>
    </ItemIcon>
   </ListStyle>
  </Style>
  <Folder>
   <name>11/10/11 8:17:20 AM</name>
   <Placemark>
    <name>Track</name>
   </Placemark>
  </Folder>
  <Placemark>
   <name>Gray Mesa</name>
   <description><![CDATA[<img width="800" src="1.jpg"/>]]></description>
   <Point>
    <coordinates>-106.493097,34.446357,1692.000000</coordinates>
   </Point>
  </Placemark>
 </Document>
</kml>

And my XSLT:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
      <xsl:for-each select="Document/Placemark">
        <xsl:value-of select="name"/>
        <xsl:value-of select="description"/>
      </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

If I remove the kml tags in the XML code it works fine, but I would rather try and automate this and not have to delete code from numerous XMLs. I've tried adding "kml" to my XSLT code "/kml/Document/Placemark"> but that is not working. Thank you!

What I would like my transformed XML to look like:

 <Document> 
  <Placemark>
   <name>Gray Mesa</name>
   <description><![CDATA[<img width="800" src="1.jpg"/>]]></description>
   <Point>
    <coordinates>-106.493097,34.446357,1692.000000</coordinates>
   </Point>
  </Placemark>
 </Document>

I think this will work for my purpose.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:kml="http://www.opengis.net/kml/2.2">
   <xsl:template match="/">
  <xsl:for-each select="kml:kml/kml:Document/kml:Placemark">
     <name><xsl:value-of disable-output-escaping="yes" select="kml:name"/></name>
     <description><xsl:value-of disable-output-escaping="yes" select="kml:description"/></description>
  </xsl:for-each>
 </xsl:template>

Each name and description tag includes this however: xmlns:kml="http://www.opengis.net/kml/2.2" I can just concatenate that with an Access query though. I do get an error when I try to import into Access though. It says I need a root folder since I only have numerous name and description tags. Is there any way to add a tag using the XSL. Thank you very much. Sorry for the messy question that I keep editing.

like image 884
Andrew Avatar asked Apr 17 '12 16:04

Andrew


People also ask

How convert XML to XSLT?

Execute an XSLT transformation from an XML fileOpen an XML document in the XML editor. Associate an XSLT style sheet with the XML document. Add an xml-stylesheet processing instruction to the XML document.

What is XML explain the transformation of XML to XSLT with example?

XSLT is a language for transforming XML documents into another XML documents. XSL includes an XML vocabulary for specifying formatting. XSL specifies the styling of an XML document by using XSLT to describe how the document is transformed into another XML document that uses the formatting vocabulary.

Is there any benefit of converting XML to XSLT?

XSLT is commonly used to convert XML to HTML, but can also be used to transform XML documents that comply with one XML schema into documents that comply with another schema. XSLT can also be used to convert XML data into unrelated formats, like comma-delimited text or formatting languages such as troff.

Is XML and XSLT the same?

The Extensible Stylesheet Language Transformation (XSLT) standard specifies a language definition for XML data transformations. XSLT is used to transform XML documents into XHTML documents, or into other XML documents.


1 Answers

Tim C is correct about the namespaces, but I'd like to add that XSLT is declarative language so usually when I see for-each in stylesheets it can be replace by something more fitting the language. So I would using the following code.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:kml="http://www.opengis.net/kml/2.2">
  <xsl:output method="text"/>

   <xsl:template match="/">
      <xsl:apply-templates select="kml:kml/kml:Document/kml:Placemark"/>
   </xsl:template>

   <xsl:template match="kml:Placemark">
         <xsl:value-of select="kml:name"/>
         <xsl:value-of select="kml:description"/>
   </xsl:template>

</xsl:stylesheet>
like image 165
BeWarned Avatar answered Oct 04 '22 22:10

BeWarned