xml 即使在转义字符后也无法让 xslt 输出 (&)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/67859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:00:19  来源:igfitidea点击:

Cannot get xslt to output an (&) even after escaping the character

xmlxsltescapinginvalid-characters

提问by

I am trying to create a query string of variable assignments separated by the &symbol (ex: "var1=x&var2=y&..."). I plan to pass this string into an embedded flash file.

我正在尝试创建一个由&符号分隔的变量赋值查询字符串(例如:)"var1=x&var2=y&..."。我计划将此字符串传递到嵌入式闪存文件中。

I am having trouble getting an &symbol to show up in XSLT. If I just type &with no tags around it, there is a problem rendering the XSLT document. If I type &amp;with no tags around it, then the output of the document is &amp;with no change. If I type <xsl:value-of select="&" />or <xsl:value-of select="&amp;" />I also get an error. Is this possible? Note: I have also tried &amp;amp;with no success.

我无法&在 XSLT 中显示符号。如果我只是&在其周围没有标记的情况下键入,则呈现 XSLT 文档时会出现问题。如果我打字&amp;时周围没有标签,则文档的输出&amp;没有变化。如果我输入<xsl:value-of select="&" />或者<xsl:value-of select="&amp;" />我也得到一个错误。这可能吗?注意:我也尝试过&amp;amp;,但没有成功。

回答by Kevin Hakanson

You can combine disable-output-escapingwith a CDATAsection. Try this:

您可以结合disable-output-escaping一个CDATA部分。尝试这个:

<xsl:text disable-output-escaping="yes"><![CDATA[&]]></xsl:text>

回答by Dimitre Novatchev

I am trying to create a query string of variable assignments separated by the &symbol (ex: "var1=x&var2=y&..."). I plan to pass this string into an embedded flash file.

I am having trouble getting an &symbol to show up in XSLT.

我正在尝试创建一个由&符号分隔的变量赋值查询字符串(例如:)"var1=x&var2=y&..."。我计划将此字符串传递到嵌入式闪存文件中。

我无法&在 XSLT 中显示符号。

Here is a runnable, short and complete demo how to produce such URL:

这是一个可运行的、简短且完整的演示如何生成这样的 URL:

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:variable name="vX" select="'x'"/>
 <xsl:variable name="vY" select="'y'"/>
 <xsl:variable name="vZ" select="'z'"/>

  <xsl:template match="/">
    <xsl:value-of select=
"concat('http://www.myUrl.com/?vA=a&amp;vX=', $vX, '&amp;vY=', $vY, '&amp;vZ=', $vZ)"/>
  </xsl:template>
</xsl:stylesheet>

When this transformation is applied on any source XML document (ignored):

当此转换应用于任何源 XML 文档(忽略)时

<t/>

the wanted, correct result is produced:

产生了想要的、正确的结果:

http://www.myUrl.com/?vA=a&vX=x&vY=y&vZ=z

As for the other issues raised in the question:

至于问题中提出的其他问题:

If I type &amp;with no tags around it, then the output of the document is &amp;with no change.

如果我打字&amp;时周围没有标签,则文档的输出&amp;没有变化。

The above statement simply isn't true ... Just run the transformation above and look at the result.

上面的陈述根本不是真的......只需运行上面的转换并查看结果。

What really is happening:

真正发生了什么

The result you are seeing is absolutely correct, however your output method is htmlor xml(the default value for method=), therefore the serializer of the XSLT processor must represent the correct result -- the string http://www.myUrl.com/?vA=a&vX=x&vY=y&vZ=z-- as (part of) a text node in a well-formed XML document or in an HTML tree.

您看到的结果是绝对正确的,但是您的输出方法是htmlor xml(的默认值method=),因此 XSLT 处理器的序列化程序必须表示正确的结果——字符串http://www.myUrl.com/?vA=a&vX=x&vY=y&vZ=z——作为(部分)文本节点格式良好的 XML 文档或在 HTML 树中。

By definition in a well-formed XML document a literal ampersand must be escaped by a character reference, such as the built-in &amp;or &#38;, or &#x26;

根据格式良好的 XML 文档中的定义,文字与符号必须通过字符引用进行转义,例如内置的&amp;or &#38;,或&#x26;

