I'm trying to group records from a filemaker database. I'm exporting as XML with the option of using XSLT to transform.
I've been doing some search and reading the other posts, and I don't think they cover exactly what I want to do.
An excerpt of the XML:
<?xml version="1.0" encoding="UTF-8"?>
<!-- This grammar has been deprecated - use FMPXMLRESULT instead -->
<FMPDSORESULT xmlns="http://www.filemaker.com/fmpdsoresult">
<ERRORCODE>0</ERRORCODE>
<DATABASE>Artpostersnbbs.fp7</DATABASE>
<LAYOUT />
<ROW MODID="19" RECORDID="11116">
<Art_Type>Poster</Art_Type>
<Location>1</Location>
<Line1>ELEVATOR
MACHINE
ROOM
107</Line1>
</ROW>
<ROW MODID="19" RECORDID="11116">
<Art_Type>Poster</Art_Type>
<Location>2</Location>
<Line1>ELEVATOR
MACHINE
ROOM
107</Line1>
</ROW>
<ROW MODID="19" RECORDID="11116">
<Art_Type>Poster</Art_Type>
<Location>3</Location>
<Line1>ELEVATOR
MACHINE
ROOM
107</Line1>
</ROW>
</FMPDSORESULT>
I want the group each record that matches both ART_TYPE and LINE1. After being grouped it should add the Location from the match to the one being grouped into so it should look like:
<ROW MODID="19" RECORDID="11116">
<Art_Type>Poster</Art_Type>
<Location>1 2 3</Location>
<Line1>ELEVATOR
MACHINE
ROOM
107
</Line1>
</ROW>
Any help on how to get started would be appreciated. Also is there any good xslt 1.0 testing program?
Thanks in advance!
EDIT: I was pointed to muenchian grouping and found this site: http://www.jenitennison.com/xslt/grouping/muenchian.html
So from reading that I came up with:
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="artTypeNames" match="ROW" use="Art_Type" />
<xsl:key name="artCopy" match="ROW" use="Line1" />
<xsl:template match="FMPDSORESULT">
<xsl:for-each select="ROW[count(. | key('artTypeNames', Art_Type)[1]) = 1]">
<xsl:sort select="Art_Type" />
<xsl:value-of select="Art_Type" />
<xsl:for-each select="key('artTypeNames', Art_Type)">
<xsl:sort select="Art_Type" />
<xsl:value-of select="Art_Type" />
</xsl:for-each>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
I entered the XML and the XSLT into an online XML Transformer and I get 'XSLT is invalid' error.
This is frustrating.
EDIT2: With Tim's help I was able to construct a proper XSLT transform:
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fm="http://www.filemaker.com/fmpdsoresult">
<xsl:template match="fm:FMPDSORESULT">
<xsl:apply-templates select="fm:ROW[count(. | key('lineData', fm:Line1)[1]) = 1]">
</xsl:apply-templates>
</xsl:template>
<xsl:template match="fm:ROW">
<xsl:copy>
<xsl:apply-templates select="fm:Art_Type" />
<fm:Location>
<xsl:apply-templates select="key('artTypeNames', fm:Art_Type)/fm:Location" />
</fm:Location>
<xsl:apply-templates select="fm:Line1" />
</xsl:copy>
</xsl:template>
<xsl:template match="fm:Location">
<xsl:if test="position() > 1">-</xsl:if>
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
It groups the Art_Type and then by the Line1 text, but now adds the location numbers to all of them like this:
<ROW xmlns="http://www.filemaker.com/fmpdsoresult">
<Art_Type>Poster</Art_Type>
<fm:Location xmlns:fm="http://www.filemaker.com/fmpdsoresult" xmlns="">1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26-27-28-29-30-31-32-33-34</fm:Location>
<Line1>CUSTODIAL
LOUNGE
117A
</Line1>
</ROW>
<ROW xmlns="http://www.filemaker.com/fmpdsoresult">
<Art_Type>Poster</Art_Type>
<fm:Location xmlns:fm="http://www.filemaker.com/fmpdsoresult" xmlns="">1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26-27-28-29-30-31-32-33-34</fm:Location>
<Line1>STORAGE
ROOM
117B
</Line1>
</ROW>
If the Line1 text is different, it should be adding it into another group.
</FMPDSORESULT>
– Hash