Reputation: 75
I am trying to write a match and replace function using XSLT. I've two variable with different name value pairs in it. Few of the name matches in both the variable. I need to replace the values of the matching name value pairs. Example would be:
<original_val>xxx=123,iii=kos,123=hfj,i920=owp02</original_val>
<replace_val>xxx=abc,i920=23ew</replace_val>
From the above you can see that the
replace_val
element does have xxx and i920 name which matches the name xxx and i920 in
original_val
element.
My task is to replace the value of xxx and i920 in original_val element with the value of replace_val element using XSL. The value of xxx and i920 is totally random. I'm using XSLT version 1. Any tips would be really helpful.
Upvotes: 0
Views: 197
Reputation: 1235
My tip is to split both strings using a template and create two new variables that contain something like this:
<originalSplit>
<element>
<name>xxx</name>
<value>123</value>
</element>
<element>
<name>iii</name>
<value>kos</value>
</element>
<element>
<name>123</name>
<value>hfj</value>
</element>
<element>
<name>i920</name>
<value>owp02</value>
</element>
</originalSplit>
<replaceSplit>
<element>
<name>xxx</name>
<value>abc</value>
</element>
<name>i920</name>
<value>23ew</value>
</element>
</replaceSplit>
Then you can loop thru the elements of the originalSplit variable and output the name of each. Then check to see if there is a name match in the replaceSplit variable. If so, output the replace value. If not, output the original value. There are a lot of questions of this site about how to split delimited strings in XSLT 1.0. I suggest using one of those answers.
Upvotes: 2