'XSLT- Modifying a sibling node based on current node by searching a value in whole xml file
I have created a following xml in which I have to calculate a controlid based on sequence number. Each control id is calculated from sequence number object by concatinating objectname and value. I will get the existing sequence number enitity from database for the first time when xml is loaded. The problem is after creating first control id, I am unable to store the created control id to check if the value is created already in a global variable as there is no such concept in XSLT.
The source xml looks like below:
<xml xmlns:xalan="http://xml.apache.org/xslt">
<Control id="" entity="Control">
<ControlName type="String">Control1</ControlName>
<ControlId type="String">Control - 1340</ControlId> <!-- control id is calculated based on sequence number prefix and value - 1-->
<SequenceNumber id="1103998">
<ObjectName type="String">Control</ObjectName>
<Value type="Integer">1341</Value>
</SequenceNumber>
</Control>
<Control id="" entity="Control">
<ControlName type="String">Control2</ControlName>
<ControlId type="String">Control - 1340</ControlId> <!-- control id is calculated based on sequence number prefix and value - 1-->
<SequenceNumber id="1103998">
<ObjectName type="String">Control</ObjectName>
<Value type="Integer">1341</Value>
</SequenceNumber>
</Control>
<Control id="" entity="Control">
<ControlName type="String">Control3</ControlName>
<ControlId type="String">Control - 1340</ControlId> <!-- control id is calculated based on sequence number prefix and value - 1-->
<SequenceNumber id="1103998">
<ObjectName type="String">Control</ObjectName>
<Value type="Integer">1341</Value>
</SequenceNumber>
</Control>
<Control id="" entity="Control">
<ControlName type="String">Test</ControlName>
<ControlId type="String">Test - 1</ControlId> <!-- control id is calculated based on sequence number prefix and value - 1-->
<SequenceNumber id="1103998">
<ObjectName type="String">Test</ObjectName>
<Value type="Integer">2</Value>
</SequenceNumber>
</Control>
</xml>
During calculation of control id, xslt should check if the value is already created, if not it should increase the value of sequence number and then concatinate objectname and value of sequence number. I am unable to find such function in xsl to check if the value is already exists during dynamically creating the xml elements.
the output should like this below:
<?xml version="1.0" encoding="UTF-8"?>
<xml xmlns:xalan="http://xml.apache.org/xslt">
<Control id="" entity="Control">
<ControlName type="String">Control1</ControlName>
<ControlId type="String">Control - 1340</ControlId> <!-- control id is calculated based on sequence number prefix and value - 1-->
<SequenceNumber id="1103998">
<ObjectName type="String">Control</ObjectName>
<Value type="Integer">1341</Value>
</SequenceNumber>
</Control>
<Control id="" entity="Control">
<ControlName type="String">Control2</ControlName>
<ControlId type="String">Control - 1341</ControlId> <!-- control id is calculated based on sequence number prefix and value - 1-->
<SequenceNumber id="1103998">
<ObjectName type="String">Control</ObjectName>
<Value type="Integer">1342</Value>
</SequenceNumber>
</Control>
<Control id="" entity="Control">
<ControlName type="String">Control3</ControlName>
<ControlId type="String">Control - 1342</ControlId> <!-- control id is calculated based on sequence number prefix and value - 1-->
<SequenceNumber id="1103998">
<ObjectName type="String">Control</ObjectName>
<Value type="Integer">1343</Value>
</SequenceNumber>
</Control>
<Control id="" entity="Control">
<ControlName type="String">Test</ControlName>
<ControlId type="String">Test - 1</ControlId> <!-- control id is calculated based on sequence number prefix and value - 1-->
<SequenceNumber id="1103998">
<ObjectName type="String">Test</ObjectName>
<Value type="Integer">2</Value>
</SequenceNumber>
</Control>
</xml>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
