INPUT:
<?xml version="1.0" encoding="UTF-8"?>
<output>
<input>
<!--details-->
</input>
<meta2>
<Comenzi>
<output_getquerydata>
<queries>
<query name="part1">
<parameters>
<!--details-->
</parameters>
<queryErrors>
<!--details-->
</queryErrors>
<queryResults>
<record id="1">
<column name="id_schedule">3</column>
<column name="VRIdTask">1319648</column>
<column name="MSSinergieOrderP"/>
<column name="MSSinergieTaskP"/>
<column name="VRPlanId">11310337</column>
<column name="TripLabel">189221</column>
<column name="MSSinergieOrderNP"/>
<column name="MSSinergieTaskNP"/>
</record>
<record id="2">
<column name="id_schedule">3</column>
<column name="VRIdTask">1319652</column>
<column name="MSSinergieOrderP">1320566</column>
<column name="MSSinergieTaskP">1319575</column>
<column name="VRPlanId">11310281</column>
<column name="TripLabel">189221</column>
<column name="MSSinergieOrderNP"/>
<column name="MSSinergieTaskNP"/>
</record>
<record id="3">
<column name="id_schedule">3</column>
<column name="VRIdTask">1319652</column>
<column name="MSSinergieOrderP">1320614</column>
<column name="MSSinergieTaskP">1319623</column>
<column name="VRPlanId">11310281</column>
<column name="TripLabel">189221</column>
<column name="MSSinergieOrderNP"/>
<column name="MSSinergieTaskNP"/>
</record>
<record id="4">
<column name="id_schedule">3</column>
<column name="VRIdTask">1319652</column>
<column name="MSSinergieOrderP">1320656</column>
<column name="MSSinergieTaskP">1319667</column>
<column name="VRPlanId">11310281</column>
<column name="TripLabel">189221</column>
<column name="MSSinergieOrderNP"/>
<column name="MSSinergieTaskNP"/>
</record>
</queryResults>
</query>
</queries>
</output_getquerydata>
</Comenzi>
</meta2>
</output>
XSL:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" />
<xsl:template name="task-attr">
<!--parameter to filter per VRPlanId-->
<xsl:param name="param.VRPlanId"/>
<xsl:variable name="var.mstp">
<xsl:for-each
select="/output/meta2/Comenzi/output_getquerydata/queries/query/queryResults/rec
ord[column[@name='VRPlanId'] = $param.VRPlanId]">
<!--preventing empty blocks and duplicates for MSTP-->
<xsl:if test="string-length(column[@name='MSSinergieTaskP']) >0
and not(preceding::record[column[@name='MSSinergieTaskP']/text() =
current()/column[@name='MSSinergieTaskP']/text()])">
<xsl:value-of select="concat(column[@name='MSSinergieTaskP'],
',')"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="var.msonpc">
<xsl:for-each
select="/output/meta2/Comenzi/output_getquerydata/queries/query/queryResults/re
cord[column[@name='VRPlanId'] = $param.VRPlanId]">
<!--preventing empty blocks and duplicates for MSONPC-->
<xsl:if test="string-length(column[@name='MSSinergieTaskNP']) >0
and not(preceding::record[column[@name='MSSinergieTaskNP']/text() =
current()/column[@name='MSSinergieTaskNP']/text()])">
<xsl:value-of select="concat(column[@name='MSSinergieTaskNP'],
',')"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<!--concat all non-duplicates MSTP and MSONPC without last delimiter-->
<xsl:value-of select="concat($var.mstp, substring($var.msonpc, 1, string-
length($var.msonpc)-1))"/>
</xsl:template>
<xsl:template match="/">
<output>
<xsl:for-each
select="/output/meta2/Comenzi/output_getquerydata/queries/query/queryResults/record">
<xsl:if test ="not(preceding::record[column[@name='VRPlanId']/text() =
current()/column[@name='VRPlanId']/text()])">
<xsl:variable name="task.id">
<xsl:call-template name="task-attr">
<!--as input parameter put non-duplicate VRPlanId-->
<xsl:with-param name="param.VRPlanId"
select="column[@name='VRPlanId']"/>
</xsl:call-template>
</xsl:variable>
<!--if all non-duplicates MSTP and MSONPC will be blank block
Cdo won't be created-->
<xsl:if test="string-length($task.id) >0">
<Cdo>
<parameters>
<task>
<xsl:attribute name="id">
<xsl:value-of select="$task.id"/>
</xsl:attribute>
</task>
<action>
<xsl:attribute name="id">
<xsl:value-of
select="column[@name='VRPlanId']"/>
</xsl:attribute>
</action>
</parameters>
</Cdo>
</xsl:if>
</xsl:if>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
Hello everyone,
Thanks for your help. The goal is to check for every distinct VRPlanID value that we build the tag Group the corresponding MSTP, MSONPC (distinct) if they're not null/empty in the id part of the tag. So, we group after VRPlanID, then get the distinct values from each record from MSTP/MSONPC. If they're empty then just build a dummy / null message/tag. This works, but i have 2 issues: - i always get a ',' comma after the last id, and i can't figure why - I no longer need the concatenated, but I want to have a tag task for each ID
So, the desired output would be:
<output>
<Cdo>
<parameters>
<task id="1319575"/>
<task id="1319623"/>
<task id="1319667"/>
<action id="11310281"/>
</parameters>
</Cdo>
</output>
Keep in mind that any combination between the tags MSSinergieTaskP and MSSinergieTaskNP is possible. So one may be empty, the other not. Or one empty just for a record, then for the next record to have a value. I'm just interested to get the distinct values for any combination, for both of them, for every distinct VRIdTask.
I can't seem to make it work. Any suggestions, please?
For grouping in XSLT 1.0, the most efficient solution is to use muenchian grouping.
In this scenario, you can create a grouping key using the value in attribute @name='VRPlanId'
.
<xsl:key name="vrPlanId" match="record" use="column[@name='VRPlanId']" />
Then group the nodes using the key
<xsl:apply-templates select="//record[generate-id() = generate-id(key('vrPlanId', column[@name='VRPlanId'])[1])]" />
So the output
template is modified to
<xsl:template match="output">
<xsl:copy>
<Cdo>
<parameters>
<xsl:apply-templates select="//record[generate-id() = generate-id(key('vrPlanId', column[@name='VRPlanId'])[1])]" />
</parameters>
</Cdo>
</xsl:copy>
</xsl:template>
For the record
template, check whether any of MSSinergieTaskP
or MSSinergieTaskNP
are having values and then loop through the grouped nodes to get the values from MSSinergieTaskP
.
Below is the complete XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:key name="vrPlanId" match="record" use="column[@name='VRPlanId']" />
<xsl:template match="output">
<xsl:copy>
<Cdo>
<parameters>
<xsl:apply-templates select="//record[generate-id() = generate-id(key('vrPlanId', column[@name='VRPlanId'])[1])]" />
</parameters>
</Cdo>
</xsl:copy>
</xsl:template>
<xsl:template match="record">
<xsl:if test="column[@name='MSSinergieTaskP'] != '' or column[@name='MSSinergieTaskNP'] != ''">
<action>
<xsl:attribute name="id">
<xsl:value-of select="column[@name='VRPlanId']" />
</xsl:attribute>
</action>
<xsl:for-each select="key('vrPlanId', column[@name='VRPlanId'])">
<task>
<xsl:attribute name="id">
<xsl:value-of select="column[@name='MSSinergieTaskP']" />
</xsl:attribute>
</task>
</xsl:for-each>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
This provides the required output
<output>
<Cdo>
<parameters>
<action id="11310281" />
<task id="1319575" />
<task id="1319623" />
<task id="1319667" />
</parameters>
</Cdo>
</output>