[xsl] identity template to filter and modify atributes

Subject: [xsl] identity template to filter and modify atributes
From: "Fabien Tillier" <f.tillier@xxxxxxxx>
Date: Tue, 30 Nov 2010 16:18:30 +0100
Hi List.
I have an XSL that uses identity template to filter some nodes based on
a specific value.
This does work.
Now, as my count of node has changed, I should modify one of upper nodes
attribute so that it reflects this count.
But how ?


Here is my xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output method = "xml" encoding="UTF-8" indent="no" />
	<xsl:key name="keyHidden" match="//table/header/col/*[@hide =
1]" use="name()"/>
	<xsl:template match="node() | @*">
		 <xsl:copy>
		 	<xsl:apply-templates
select="@*|node()[not(key('keyHidden',name()) != '')]"/>
		 </xsl:copy>
	</xsl:template>
	<xsl:template match="header">
		<!--  we have to change the value of col attribute to
reflect the real number of sub nodes once hidden columns have been
filtered out -->

	<!-- Count how many sub nodes we have -->
		<xsl:variable name="colCount">
			<xsl:value-of select="@col - count(col/*[@hide =
1])"/>
		</xsl:variable>

<!-- That's the idea, but it doesn't work ... -->
		<xsl:attribute name="col">
			 <xsl:value-of select="$colCount"/>
		</xsl:attribute>


		<xsl:copy>
		 	<xsl:apply-templates
select="@*[not(name()='@col')]|node()[not(key('keyHidden',name()) !=
'')]"/>
		 </xsl:copy>


		</xsl:template>
</xsl:stylesheet>



And here is a sample xml data

The @col attribute in <header> is 12 and should be set to 8 as the nodes
with attribute @hide=1 below will be removed

<application>
<table>
<data>
</data>
<header bgcolor="#505E87" col="12" fit="1" ic50="1" type="8">
         <info>
            <number>259</number>
            <family>2</family>
            <type>8</type>
            <theader>Here is a header</theader>
            <tfooter/>
         </info>
         <col>
            <N112 dataalign="left" headeralign="left" sub="1">Column
1</N112>
            <N100 dataalign="left" headeralign="left" sub="1">Column
2</N100>
            <N101 dataalign="left" headeralign="left" sub="1">Column 3
</N101>
            <N308 dataalign="right" headeralign="right" hide="1"
sub="1"> Column 4 </N308>
            <N107 dataalign="center" headeralign="center" sub="1" >
Column 5</N107>
            <P2010 dataalign="center" headeralign="center" sub="1"
>Column 6</P2010>
            <N109 dataalign="center" headeralign="center" sub="1">Column
7</N109>
            <N307 dataalign="right" headeralign="right" hide="1"
sub="1">Column 8</N307>
            <N206 dataalign="right" headeralign="right" hide="1"
sub="1">Column 9</N206>
            <N310 dataalign="center" headeralign="center" hide="1"
sub="1">Column 10</N310>
            <N108 dataalign="center" headeralign="center" sub="1">Column
11</N108>
            <N105 dataalign="right" headeralign="right" sub="3">Column
12</N105>
         </col>
      </header>
   </table>
</application>

Any clue ?
Thanks a lot in advance,
Best regards,
Fabien

Current Thread