Jump to content
Server Maintenance This Week. ×

Rusty XML/XSL user!


rp272

This topic is 3806 days old. Please don't post here. Open a new topic instead.

Recommended Posts

  • Newbies

It is almost 10 years since I last worked with XML and then only as part of a Masters module.

 

I am now retired and busy planning trips in our motorhome for which I create a list of ‘favourites’ for my navigation app.

 

I have previously done this using Excel but it is rather tedious so I looked out my copy of Filemaker Pro v9 which is brilliant for manipulating the ‘Points of Interest’ files that can be downloaded. And then export as XML and do an XSLT transfom on the fly.

 

To avoid having to hunt through a long list of sites I wish to group them and have given each record a group designator field. The data will be sorted first before export  so the XSL file will only have to cope with adding opening and closing group tags when the value of that field changes.

 

The following is an example of the output I get from FM, followed by the way I would like to transform it. I can do the straightforward transform but am a bit bogged down with adding the groups when ‘source’ changes .

 

Please can anyone help?

 

 

SOURCE XML

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

<FMPDSORESULT xmlns="http://www.filemaker.com/fmpdsoresult">

            <ERRORCODE>0</ERRORCODE>

            <DATABASE>navigator.fp7</DATABASE>

            <LAYOUT/>

            <ROW MODID="52" RECORDID="531">

                        <source>sites</source>

                        <site_name>Teversal</site_name>

                        <latitude_mas>191334960</latitude_mas>

                        <longitude_mas>-4664520</longitude_mas>

            </ROW>

            <ROW MODID="52" RECORDID="532">

                        <source>sites</source>

                        <site_name>Cambridge</site_name>

                        <latitude_mas>187792524</latitude_mas>

                        <longitude_mas>-444024</longitude_mas>

            </ROW>

            <ROW MODID="52" RECORDID="533">

                        <source>sites</source>

                        <site_name>Crystal Palace</site_name>

                        <latitude_mas>185133704</latitude_mas>

                        <longitude_mas>-265731</longitude_mas>

            </ROW>

            <ROW MODID="52" RECORDID="534">

                        <source>sites</source>

                        <site_name>Ford Farm</site_name>

                        <latitude_mas>182940480</latitude_mas>

                        <longitude_mas>-5705280</longitude_mas>

            </ROW>

            <ROW MODID="40" RECORDID="535">

                        <source>visits</source>

                        <site_name>Beaulieu </site_name>

                        <latitude_mas>185133704</latitude_mas>

                        <longitude_mas>-265731</longitude_mas>

            </ROW>

            <ROW MODID="39" RECORDID="536">

                        <source>visits</source>

                        <site_name>Basildon Park </site_name>

                        <latitude_mas>185407430</latitude_mas>

                        <longitude_mas>4027845</longitude_mas>

            </ROW>

</FMPDSORESULT>

 

 

TRANSFORMED XML

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

<favourites>

            <group name="campsites">

                        <item name="Teversal" lat="191334960" lon="-4664520"/>

                        <item name="Cambridge" lat="187792524" lon="444024"/>

                        <item name="Crystal Palace " lat="185133704" lon="-265731"/>

                        <item name="Ford Farm" lat="182940480" lon="-5705280"/>

                        <item name="Green Pastures" lat="182779200" lon="-5690520"/>

            </group>

            <group name="days out">

                        <item name="Beaulieu" lat="185133704" lon="-265731"/>

                        <item name="Basildon Park" lat="185407430" lon="4027845"/>

            </group>

</favourites>

Link to comment
Share on other sites

  • Newbies

Well they don't!

 

I thought that 'source' = sites could be transformed as campsites and = visits as 'days out'. However if this is difficult I could always change the values of 'source'.

 

It would be easy if <xsl:variable... /> actually worked as a programming variable! I'm sure I read somewhere that it was possible to compare one element value from each 'ROW' (as in the case of FMPDSORESULT)  with that of the preceding 'ROW' or is this a grouping problem?

 

Any suggestions would be appreciated

Link to comment
Share on other sites

Grouping in XSLT is not trivial. It cannot work the way you suggest, because the XSLT stylesheet itself must be a valid XML document, with properly nested elements: you cannot open a group element in one xsl::if statement and close it in another.

 

I thought that 'source' = sites could be transformed as campsites and = visits as 'days out'.

 

Well, yes they could. Are these the only two groups you will ever have? If you have a fixed (and small) list of groups, then grouping can become rather simple; otherwise it's not - see:
http://www.jenitennison.com/xslt/grouping/
 

Link to comment
Share on other sites

  • Newbies

Grouping in XSLT is not trivial. It cannot work the way you suggest, because the XSLT stylesheet itself must be a valid XML document, with properly nested elements: you cannot open a group element in one xsl::if statement and close it in another.

 

 

Well, yes they could. Are these the only two groups you will ever have? If you have a fixed (and small) list of groups, then grouping can become rather simple; otherwise it's not - see:

http://www.jenitennison.com/xslt/grouping/

 

I can see that XSLT is not trivial. Unfortunately although the list of groups is relatively small they are not necessarily fixed. I will ready Jeni Tennison's explanation and see if I can apply it to my project.

Link to comment
Share on other sites

Unfortunately although the list of groups is relatively small they are not necessarily fixed.

 

Well, you have to have a list somewhere, if you want to rename the groups.  Or will you be only renaming some of the groups? If so, you will have to do this in two steps.

 

On the bright side, you can take advantage of Xalan's distinct() extension function (I believe this works in version 9, too):

http://xml.apache.org/xalan-c/extensionslib.html#distinct

Makes it rather simpler to get the list of groups, compared to the Muenchian key method.

Link to comment
Share on other sites

Here's an example of using xalan:distinct() to group records:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fmp="http://www.filemaker.com/fmpdsoresult"
xmlns:xalan="http://xml.apache.org/xalan"
exclude-result-prefixes="xalan fmp">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
<favourites>
    <xsl:for-each select="xalan:distinct(fmp:FMPDSORESULT/fmp:ROW/fmp:source)">
    <group>
        <xsl:attribute name="name"><xsl:value-of select="current()"/></xsl:attribute>
    
        <xsl:for-each select="/fmp:FMPDSORESULT/fmp:ROW[fmp:source=current()]">
        <item>
            <xsl:attribute name="name"><xsl:value-of select="fmp:site_name"/></xsl:attribute>
            <!-- ADD MORE ATTRIBUTES HERE -->
        </item>
        </xsl:for-each>
    
    </group>
    </xsl:for-each>
</favourites>
</xsl:template>
</xsl:stylesheet>

 

Link to comment
Share on other sites

This topic is 3806 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.