gpt4 book ai didi

xslt - xsl :character-map to replace special characters

转载 作者:行者123 更新时间:2023-12-05 00:06:26 29 4
gpt4 key购买 nike

给定一个元素,其值为:

 <xml_element>Distrib = SU &amp; Prem &amp;lt;&amp;gt; 0</xml_element>

我要转 &amp;lt;&amp;gt;进入 &lt;&gt;因为下游应用程序需要在整个 XML 文档中采用这种格式。我也需要这个用于引号和撇号。我正在尝试 XSLT 2.0 中的字符映射。
<xsl:character-map name="specialchar">
<xsl:output-character character="&apos;" string="&amp;apos;" />
<xsl:output-character character="&quot;" string="&amp;quot;" />
<xsl:output-character character="&gt;" string="&amp;gt;" />
</xsl:character-map>

最佳答案

<xsl:character-map> 指令可用于将单个字符序列化为任何字符串。但是,此问题需要多个字符(一个与号后跟另一个要替换的字符。
<xsl:character-map>不能用来解决此类问题。

这里有一个解决这个问题的方法,使用 XPath 2.0 replace()功能:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>

<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="text()">
<xsl:value-of select=
'replace(
replace(
replace(., "&amp;lt;", "&lt;"),
"&amp;gt;",
"&gt;"
),
"&amp;apos;",
"&apos;"
)
'/>
</xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时 :
<xml_element>Distrib = SU &amp; &amp;apos;Prem &amp;lt;&amp;gt; 0</xml_element>

产生了想要的结果 :
<xml_element>Distrib = SU &amp; 'Prem &lt;&gt; 0</xml_element>

关于xslt - xsl :character-map to replace special characters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3207297/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com