有人可以帮我解决如何用XML中的多个值替换多个字符串的出现 我的应用程序剂量不支持某些字符,所以我需要用字符串值替换它们,仅在UDF元素中,
输入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"
输出xml应如下所示: 我们可以在XSLT中编写一个自定义函数并调用一次来替换这些字符,以便将来识别更多字符时自定义函数中的更新将自动处理调用代码
<?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>
答案 0 :(得分:1)
当使用XML运行下面的XSLT时,会产生所需的输出
<强> 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>