0

Can someone help me how to replace occurrence of multiple strings with multiple values in XML my application dose not supports some characters so I need to replace these with string value and in UDF element only,

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <Header FileDate="02/12/2015" InputDateFormat="MM/dd/yyyy"/>
  <Body DataType="Account">
<Account  Name="XYZ"  InceptionDate="03/01/2005"  DEPT="USD BU" BusinessUnit="WTC" ClientAccountId="123" ISOCurrency="USD" >
<UDF Name="Product" Value="DUMMY"/>
<UDF Name="QUANTITY" Value=" &gt; 10 &lt; 15"/>
</Account>
<Account  Name="ABC"  InceptionDate="03/01/2005"  DEPT="USD BU" BusinessUnit="WTC" ClientAccountId="124" ISOCurrency="USD" >
<UDF Name="Comment" Value="abc 10% &amp; 20 &amp; @ xyz"/>
<UDF Name="Product Code" Value="EMMKTOPP"/>
</Account>
</Body>
  <Trailer RecordCount="2"/>
</root>

Replace values in all UDF elements
replace &gt; with "greater than"
replace &lt; with "less than"
replace % with "percent"
replace &amp; with "and"
replace @ with "at"

Output xml should look like: can we write one custom function in XSLT and invoke that once to replace these characters so that in case in future more characters identified update in custom function will automatically take care of calling code

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <Header FileDate="02/12/2015" InputDateFormat="MM/dd/yyyy"/>
  <Body DataType="Account">
<Account  Name="XYZ"  InceptionDate="03/01/2005"  DEPT="USD BU" BusinessUnit="WTC" ClientAccountId="123" ISOCurrency="USD" >
<UDF Name="Product" Value="DUMMY"/>
<UDF Name="QUANTITY" Value=" greater than 10 less than 15"/>
</Account>
<Account  Name="ABC"  InceptionDate="03/01/2005"  DEPT="USD BU" BusinessUnit="WTC" ClientAccountId="124" ISOCurrency="USD" >
<UDF Name="Comment" Value="abc 10percent and 20 and at xyz"/>
<UDF Name="Product Code" Value="EMMKTOPP"/>
</Account>
</Body>
  <Trailer RecordCount="2"/>
</root>
3
  • You know the tag, you know the attribute name => use DOMDocument. Commented Feb 26, 2015 at 8:56
  • HI Deepak, What have you tried so far? You have to match "UDF/Value" and use the translate() function. Best regards, Peter Commented Feb 26, 2015 at 10:30
  • I think Translate() replace character by character and I need to replace strings with strings Commented Feb 27, 2015 at 5:48

1 Answer 1

1

When the below XSLT run with the XML produces the desired output

XSLT:

<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="no" use-character-maps="replaceentity"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:character-map name="replaceentity">
<xsl:output-character string="greater than" character="&gt;"/>
<xsl:output-character string="less than" character="&lt;"/>
<xsl:output-character string="percent" character="%"/>
<xsl:output-character string="and" character="&amp;"/>
<xsl:output-character string="at" character="@"/>
</xsl:character-map>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

3 Comments

I tried with this XSLT, its not working and in output no string is replaced.
@Deepak Khatri: This is XSLT version 2.0. I think your processor does not support this. Kindly check your processors
Yes my ETLtool datastage using version 1, can you please provide way in version 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.