I have the following piece of XML:
<research>
<research.record>
<research.record_number>1</research.record_number>
<research.type>
<value lang="en-US">some research type</value>
</research.type>
<research.type>
<value lang="en-US">some other type of research</value>
</research.type>
<project.record>
<priref>101</priref>
<project.type>
<value lang="en-US">some type of project</value>
</project.type>
</project.record>
</research.record>
</research>
<research>
<research.record>
<research.record_number>2</research.record_number>
<research.type>
<value lang="en-US">some other type of research</value>
</research.type>
<research.type>
<value lang="en-US">a third type of research</value>
</research.type>
<project.record>
<priref>101</priref>
<project.type>
<value lang="en-US">some type of project</value>
</project.type>
</project.record>
</research.record>
</research>
<research>
<research.record>
<research.record_number>3</research.record_number>
<research.type>
<value lang="en-US">some other type of research</value>
</research.type>
<research.type>
<value lang="en-US">a fourth type</value>
</research.type>
<project.record>
<priref>201</priref>
<project.type>
<value lang="en-US">some other type of project</value>
</project.type>
</project.record>
</research.record>
</research>
<research>
... etc ...
With XSLT 1.0 I transform this XML into a list of unique project records by using xsl:key.
So far, so good...
The problem is: I also want to show unique research types for each unique project record.
My simplified stylesheet which shows the repeated research.types (but not unique ones)
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"/>
<xsl:key name="uniqueProject" match="project.record" use="priref"/>
<xsl:template match="record">
<div class="table-row">
<xsl:apply-templates select="//project.record[generate-id() = generate-id(key('uniqueProject', priref)[1])]">
<xsl:sort select="startdate"/>
</xsl:apply-templates>
</div>
</xsl:template>
<xsl:template match="project.record">
<div class="project-container">
<xsl:text>project.record </xsl:text>
<xsl:value-of select="priref"/>
<xsl:text>: </xsl:text>
<xsl:for-each select="//research/research.record/research.type[../project.record/priref = current()/priref]">
<xsl:sort select="value[@lang='en-US']" data-type="text" />
<xsl:if test="value[@lang='en-US'][not(.=preceding::research/research.record/research.type/value[@lang='en-US'][../../project.record/priref = current()/priref])]">
<xsl:value-of select="value[@lang='en-US']"/>
</xsl:if>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</div>
<br/>
<br/>
</xsl:template>
</xsl:stylesheet>
My wanted output would be:
project.record 101: some research type, some other type of research, a third type of research
project.record 201: some other type of research, a fourth type
Hope someone can help me out with the right XSLT/XPATH. (Can only use XSLT1.0)
I would approach this by using a second key that groups research.type
values by a combination of their value and their associated priref.
<xsl:key name="resTypeKey" match="research.type/value[@lang='en-US']"
use="concat(., '+++', ../../project.record/priref)" />
and then use the same Muenchian trick as you already have for the table rows:
<xsl:template match="project.record">
<div class="project-container">
<xsl:text>project.record </xsl:text>
<xsl:value-of select="priref"/>
<xsl:text>: </xsl:text>
<xsl:apply-templates select="
key('uniqueProject', priref)/../research.type
/value[@lang='en-US'][
generate-id() = generate-id(
key('resTypeKey', concat(., '+++', current()/priref))[1])]">
<xsl:sort select="." />
</xsl:apply-templates>
</div>
<br/>
<br/>
</xsl:template>
<xsl:template match="research.type/value">
<xsl:if test="position() > 1">, </xsl:if>
<xsl:value-of select="."/>
</xsl:template>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With