Remember: A string that is represented as (part of) an XML text node, or within an HTML tree, may not look like the same string when represented as text. Nevertheless, these are two different representations of the same string.

请记住:表示为 XML 文本节点(的一部分)或 HTML 树中的字符串在表示为文本时可能看起来不像相同的字符串。然而,这是同一个字符串的两种不同表示。

To better understand this simple fact, do the following:

为了更好地理解这个简单的事实,请执行以下操作

Take the above transformation and replace the xsl:outputdeclaration:

进行上述转换并替换xsl:output声明:

 <xsl:output method="text"/>

with this one:

有了这个:

 <xsl:output method="xml"/>

Also, surround the output in a single XML element. You may also try to use different escapes for the ampersand. The transformation may now look like this:

此外,将输出括在单个 XML 元素中。您也可以尝试对&符号使用不同的转义符。转换现在可能如下所示:

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

 <xsl:variable name="vX" select="'x'"/>
 <xsl:variable name="vY" select="'y'"/>
 <xsl:variable name="vZ" select="'z'"/>

  <xsl:template match="/">
        <t>
            <xsl:value-of select=
 "concat('http://www.myUrl.com/?vA=a&amp;vX=', $vX, '&#38;vY=', $vY, '&#x26;vZ=', $vZ)"/>
        </t>
  </xsl:template>
</xsl:stylesheet>

And the result is:

结果是

<t>http://www.myUrl.com/?vA=a&amp;vX=x&amp;vY=y&amp;vZ=z</t>

You will get the same result with output method html.

您将获得与 output method 相同的结果html

Question:

问题

Is the URL that is output different (or even "damaged") than the one output in the first transformation?

输出的 URL 是否与第一个转换中的输出不同(甚至“损坏”)?

Answer:

回答

No, in both cases the same string was output -- however in each case a different representationof the string was used.

不,在这两种情况下都输出了相同的字符串——但是在每种情况下都使用了不同的字符串表示形式

Question:

问题

Must I use the DOE (disable-output-escaping="yes") attribute in order to output the wanted URL?

我是否必须使用 DOE ( disable-output-escaping="yes") 属性才能输出所需的 URL?

Answer:

回答

No, as shown in the first transformation.

不,如第一个转换所示。

Question:

问题

Is it recommended to use the DOE (disable-output-escaping="yes") attribute in order to output the wanted URL?

是否建议使用 DOE ( disable-output-escaping="yes") 属性来输出所需的 URL?

Answer:

回答

No, using DOE is a bad practice in XSLT and usually a signal that the programmer doesn't have a good grasp of the XSLT processing model. Also, DOE is only an optional feature of XSLT 1.0 and it is possible that your XSLT processor doesn't implement DOE, or even if it does, you could have problems running the same transformation with another XSLT processor.

不,在 XSLT 中使用 DOE 是一种不好的做法,通常表明程序员没有很好地掌握 XSLT 处理模型。此外,DOE 只是 XSLT 1.0 的一个可选特性,您的 XSLT 处理器可能没有实现 DOE,或者即使实现了,您在使用另一个 XSLT 处理器运行相同的转换时也可能会遇到问题。

Question

I came from a different problem to the question i made a bounty for. My problem: i try to generate this onclick method:

    <input type="submit" 
onClick="return confirm('are you sure?') && confirm('seriously?');" />

what i can make is: i can place the confirms in a function ... but its buggin me that i can not make a & inside a attribute! The solve of this question is the solve of my problem i think.

我来自一个不同的问题,我提出了一个赏金的问题。我的问题:我尝试生成这个 onclick 方法:

    <input type="submit" 
onClick="return confirm('are you sure?') && confirm('seriously?');" />

我可以做的是:我可以将确认放在一个函数中……但是我无法在属性中创建 & !这个问题的解决就是我认为的问题的解决。

Answer

回答

Actually, you can specify the &&Boolean operation inside a JavaScript expression inside an attribute, by representing it as &amp;&amp;

实际上,您可以&&在属性内的 JavaScript 表达式中指定布尔运算,将其表示为&amp;&amp;

Here is a complete example, that everyone can run, as I did on three browsers: Chrome, Firefox 41.1.01 and IE11:

这是一个完整的示例,每个人都可以运行,就像我在三个浏览器上所做的那样:Chrome、Firefox 41.1.01 和 IE11:

