i have an xml file as below.
<rule>
<ruleType>IC</ruleType>
<attributes>
<attribute>
<attributeName>salience</attributeName>
<value>5</value>
</attribute>
<attribute>
<attributeName>abc</attributeName>
<value>123</value>
</attribute>
</attributes>
</rule>
I need to change the salience value based on the ruleType.
For example if the ruleType(<ruleType>IC</ruleType>
) is IC then i need to generate an xml as below.
<rule>
<ruleType>IC</ruleType>
<attributes>
<attribute>
<attributeName>salience</attributeName>
<value>100</value>
</attribute>
<attribute>
<attributeName>abc</attributeName>
<value>123</value>
</attribute>
</attributes>
</rule>
if the ruletype is GC(<ruleType>GC</ruleType>
) then i need to generate as below.
<rule>
<ruleType>GC</ruleType>
<attributes>
<attribute>
<attributeName>salience</attributeName>
<value>50</value>
</attribute>
<attribute>
<attributeName>abc</attributeName>
<value>123</value>
</attribute>
</attributes>
</rule>
and sometimes i may get ruleType as empty in that case i need to generate as below.
<rule>
<ruleType/>
<attributes>
<attribute>
<attributeName>salience</attributeName>
<value>10</value>
</attribute>
<attribute>
<attributeName>abc</attributeName>
<value>123</value>
</attribute>
</attributes>
</rule>
Even if it get empty attributes then also i need to generate as above with some default salience value as 10.
I need to modifyc the value elements only assosciated attributeName(salience). If the attributeName is other than salience then i need to put as it is in my resultant xml.
I am using xsl 1.0.
Please provide me some pointers to do the same.
The key to your task is to use the well known Identity Transformation and override the wanted nodes as required.
[XSLT 2.0] You can exploit new XPath 2.0
if
construct and XSLT 2.0 possibility to define custom function, thus minimizing the code to two , easy readable, templates.[XSLT 1.0] For example, even if you could use a single template based on
xsl:choose
instruction, I would use a separate template for each value to be overridden, mainly for readability.