I have the following input:
<root>
<output>
<queries>
<query name="name1">
<parameters>
<parameter name="id_contact">8947</parameter>
</parameters>
<queryResults/>
</query>
<query name="name1">
<parameters>
<parameter name="id_contact">8943</parameter>
</parameters>
<queryResults>
<record id="1">
<column name="id_task">16422</column>
<column name="id_contact">8943</column>
</record>
</queryResults>
</query>
<query name="name1">
<parameters>
<parameter name="id_contact">1571</parameter>
</parameters>
<queryResults/>
</query>
</queries>
</output>
<output2>
<output_getquerydata>
<data>
<query name="name2">
<parameters>
<parameter name="id">1</parameter>
</parameters>
<queryResults>
<record id="1">
<column name="id_task">14016</column>
<column name="id_contact">8947</column>
</record>
<record id="2">
<column name="id_task">14012</column>
<column name="id_contact">8943</column>
</record>
<record id="3">
<column name="id_task">8826</column>
<column name="id_contact">1571</column>
</record>
</queryResults>
</query>
<output_getquerydata>
<queries>
<query name="name3">
<parameters>
<parameter name="id_task">14016</parameter>
</parameters>
<queryResults>
<record id="1">
<column name="id_shift">2989</column>
</record>
</queryResults>
</query>
<query name="name3">
<parameters>
<parameter name="id_task">14012</parameter>
</parameters>
<queryResults/>
</query>
<query name="name3">
<parameters>
<parameter name="id_task">8826</parameter>
</parameters>
<queryResults/>
</query>
</queries>
</output_getquerydata>
</data>
</output_getquerydata>
</output2>
</root>
My XSL:
<xsl:strip-space elements="*"/>
<xsl:key name="k" match="output/queries/query/queryResults/record" use="column[@name='id_contact']"/>
<xsl:key name="ok" match="output2/output_getquerydata/data/query/queryResults/record" use="column[@name='id_task']"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- suppress the first output branch -->
<xsl:template match="output"/>
<!-- suppress records that have a matching entry in the other branch -->
<xsl:template match="record[key('k', column[@name='id_contact'])]"/>
<xsl:template match="parameters[not(key('ok', ./parameter))]"/>
The goal is for each 'query1' that has queryResults/record, I take the value from that queryResults/record/column[@name='id_contact'] and delete every record from "query2" that have that value in query2/queryResults/record/column[@name='id_contact']. This part works, but the next part doesn't work as it should : Then, after the values from query2 have been deleted, take the remaining query2/id_task values and keep the query[name3] that has the same value in id_task.
Desired output:
<root>
<output2>
<output_getquerydata>
<data>
<query name="name2">
<parameters>
<parameter name="id">1</parameter>
</parameters>
<queryResults>
<record id="1">
<column name="id_task">14016</column>
<column name="id_contact">8947</column>
</record>
<!--record no.22 deleted, because id_contact=8943 is a match in query1-->
<record id="3">
<column name="id_task">8826</column>
<column name="id_contact">1571</column>
</record>
</queryResults>
</query>
<output_getquerydata>
<queries>
<query name="name3">
<parameters>
<parameter name="id_task">14016</parameter>
</parameters>
<queryResults>
<record id="1">
<column name="id_shift">2989</column>
</record>
</queryResults>
</query>
<!--2nd query name3 deleted, because id_task=14012 is not a match in remaining query2 values-->
<query name="name3">
<parameters>
<parameter name="id_task">8826</parameter>
</parameters>
<queryResults/>
</query>
</queries>
</output_getquerydata>
</data>
</output_getquerydata>
</output2>
</root>
What am I doing wrong? Thank you
The template match that is not working is this one...
<xsl:template match="parameters[not(key('ok', ./parameter))]"/>
Firstly, you should really be matching the query
node, as this is the one you want to remove.
Secondly, templates match against the input XML, not the output XML. You may be removing records from the output, but the input is unchanged, and so this is what will be matched.
So, what you need to do is check if the query2
value exists or not, and if it does exist, then check if that same value is going to be removed (i.e it exists in query1
Replace the above template match with this one instead, and see if that makes a difference:
<xsl:template match="queries/query
[not(key('ok', parameters/parameter))
or key('k', key('ok', parameters/parameter)/column[@name='id_contact'])]"/>