HTML:

HTML:

<!DOCTYPE html> 

    <html> 
      <head> 
        <link rel="stylesheet" href="style.css"> 
        <script src="script.js"></script> 
      </head> 
      <body> 
        <h1>Hello Plunker!</h1> 
        <input type="submit" 
onClick="alert(confirm('are you sure?') &amp;&amp; confirm('seriously?'));" /> 
      </body> 
    </html>

JavaScript(script.js):

JavaScript(script.js):

function confirm(message) { 
  alert(message); 
  return message === 'are you sure?'; 

}

When you run this, you'll first get this alert:

当您运行此程序时,您将首先收到此警报:

enter image description here

在此处输入图片说明

Then, after clicking the OKbutton, you'll get the second alert:

然后,单击OK按钮后,您将收到第二个警报:

enter image description here

在此处输入图片说明

And after clicking OKyou'll finally get the alert that produces the result of the &&operation:

单击后,OK您将最终获得产生&&操作结果的警报:

enter image description here

在此处输入图片说明

You may play with this code by varying the values of the arguments passed to the confirm()function and you will verify that the produced results are those of using the &&operator.

您可以通过改变传递给confirm()函数的参数的值来使用此代码,您将验证生成的结果是使用&&运算符的结果。

For example, if you change the <input>element to this:

例如,如果您将<input>元素更改为:

<input type="submit" 
onClick="alert(confirm('really sure?') &amp;&amp; confirm('seriously?'));" /> 

You'll get first this alert:

您将首先收到此警报:

enter image description here

在此处输入图片说明

And when you click OK, you'll immediately get the final result of the &&operation:

当您点击 时OK,您将立即获得操作的最终结果&&

enter image description here

在此处输入图片说明

The second alert is skipped, because the 1st operand of the &&operation was falseand JavaScript is shortcutting an &&where the 1st operand is false.

第二个警报被跳过,因为该操作的第一个操作数&&false并且 JavaScript 正在快捷地&&将第一个操作数是false

To summarize:

总结一下

It is easy to use the &&operator inside an attribute, in an HTML document generated by XSLT, by specifying the &&operand as &amp;&amp;

这是很容易使用&&操作者的属性内,通过XSLT生成的HTML文件中,通过指定&&的操作数作为&amp;&amp;

回答by user4010

Are you expressing the URI in HTML or XHTML? e.g. <tag attr="http://foo.bar/?key=value&amp;key2=value2&amp;..."/>If so, "&amp;" is the correct way to express an ampersand in an attribute value, even if it looks different from than literal URI you want. Browsers will decode "&amp;" and any other character entity reference before either loading them or passing them to Flash. To embed a literal, lone "&" directly in HTML or XHTML is incorrect.

你是用 HTML 还是 XHTML 来表达 URI?例如,<tag attr="http://foo.bar/?key=value&amp;key2=value2&amp;..."/>如果是这样,“ &amp;” 是在属性值中表达&符号的正确方法,即使它看起来与您想要的文字 URI 不同。浏览器将&amp;在加载它们或将它们传递给 Flash 之前解码“ ”和任何其他字符实体引用。要&在 HTML 或 XHTML 中直接嵌入文字,单独的 " " 是不正确的。

I also personally recommend learning more about XML in order to think about these kinds of things in a clearer way. For instance, try using the W3C DOM more (for more than just trivial Javascript); even if you don't use it day-to-day, learning the DOM helped me think about the tree structure of XML correctly and how to think about correctly encoding attributes and elements.

我个人还建议学习更多关于 XML 的知识,以便以更清晰的方式思考这些事情。例如,尝试更多地使用 W3C DOM(不仅仅是简单的 Javascript);即使您不日常使用它,学习 DOM 也帮助我正确思考 XML 的树结构以及如何正确思考对属性和元素进行编码。

回答by Thunder3

Use disable-output-escaping="yes"in your value-oftag

disable-output-escaping="yes"在您的value-of标签中使用

回答by rjray

If you are creating a query string as part of a larger URL in an attribute of some other tag (like "embed"), then you actually wantthe & to be escaped as &amp;. While all browsers will figure out what you mean and Do The Right Thing, if you were to pass your generated doc to a validator it would flag the un-escaped & in the attribute value.

