gpt4 book ai didi

html - 在 XSL 中双重转义原始 HTML?

转载 作者:数据小太阳 更新时间:2023-10-29 02:35:43 25 4
gpt4 key购买 nike

我正在处理一个 XML 文件,该文件在节点属性 ( ) 中存储了原始 HTML。

我刚刚意识到 HTML 是双重编码的,所以,而不是:

<div>

其实是这样写的:

<div>

这意味着如果我做类似的事情:

<xsl:value-of select="node/@data" disable-output-escaping="yes" />

我仍然会得到一个(单个)转义值:

&lt;div&gt;

再次取消转义的最简单方法是什么?

最佳答案

这绝对不是很漂亮,但基本上您看到的是有限数量的字符串替换操作

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" />

<xsl:variable name="ampDbl" select="'&amp;amp;'" />
<xsl:variable name="amp" select="'&amp;'" />
<xsl:variable name="ltDbl" select="'&amp;lt;'" />
<xsl:variable name="lt" select="'&lt;'" />
<xsl:variable name="gtDbl" select="'&amp;gt;'" />
<xsl:variable name="gt" select="'&gt;'" />

<xsl:template match="/">
<xsl:apply-templates select="//@data" mode="unescape" />
</xsl:template>

<xsl:template match="@data" mode="unescape">
<xsl:variable name="step1">
<xsl:call-template name="StringReplace">
<xsl:with-param name="s" select="string()" />
<xsl:with-param name="search" select="$ltDbl" />
<xsl:with-param name="replace" select="$lt" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="step2">
<xsl:call-template name="StringReplace">
<xsl:with-param name="s" select="$step1" />
<xsl:with-param name="search" select="$gtDbl" />
<xsl:with-param name="replace" select="$gt" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="step3">
<xsl:call-template name="StringReplace">
<xsl:with-param name="s" select="$step2" />
<xsl:with-param name="search" select="$ampDbl" />
<xsl:with-param name="replace" select="$amp" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$step3" disable-output-escaping="yes" />
</xsl:template>

<!-- generic string replace template -->
<xsl:template name="StringReplace">
<xsl:param name="s" select="''" />
<xsl:param name="search" select="''" />
<xsl:param name="replace" select="''" />

<xsl:choose>
<xsl:when test="contains($s, $search)">
<xsl:value-of select="substring-before($s, $search)" />
<xsl:value-of select="$replace" />
<xsl:variable name="rest" select="substring-after($s, $search)" />
<xsl:if test="$rest">
<xsl:call-template name="StringReplace">
<xsl:with-param name="s" select="$rest" />
<xsl:with-param name="search" select="$search" />
<xsl:with-param name="replace" select="$replace" />
</xsl:call-template>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$s" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

应用于

<root>
<node data="&amp;lt;div&amp;gt;bla &amp;amp;amp; bla&amp;lt;/div&amp;gt;" />
</root>

给出(在源代码中)

<div>bla &amp; bla</div>

在屏幕上当然会变成这样:

bla & bla

您可能希望为 '&quot;' 添加 step4'"'

关于html - 在 XSL 中双重转义原始 HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19733204/

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