验证失败:“EntityRef:期待“”

发布于 2024-09-13 11:10:57 字数 863 浏览 3 评论 0原文

您好,我有一些无法验证的 XML。我已将问题范围缩小到这一点:

<script type="text/javascript">document.getelementbyid("oxm-1f4a4485-5a1d-45f9-a989-9c65a0b9ceb6").src="http://bid.website.net/display?l=h4siaaaaaaaaad2nmq6cqbrenycw7qjyolfccxmregvcoae0u0sly_agtvaewwn4bg_havwbnebpvmzkkzra_kzzdvoloq4u-hjnp7sii0rxcbzz5vl5kxsrds6wtsfbxmcr9chysuhqbecuckb8cvx4m-pbcxugtdrll6d3dqtihnqukth2yvdkptr67cuzfvlxjlinkul9634lpal_h4mwhso8aabzhw1cdcwjxl6xivgv8agrjxjc_gaaaa==&p=h4siaaaaaaaaabxkmq7cmaxaurcqjjrrsfqqsrm7x3fsrwyvosda8qnj_3ojfgb49o45pblq7e80syzjhopggso9wyzpcpntzkxk1ldtbbi7otmxfj9da1wpjcf10vtxdj9e5_utyj19k2lfssepld5agnqaaaa=&url=http%3a%2f%2flocalhost%2fproject-debug%2fproject.html";</script>

我将其放入 XML 验证器中,然后它输出:

该页面包含以下内容 错误:第 1 行第 16 列错误: EntityRef:期待 ';'

关于缺少“;”的位置的任何想法应该去吗?还有其他问题吗?

Hi I've got some XML that won't validate. I've narrowed down the problem to this bit:

<script type="text/javascript">document.getelementbyid("oxm-1f4a4485-5a1d-45f9-a989-9c65a0b9ceb6").src="http://bid.website.net/display?l=h4siaaaaaaaaad2nmq6cqbrenycw7qjyolfccxmregvcoae0u0sly_agtvaewwn4bg_havwbnebpvmzkkzra_kzzdvoloq4u-hjnp7sii0rxcbzz5vl5kxsrds6wtsfbxmcr9chysuhqbecuckb8cvx4m-pbcxugtdrll6d3dqtihnqukth2yvdkptr67cuzfvlxjlinkul9634lpal_h4mwhso8aabzhw1cdcwjxl6xivgv8agrjxjc_gaaaa==&p=h4siaaaaaaaaabxkmq7cmaxaurcqjjrrsfqqsrm7x3fsrwyvosda8qnj_3ojfgb49o45pblq7e80syzjhopggso9wyzpcpntzkxk1ldtbbi7otmxfj9da1wpjcf10vtxdj9e5_utyj19k2lfssepld5agnqaaaa=&url=http%3a%2f%2flocalhost%2fproject-debug%2fproject.html";</script>

I put it in an XML validator and it spat out:

This page contains the following
errors: error on line 1 at column 16:
EntityRef: expecting ';'

Any ideas as to where the missing ';' is supposed to go? Is there another problem?

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

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

发布评论

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

评论(2

悸初 2024-09-20 11:10:57

您的网址中包含未转义的 & 符号 &。它们要么需要 (a) 更改为字符实体 (&),要么 (b) 包含在 CDATA 部分中。

CDATA 部分允许您保留特殊字符,例如 & 未转义,因此这是最简单的:

<script type="text/javascript">
// <![CDATA[
    document.getElementById(...).src="...";
// ]]>
</script>

除了确切的字符序列 ]]> 之外,您还可以在 CDATA 部分中包含任何您想要的内容;// 注释用于确保不理解 CDATA 部分的浏览器忽略 ]]> 标记。

顺便说一下,JavaScript 是区分大小写的。那应该是 getElementById 而不是 getelementbyid

You have unescaped ampersands & in your URL. They either need to be (a) changed to character entities (&), or (b) enclosed in a CDATA section.

A CDATA section lets you leave special characters like & unescaped, so that'd be easiest:

<script type="text/javascript">
// <![CDATA[
    document.getElementById(...).src="...";
// ]]>
</script>

You can include anything you want inside of a CDATA section aside from the exact character sequence ]]>. The // comments are there to make sure browsers that don't understand CDATA sections ignore the <![CDATA[ and ]]> markers.

By the way, JavaScript is case sensitive. That should be getElementById not getelementbyid.

半寸时光 2024-09-20 11:10:57

修改内容并不总是可能的,例如,如果您正在抓取网站。

你不能只是 str_replace '&'与 '&'因为 html 可能包含有效的 html 实体,并且您会得到类似“&amp;”的内容

这是一个正则表达式,它应该用 & 符号的 htmlentities 替换 & 符号,而不破坏良好的 htmlentities:

$html = preg_replace("|&([^;]+?)[\s<&]|","&$1 ",$html);

我用它抓取了大约 700 个页面,没有任何问题:)

modifying the content isn't always possible, e.g if you're scraping a website.

you can't just str_replace '&' with '&' because the html might include valid html entities, and you'd get something like "&amp;"

Here's a regex which should replace ampersands with htmlentiries for ampersands, without breaking good htmlentities:

$html = preg_replace("|&([^;]+?)[\s<&]|","&$1 ",$html);

I used it to scrape about 700 pages without any problems :)

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