Got the following XML structure:
<?xml version="1.0" encoding="UTF-16"?>
<xml>
<v id="Node_Name_1">Node_1_Value</v>
<v id="Node_Name_2">Node_2_Value</v>
<v id="Node_Name_3">Node_3_Value</v>
<v id="Node_Name_4">Node_4_Value</v>
</xml>
The structure above is returned by a web service as String, what I want is to transform that structure into:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<Node_Name_1>Node_1_Value</Node_Name_1>
<Node_Name_2>Node_2_Value</Node_Name_2>
<Node_Name_3>Node_3_Value</Node_Name_3>
<Node_Name_4>Node_4_Value</Node_Name_4>
</xml>
This is my code:
local pattern = '(<v id="(.+)">(.+)</v>)'
local rplcmnt = "<%1>%2</%1>"
local endXml = string:gsub(orgXml, pattern, rplcmnt)
local endStr = "<?xml version='1.0' encoding='UTF-8'?><xml>"..endXml.."</xml>"
The problem is that the pattern returns the whole content of node and at replacing stage it takes first node name, create the tag and then uses it for wrap everything else.
'(<v id="(.+?)">(.+?)</v>)'- Avinash Raj