如果您在某个其他标签(如“嵌入”)的属性中创建查询字符串作为较大 URL 的一部分,那么您实际上希望将 & 转义为 &。虽然所有浏览器都会弄清楚您的意思并做正确的事情,但如果您将生成的文档传递给验证器,它会在属性值中标记未转义的 &。

回答by ashirley

If you are trying to produce an XML file as output, you will want to produce &amp;(as &on it's own is invalid XML). If you are just producing a string then you should set the output mode of the stylesheet to textby including the following as a child of the xsl:stylesheet

如果您试图生成一个 XML 文件作为输出,您将需要生成&amp;(因为&它本身就是无效的 XML)。如果您只是生成一个字符串,那么您应该将样式表的输出模式设置为text包含以下内容作为xsl:stylesheet

<xsl:output method="text"/>

This will prevent the stylesheet from escaping things and <xsl:value-of select="'&amp;'" />should produce &.

这将防止样式表转义,并且<xsl:value-of select="'&amp;'" />应该生成&.

回答by Robert Rossney

If your transform is emitting an XML document, you shouldn't disable output escaping. You wantmarkup characters to be escaped in the output, so that you don't emit malformed XML. The XML object that you're processing the output with (e.g. a DOM) will unescape the text for you.

如果您的转换发出 XML 文档,则不应禁用输出转义。您希望在输出中对标记字符进行转义,以免发出格式错误的 XML。您正在处理输出的 XML 对象(例如 DOM)将为您取消转义文本。

If you're using string manipulation instead of an XML object to process the output, you have a problem. But the problem's not with your XSLT, it's with the decision to use string manipulation to process XML, which is almost invariably a bad one.

如果您使用字符串操作而不是 XML 对象来处理输出,则会出现问题。但问题不在于您的 XSLT,而在于使用字符串操作来处理 XML 的决定,这几乎总是一个糟糕的决定。

If your transform is emitting HTML or text (and you've set the output type on the <xsl:output>element, right?), it's a different story. Then it's appropriate to use disable-output-escaping='yes'on your <xsl:value-of>element.

如果您的转换发出 HTML 或文本(并且您已经在<xsl:output>元素上设置了输出类型,对吗?),情况就不同了。那么它适合disable-output-escaping='yes'在您的<xsl:value-of>元素上使用。

But in any case, you'll need to escape the markup characters in your XSLT's text nodes, unless you've wrapped the text in a CDATA section.

但在任何情况下,您都需要对 XSLT 文本节点中的标记字符进行转义,除非您已将文本包装在 CDATA 部分中。

回答by Taran

Disable output escaping will do the job......as this attribute is supported for a text only you can manipulate the template also eg:

禁用输出转义将完成这项工作......因为只有文本支持此属性,您也可以操作模板,例如:

 <xsl:variable name="replaced">

    <xsl:call-template name='app'>
      <xsl:with-param name='name'/>       
    </xsl:call-template>
  </xsl:variable>


<xsl:value-of select="$replaced" disable-output-escaping="yes"/>

---> Wrapped the template call in a variable and used disable-output-escaping="yes"..

---> 将模板调用包装在一个变量中并使用 disable-output-escaping="yes"..

回答by Ram

Just replace & with

只需替换 &

<![CDATA[&]]>

in the data. EX: Data XML

在数据中。例如:数据 XML

<title>Empire <![CDATA[&]]> Burlesque</title>

XSLT tag:

XSLT 标签:

<xsl:value-of select="title" />

Output: Empire & Burlesque

输出:帝国与滑稽剧

回答by Peter Dolland

org.apache.xalan.xslt.Process, version 2.7.2 outputs to the "Here is a runnable, short and complete demo how to produce such URL" mentioned above:

org.apache.xalan.xslt.Process,版本 2.7.2 输出到上面提到的“Here is a runnable, short and complete demo how to generate such URL”:

<?xml version="1.0" encoding="UTF-8"?>http://www.myUrl.com/?vA=a&amp;vX=x&amp;vY=y&amp;vZ=z11522

The XML declaration is suppressed with an additional omit-xml-declaration="yes", but however with output method="text"escaping the ampersands is not justifiable.

XML 声明被附加的 抑制omit-xml-declaration="yes",但输出method="text"转义与符号是不合理的。