I'm trying to transform some XML and I'm not really sure if it's possible.
- Is this even possible to achieve?
- How might I update the translate to accomplish it?
I want to group these instances by the academic year and output the rest of the instance content in a table with columns/rows for each instance. The year would become the heading, and the remaining content would appear in the table. If there was also some instances of 2014, that would be a new heading and new table.
<courses>
<course>
<uuid>123</uuid>
<coursetitle>Being better at XSLT</coursetitle>
<coursetypes>
<coursetype>Postgraduate</coursetype>
</coursetypes>
<instances>
<instance>
<uuid>1054EDE3F28D9873</uuid>
<instancecode>MPW1F</instancecode>
<year>2013</year>
</instance>
<instance>
<uuid>D9FC4501-F504-B560-FB8DFC3EF41C0E1F</uuid>
<instancecode>MPO1F</instancecode>
<year>2013</year>
</instance>
<instance>
<uuid>D9FC5017-0789-16A0-C8BBD0F0BE016E6F</uuid>
<instancecode>MPW2F</instancecode>
<year>2013</year>
</instance>
<instance>
<uuid>D9FC4963-A684-565A-5AA282A7CEC7B4CC</uuid>
<instancecode>MPO2F</instancecode>
<year>2013</year>
</instance>
</instances>
</course>
<course>
<uuid>1234</uuid>
<coursetitle>Being better at XML</coursetitle>
<coursetypes>
<coursetype>Postgraduate</coursetype>
</coursetypes>
<instances>
<instance>
<uuid>1054EDE3F28D9873</uuid>
<instancecode>MPW1F</instancecode>
<year>2014</year>
</instance>
<instance>
<uuid>D9FC4501-F504-B560-FB8DFC3EF41C0E1F</uuid>
<instancecode>MPO1F</instancecode>
<year>2014</year>
</instance>
<instance>
<uuid>D9FC5017-0789-16A0-C8BBD0F0BE016E6F</uuid>
<instancecode>MPW2F</instancecode>
<year>2013</year>
</instance>
<instance>
<uuid>D9FC4963-A684-565A-5AA282A7CEC7B4CC</uuid>
<instancecode>MPO2F</instancecode>
<year>2013</year>
</instance>
</instances>
</course>
</courses>
I've had a look at XSLT 1.0 - Create a Unique, Ordered List but it only seems to work on grouping items relative the root.
This is the transform we're trying to run* and it seems to work on the first occurance of the year 2013 however the next course instances are ignored (assuming because it has already found an instance of that year).
**note that I removed some of the content that this is printing from the code above for the sake of being a little cleaner*
<!-- Instances -->
<xsl:template match="instances">
<xsl:for-each select="./instance[applicationmethod != 'NO_APP' and applicationmethod != '' and generate-id()= generate-id(key('acyear',year)[1])]">
<xsl:sort select="year" />
<xsl:value-of select="./year"/>,
</xsl:for-each>
<xsl:apply-templates select="./instance">
<xsl:sort select="year"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="instance">
<xsl:if test="./applicationmethod != 'NO_APP' and ./applicationmethod != ''">
<li>
<xsl:text><strong>UUID:</strong></xsl:text><xsl:value-of select="./uuid"/>
<xsl:text>, <strong>Instance Code:</strong></xsl:text><xsl:value-of select="./instancecode"/>
<xsl:text>, <strong>Year:</strong></xsl:text><xsl:value-of select="./academicyear"/>
<xsl:text>, <strong>Course Year:</strong></xsl:text><xsl:value-of select="./courseyear"/>
<xsl:text>, <strong>Start Date:</strong></xsl:text><xsl:value-of select="./startdate"/>
<xsl:text>, <strong>End Date:</strong></xsl:text><xsl:value-of select="./enddate"/>
<xsl:text>, <strong>Display From:</strong></xsl:text><xsl:value-of select="./displayfrom"/>
<xsl:text>, <strong>Display To:</strong></xsl:text><xsl:value-of select="./displayto"/>
<xsl:text>, <strong>Mode of Study:</strong></xsl:text><xsl:value-of select="./modeofstudy"/>
<xsl:text>, <strong>Application Method:</strong></xsl:text><xsl:value-of select="./applicationmethod"/>
<xsl:text>, <strong>Apply Link:</strong></xsl:text><xsl:value-of select="./applylink"/>
<xsl:text>, <strong>Cost:</strong></xsl:text><xsl:value-of select="./cost"/>
<xsl:text>, <strong>Day and Time:</strong></xsl:text><xsl:value-of select="./dayandtime"/>
</li>
</xsl:if>
</xsl:template>
Output
Each course will be a different page, but the content would look like
Being better at XSLT
Postgraduate
2013
- MPW1F
- MPO1F
- MPW2F
- MPO2F
Being better at XML
Postgraduate
2014
- MPW1F
- MPO1F
2013
- MPW2F
- MPO2F