更新:
虽然与以前不太一样,但问题仍然存在。下面是输入内容、输出内容以及我想要输出内容的示例
输入示例:
<p><span style="font-size: medium">Product description text</span></p>
当前输出:
<p><span style="font-size: medium">Product description text</span></p>
预期输出:
<p><span style="font-size: medium">Product description text</span></p>
。
使用 CDATA 很有帮助,因为它允许我输入“<”但如上面的输出所示,即使使用disable-output-escaping,输出也发生了变化
。
。
原始问题:
我收到的错误是“'<',十六进制值0x3C,是无效的属性字符”
我想要做的是替换所有出现的 < ;和>与 <和>分别。
为了尽可能简单,这里是 lt; 的代码:
<xsl:variable name="lt">
<xsl:text><</xsl:text>
</xsl:variable>
<xsl:variable name="lthex">&lt;</xsl:variable>
<xsl:copy-of select="ew:replacestring(products_description/node(),$lthex,$lt)"/>
我尝试了各种方法来代替文本,例如 value-of 等。
我知道代码格式和链接到它的 vb 代码没有任何问题,因为我多次使用它来替换并输出到其他地方
,但问题是我想要 <和>从字面上看是输出,而不是浏览器看到并更改的代码
。
如果您需要更多信息,请询问(我正在努力解释这一点)
任何帮助将不胜感激
Update:
The issue still persists although it is not quite the same as before. Below is an example of what is being input, what is being output and what I want to have output
An example of the input:
<p><span style="font-size: medium">Product description text</span></p>
Current output:
<p><span style="font-size: medium">Product description text</span></p>
Intended output:
<p><span style="font-size: medium">Product description text</span></p>
.
Using CDATA has helped as it allows me to input '<' but as seen in the output above, even when using disable-output-escaping, it has changed in the output
.
.
Original question:
The error I'm getting is "'<', hexadecimal value 0x3C, is an invalid attribute character"
What I'm trying to do is replace all occurrences of < ; and > ; with < and > respectively.
To keep this as simple as possible, here is the code for just lt;:
<xsl:variable name="lt">
<xsl:text><</xsl:text>
</xsl:variable>
<xsl:variable name="lthex"><</xsl:variable>
<xsl:copy-of select="ew:replacestring(products_description/node(),$lthex,$lt)"/>
I've tried various things in place of text e.g. value-of etc.
I know there's nothing wrong with the code format and the vb code linked to it because I'm using it multiple times to replace and output elsewhere
The problem with this though is that I want < and > to literally be ouput, not a code which is then seen by the browser and changed
.
If you need more information just ask (I'm struggling to explain this very well)
Any help will be appreciated
发布评论
评论(3)
这是因为
<
是非法的(这就是您的应用程序抱怨的原因)。对于>
,您可以使用:This is because
<
is illegal (and that's why your app is complaining). For>
, you can use:原来的问题
您真正拥有的是双重转义输入,因此您需要一些方法来完成,而不是简单地使用
disable-output-escaping="yes"
输出文本字符串替换。显然,您似乎使用ew:replacestring()
扩展函数进行了替换。问题是,即使您设法将<
替换为<
,结果仍然只是一个字符串,而<
code> 字符在输出时被转义。您原来的替换文本代码格式不正确,因为
<
本身被视为标记。使用或
等效项并解决该问题。然而,这对于解决替换字符仍然作为实体引用输出的问题没有帮助。在变量定义中使用
disable-output-escaping="yes"
也对这个问题没有影响。这些代码示例不起作用,因为
disable-output-escaping="yes"
仅在序列化值时有效,而在分配变量时无效。可能的修复
您可以尝试做的是将文本替换的结果存储到变量中
,然后使用
disable-output-escaping="yes"
输出此变量的值另一种不带扩展名的解决方案 版本
我能够
将您的输入转换为此输出
使用稍加修改的 Jeni Tennison 的缩写替换模板。在您的情况下,包含首字母缩略词标题的文档将如下所示
所需的修改是将代码示例的这一部分更改
为如下所示
该模板应该在每个环境中工作,因为它是 XSLT 1.0 并且不需要任何扩展名功能。
The original problem
What you really seem to have is double escaped input so instead of just simple outputting the text with
disable-output-escaping="yes"
you need some ways to do string replacing. Clearly it seems that you do the replacing withew:replacestring()
extension function. The problem is that even if you manage to replace<
with<
, the result is still only a string and the<
character gets escaped on output.Your original replacement text code was not well-formed because
<
as such is seen as markup. Usingor
are equivalent and fix that problem. However this doesn't help in the problem that the replacement character still is outputted as an entity reference. Using
disable-output-escaping="yes"
here in the variable definition has no effect on that problem either.These code examples don't work because
disable-output-escaping="yes"
only has an effect when the value is serialized, not when a variable gets assigned.A possible fix
What you could try to do is to store the result of your text replacing into a variable
and then output the value of this variable with
disable-output-escaping="yes"
Another solution without extension functions
I was able to transform your input
to this output
with a slightly modified version of Jeni Tennison's acronym replacement template. In your case the document containing the acronym titles would look like this
The required modification would be to change this part of the code example
to look like this
That template should work in every environment because it is XSLT 1.0 and it doesn't require any extension functions.
编辑:看起来您正在尝试将 XML 或 HTML 序列化的序列化而不是 XML 或 HTML 序列化作为输入,并期望它输出 XML 或 HTML 序列化?
正确的做法是修复损坏的输入,但请参阅如何借助 XSLT 对 XML 字符进行转义?
您在这里有某种误解。 XSL 将 XML 作为输入并输出 HTML 或 XML。
<
在序列化 HTML 和 XML 中表示为<
。包含未开始标记的
<
的 HTML 或 XML 文件是不正确的序列化(格式不正确)。<
是 HTML 或 XML 文件中<
的正确表示形式。HTML 或 XML 文件只是其所表示的数据的序列化。也就是说,它比它所代表的数据低一级。
您应该将 HTML 或 XML 的内容视为数据所代表的内容 (
<
),而不是数据的序列化 (<
)。请尝试解释为什么您认为
<
是一个问题,以便我们纠正误解?Edit: It looks like you're trying to take a serialization of an XML or HTML serialization as input instead of an XML or HTML serialization, and expect it to output an XML or HTML serialization?
The correct thing would be to fix your broken input, but see How to unescape XML characters with help of XSLT?
You have some kind of misunderstanding here. XSL takes XML as input and outputs HTML or XML.
<
is represented as<
in serialized HTML and XML.An HTML or XML file that contains a
<
that does not start a tag is an incorrect serialization (not wellformed).<
is the correct representation of<
in an HTML or XML file.An HTML or XML file is only a serialization of the data it represents. That is, it's one level below the data it represents.
You should think of the contents of the HTML or XML as what the data represents (
<
), not as the serialization of the data (<
).Try instead to explain why you think having
<
is a problem, so we can correct the misunderstanding?