xslt Filter

Discuss the spreadsheet application
Post Reply
ing
Posts: 4
Joined: Wed May 07, 2008 1:21 pm

xslt Filter

Post by ing »

Hello!

I'm trying to write a xslt filter for gps-data stored in a gpx xml file. However it doesn't work :( .
I've found the description for xslt files in this forum, but when I use the example from there (via Copy&Paste), I'll get the same error: OpenOffice calc hangs, the progress bar is at 50%.

My gpx file looks like this:

Code: Select all

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<gpx creator="Mobile Action http://www.mobileaction.com/" version="1.0" xmlns="http://www.topografix.com/GPX/1/0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
  <trk>
    <name>20080501 1753</name>
    <desc>Color:008000ff</desc>
    <trkseg>
      <trkpt lat="51.721268" lon="3.763295">
        <time>2008-05-01T15:53:24Z</time>
        <speed>0.14</speed>
      </trkpt>
      <trkpt lat="51.721298" lon="3.763353">
        <time>2008-05-01T15:54:12Z</time>
        <speed>0.07</speed>
      </trkpt>
    </trkseg>
  </trk>
</gpx>
And my filter looks like this

Code: Select all

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" office:version="1.0">
    <xsl:template match="gpx">
      <office:document>
       <office:body>
        <office:spreadsheet>
         <table:table>
          <table:table-row>
        <table:table-cell><text:p>lat</text:p></table:table-cell>
        <table:table-cell><text:p>lon</text:p></table:table-cell>
        <table:table-cell><text:p>time</text:p></table:table-cell>
        <table:table-cell><text:p>speed</text:p></table:table-cell>
          </table:table-row>
         <xsl:for-each select="trk/trkseg/trkpt">
          <table:table-row>
         <table:table-cell><text:p><xsl:value-of select="@lat"/></text:p></table:table-cell>
         <table:table-cell><text:p><xsl:value-of select="@lon"/></text:p></table:table-cell>
         <table:table-cell><text:p><xsl:value-of select="time"/></text:p></table:table-cell>
         <table:table-cell><text:p><xsl:value-of select="speed"/></text:p></table:table-cell>
          </table:table-row>
         </xsl:for-each>
        </table:table>
       </office:spreadsheet>
      </office:body>
    </office:document>
    </xsl:template>
    </xsl:stylesheet>
I'm using OpenOffice 2.4. Has someone an idea where the problem is?
eric70
Posts: 2
Joined: Sun Mar 27, 2011 11:48 pm

Re: xslt Filter

Post by eric70 »

I am is the same way!

but with my .xslt sheet I arrive to import .gpx file with a previous modification of the .gpx file.

gpx2ods.xslt

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!--Repris d'un document généré par ooo 3.0 -->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" 
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" 
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" 
xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" 
xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" 
xmlns:xlink="http://www.w3.org/1999/xlink" 
xmlns:dc="http://purl.org/dc/elements/1.1/" 
xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" 
xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" 
xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" 
xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" 
xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" 
xmlns:math="http://www.w3.org/1998/Math/MathML" 
xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" 
xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" 
xmlns:ooo="http://openoffice.org/2004/office" 
xmlns:ooow="http://openoffice.org/2004/writer" 
xmlns:oooc="http://openoffice.org/2004/calc" 
xmlns:dom="http://www.w3.org/2001/xml-events" 
xmlns:xforms="http://www.w3.org/2002/xforms" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:rpt="http://openoffice.org/2005/report" 
xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" 
xmlns:rdfa="http://docs.oasis-open.org/opendocument/meta/rdfa#" 
xmlns:field="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:field:1.0" 
xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" 
version="1.0">

<!--output trouvé dans le xsl spreadsheet xml -> ods -->
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<!--trouvé dans xls gpx2gp -->
<xsl:strip-space elements="*"/>

<!--boucle principale, réalisé une seule fois pour tout le document -->
        <xsl:template match="//gpx">		


          <office:document>
           <office:body>
            <office:spreadsheet>
             <table:table>
              <table:table-row>
            <table:table-cell office:value-type="string"><text:p>lat</text:p></table:table-cell>
            <table:table-cell office:value-type="string"><text:p>lon</text:p></table:table-cell>
            <table:table-cell office:value-type="string"><text:p>time</text:p></table:table-cell>
            <table:table-cell office:value-type="string"><text:p>speed</text:p></table:table-cell>
            <table:table-cell office:value-type="string"><text:p>elevation</text:p></table:table-cell>
              </table:table-row>

<!--appelle le template suivant à cette position car il est interdit de les encastrer -->
             <xsl:for-each select="//trkpt">
              <table:table-row>
              <table:table-cell><text:p><xsl:value-of select="@lat"/></text:p></table:table-cell>
             <table:table-cell><text:p><xsl:value-of select="@lon"/></text:p></table:table-cell>
             <table:table-cell><text:p><xsl:value-of select="time"/></text:p></table:table-cell>
             <table:table-cell><text:p><xsl:value-of select="speed"/></text:p></table:table-cell>
             <table:table-cell><text:p><xsl:value-of select="ele"/></text:p></table:table-cell>
            </table:table-row>
             </xsl:for-each>

            </table:table>
           </office:spreadsheet>
          </office:body>
        </office:document>
       </xsl:template>



</xsl:stylesheet>
Previourly, please, modify the .gpx file :

xmlns="http://www.topografix.com/GPX/1/0"
-->
_xmlns="http://www.topografix.com/GPX/1/0"

Is someone can explain us why this "xmlns=" expression blocs the xsl process?
Ooo 3.2 Linux Ubuntu 10.10 maveric
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

Re: xslt Filter

Post by hol.sten »

I gave the xslt filter and the gpx file from the first post of this thread a try on Ubuntu 10.04 using OOo 3.2.0. I cannot see any errors in both files. But, as described above, the xslt filter does not work with the given gpx file. Next try was to remove the part xmlns="http://www.topografix.com/GPX/1/0" from the gpx file's <gpx> tag. Afterwards the xslt filter worked like a charm. My last try was to download an example gpx file from http://www.gpsies.com. From that file I had to remove xmlns="http://www.topografix.com/GPX/1/1" from the gpx file's <gpx> tag, too, and additionally the <metadata> tag and all its child tags. But after these slight modifications the xslt filter worked again and imported the whole track.

I have no clue why removing xmlns="http://www.topografix.com/GPX/1/0" or xmlns="http://www.topografix.com/GPX/1/1" is always necessary, but afterwards the xslt filter does work. But at least I can confirm, that this hint from the previous post does the trick.
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
awulms
Posts: 1
Joined: Wed Feb 15, 2012 11:49 pm

Re: xslt Filter

Post by awulms »

The filter does not work with the xmlns="http://www.topografix.com/GPX/1/0" present in the gpx file, because that will make all non-prefixed elements in the gpx file go into the default namespace http://www.topografix.com/GPX/1/0.
The xslt must explicitly reference such default namespace in order to match or select its elements.

Here is a fixed version of the xslt which does exactly that (and I have fine-tuned some xpath expressions to improve the performance):

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!--Repris d'un document généré par ooo 3.0 -->
<xsl:stylesheet 
    xmlns:defaultns="http://www.topografix.com/GPX/1/1"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
    xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
    xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
    xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
    xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
    xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
    xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
    xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
    xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
    xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
    xmlns:math="http://www.w3.org/1998/Math/MathML"
    xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
    xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
    xmlns:ooo="http://openoffice.org/2004/office"
    xmlns:ooow="http://openoffice.org/2004/writer"
    xmlns:oooc="http://openoffice.org/2004/calc"
    xmlns:dom="http://www.w3.org/2001/xml-events"
    xmlns:xforms="http://www.w3.org/2002/xforms"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:rpt="http://openoffice.org/2005/report"
    xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2"
    xmlns:rdfa="http://docs.oasis-open.org/opendocument/meta/rdfa#"
    xmlns:field="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:field:1.0"
    xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0"
    version="1.0">
    <!--output trouvé dans le xsl spreadsheet xml -> ods -->
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

    <!--trouvé dans xls gpx2gp -->
    <xsl:strip-space elements="*"/>

    <!--boucle principale, réalisé une seule fois pour tout le document -->
    <xsl:template match="defaultns:gpx">
              <office:document>
               <office:body>
                <office:spreadsheet>
                 <table:table>
                  <table:table-row>
                <table:table-cell office:value-type="string"><text:p>lat</text:p></table:table-cell>
                <table:table-cell office:value-type="string"><text:p>lon</text:p></table:table-cell>
                <table:table-cell office:value-type="string"><text:p>time</text:p></table:table-cell>
                <table:table-cell office:value-type="string"><text:p>speed</text:p></table:table-cell>
                <table:table-cell office:value-type="string"><text:p>elevation</text:p></table:table-cell>
                  </table:table-row>

    <!--appelle le template suivant à cette position car il est interdit de les encastrer -->
                 <xsl:for-each select="defaultns:trk/defaultns:trkseg/defaultns:trkpt">
                  <table:table-row>
                  <table:table-cell><text:p><xsl:value-of select="@lat"/></text:p></table:table-cell>
                 <table:table-cell><text:p><xsl:value-of select="@lon"/></text:p></table:table-cell>
                 <table:table-cell><text:p><xsl:value-of select="defaultns:time"/></text:p></table:table-cell>
                 <table:table-cell><text:p><xsl:value-of select="defaultns:speed"/></text:p></table:table-cell>
                 <table:table-cell><text:p><xsl:value-of select="defaultns:ele"/></text:p></table:table-cell>
                </table:table-row>
                 </xsl:for-each>

                </table:table>
               </office:spreadsheet>
              </office:body>
            </office:document>
           </xsl:template>
</xsl:stylesheet>
LibreOffice 3.4.2 on OpenSUSE 12.1
eric70
Posts: 2
Joined: Sun Mar 27, 2011 11:48 pm

Re: xslt Filter

Post by eric70 »

Thinks a lot awulms,
I will just add the line "xmlns:defaultns="http://www.topografix.com/GPX/1/1"" and it works!
Ooo 3.2 Linux Ubuntu 10.10 maveric
balden
Posts: 1
Joined: Sat Apr 13, 2013 11:34 pm

Re: xslt Filter

Post by balden »

Thanks a lot gentlemen. This is extremely useful information.
I have been able to import a GPX file from my Mio Cyclo 305 using this XSLT as a starting point.
I must admit I know nothing about XML but after a little guesswork and looking up keywords on the web, I managed to modify the XSLT in order to obtain the numeric fields directly as numeric values, a proper date/time, and to read the "extensions" in the GPX file (heartrate, cadence sensor, wheel speed sensor, etc.).
Here it is, in case anybody has a use for it:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!--Repris d'un document généré par ooo 3.0 -->
<xsl:stylesheet
xmlns:defaultns="http://www.topografix.com/GPX/1/1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
xmlns:math="http://www.w3.org/1998/Math/MathML"
xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
xmlns:ooo="http://openoffice.org/2004/office"
xmlns:ooow="http://openoffice.org/2004/writer"
xmlns:oooc="http://openoffice.org/2004/calc"
xmlns:dom="http://www.w3.org/2001/xml-events"
xmlns:xforms="http://www.w3.org/2002/xforms"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rpt="http://openoffice.org/2005/report"
xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2"
xmlns:rdfa="http://docs.oasis-open.org/opendocument/meta/rdfa#"
xmlns:field="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:field:1.0"
xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0"
version="1.0">
<!--output trouvé dans le xsl spreadsheet xml -> ods -->
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<!--trouvé dans xls gpx2gp -->
<xsl:strip-space elements="*"/>

<!--boucle principale, réalisé une seule fois pour tout le document -->
<xsl:template match="defaultns:gpx">
<office:document>
<office:body>
<office:spreadsheet>
<style:style style:name="ce1" style:family="table-cell" style:parent-style-name="Default" style:data-style-name="N10051"/>
<table:table>
<table:table-row>
<table:table-cell office:value-type="string"><text:p>lat</text:p></table:table-cell>
<table:table-cell office:value-type="string"><text:p>lon</text:p></table:table-cell>
<table:table-cell office:value-type="string"><text:p>time</text:p></table:table-cell>
<table:table-cell office:value-type="string"><text:p>speed</text:p></table:table-cell>
<table:table-cell office:value-type="string"><text:p>elevation</text:p></table:table-cell>
<table:table-cell office:value-type="string"><text:p>heartrate</text:p></table:table-cell>
<table:table-cell office:value-type="string"><text:p>course</text:p></table:table-cell>
<table:table-cell office:value-type="string"><text:p>acceleration</text:p></table:table-cell>
<table:table-cell office:value-type="string"><text:p>cadence</text:p></table:table-cell>
</table:table-row>

<!--appelle le template suivant à cette position car il est interdit de les encastrer -->
<xsl:for-each select="defaultns:trk/defaultns:trkseg/defaultns:trkpt">
<table:table-row>

<table:table-cell office:value-type="float">
  <xsl:attribute name="office:value">
    <xsl:value-of select="@lat"/>
  </xsl:attribute>
</table:table-cell>

<table:table-cell office:value-type="float">
  <xsl:attribute name="office:value">
    <xsl:value-of select="@lon"/>
  </xsl:attribute>
</table:table-cell>

<table:table-cell office:value-type="date" table:style-name="ce1">
  <xsl:attribute name="office:date-value">
    <xsl:value-of select="defaultns:time"/>
  </xsl:attribute>
</table:table-cell>

<table:table-cell office:value-type="float">
  <xsl:attribute name="office:value">
    <xsl:value-of select="defaultns:extensions/defaultns:speed"/>
  </xsl:attribute>
</table:table-cell>

<table:table-cell office:value-type="float">
  <xsl:attribute name="office:value">
    <xsl:value-of select="defaultns:ele"/>
  </xsl:attribute>
</table:table-cell>

<table:table-cell office:value-type="float">
  <xsl:attribute name="office:value">
    <xsl:value-of select="defaultns:extensions/defaultns:heartrate"/>
  </xsl:attribute>
</table:table-cell>

<table:table-cell office:value-type="float">
  <xsl:attribute name="office:value">
    <xsl:value-of select="defaultns:extensions/defaultns:course"/>
  </xsl:attribute>
</table:table-cell>

<table:table-cell office:value-type="float">
  <xsl:attribute name="office:value">
    <xsl:value-of select="defaultns:extensions/defaultns:acceleration"/>
  </xsl:attribute>
</table:table-cell>

<table:table-cell office:value-type="float">
  <xsl:attribute name="office:value">
    <xsl:value-of select="defaultns:extensions/defaultns:cadence"/>
  </xsl:attribute>
</table:table-cell>

</table:table-row>
</xsl:for-each>

</table:table>
</office:spreadsheet>
</office:body>
</office:document>
</xsl:template>
</xsl:stylesheet>
Libre Office 4.0.2.1
OpenSuSE 12.2
Post Reply