Reputation: 45
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=" > 10 < 15"/>
</Account>
<Account Name="ABC" InceptionDate="03/01/2005" DEPT="USD BU" BusinessUnit="WTC" ClientAccountId="124" ISOCurrency="USD" >
<UDF Name="Comment" Value="abc 10% & 20 & @ xyz"/>
<UDF Name="Product Code" Value="EMMKTOPP"/>
</Account>
</Body>
<Trailer RecordCount="2"/>
</root>
Replace values in all UDF elements
replace > with "greater than"
replace < with "less than"
replace % with "percent"
replace & 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>
Upvotes: 0
Views: 1355
Reputation: 459
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=">"/>
<xsl:output-character string="less than" character="<"/>
<xsl:output-character string="percent" character="%"/>
<xsl:output-character string="and" character="&"/>
<xsl:output-character string="at" character="@"/>
</xsl:character-map>
</xsl:stylesheet>
Upvotes: 1