xml addattribute 转义&

发布于 2024-10-14 22:24:01 字数 736 浏览 11 评论 0原文

我正在转义一个字符串以匹配另一个字符串。

使用 javascript

function escapeCode()
{
     var a = RequestIsland.XMLDocument.firstChild;
     var lineItems = a.selectNodes( "//a/p" );
     while ( (lineItem = lineItems.nextNode()) != null )
     {      
        var text = lineItem.getAttribute("c");
        alert(text);
        text = escapeXML(text);
        lineItem.setAttribute("c",text);
        alert(text);
        alert(lineItem.xml);
     }
}

使用字符串“car's & Trucks”,

我得到了

"car's & trucks" 
"car's & trucks" 
"car's & trucks"

当文本进入属性时,它会转义 & 。再次。

有谁知道为什么会发生这种情况以及如何阻止它?

如果 & 则此方法有效。不在字符串中。

I am escaping a string to match another string.

Using javascript

function escapeCode()
{
     var a = RequestIsland.XMLDocument.firstChild;
     var lineItems = a.selectNodes( "//a/p" );
     while ( (lineItem = lineItems.nextNode()) != null )
     {      
        var text = lineItem.getAttribute("c");
        alert(text);
        text = escapeXML(text);
        lineItem.setAttribute("c",text);
        alert(text);
        alert(lineItem.xml);
     }
}

using string "car's & trucks"

I get

"car's & trucks" 
"car's & trucks" 
"car's & trucks"

Somehow when the text goes into the attribute it is escaping the & again.

Anyone know why this is happening and how to stop it?

This works if the & is not in the string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狠疯拽 2024-10-21 22:24:01

好吧,正如您在评论中指出的那样,当文本存储到属性中时,文本不会被重新转义。我认为您所看到的是“.xml”访问器向您返回一些实际的 XML 标记必须是什么样子,以便您最终得到这样的属性值。

如果您考虑标记本身,对于原始的未转义字符串,您需要执行此操作才能使其成为有效的 XML:

<tag c='car's & trucks'>

换句话说,如果您引用“&”号和撇号,标记将无效。您编写的代码尝试将属性值更新为字符串包括转义序列。

从 JavaScript 设置属性值时,无需转义特殊的 XML 元字符。仅当您准备将实际 XML 标记传递给 XML 解析器时,这一点才重要。一旦你在 JavaScript 中搞乱了 DOM,XML 就已经被解析了。

Well, as you note in a comment, the text is not being re-escaped when it is stored into the attribute. I think what you're seeing is that the ".xml" accessor is giving you back what some actual XML markup would have to look like in order for you to end up with an attribute value like that.

If you think about the markup itself, for your original unescaped string you'd need to do this in order for it to be valid XML:

<tag c='car's & trucks'>

In other words, if you were to not quote the ampersand and apostrophe, the markup would be invalid. Your code as written is trying to update the attribute value to be the string including the escape sequences.

When setting attribute values from JavaScript, there's no need to escape special XML meta-characters. It's only important when you're preparing actual XML markup to be handed to an XML parser. Once you're messing with the DOM in JavaScript, the XML has already been parsed.

倾城泪 2024-10-21 22:24:01

当 DOM 树被序列化为词法 XML 时,转义是自动发生的事情。如果您在将字符串添加到 DOM 树之前对其进行转义,那么正如您所发现的那样,它最终会进行双重转义。

Escaping is something that happens automatically when the DOM tree is serialized as lexical XML. If you escape a string before adding it to the DOM tree, then it's going to end up doubly-escaped, as you have discovered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文