验证失败:“EntityRef:期待“”
您好,我有一些无法验证的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的网址中包含未转义的 & 符号
&
。它们要么需要 (a) 更改为字符实体 (&
),要么 (b) 包含在 CDATA 部分中。CDATA 部分允许您保留特殊字符,例如
&
未转义,因此这是最简单的:除了确切的字符序列
]]> 之外,您还可以在 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: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
notgetelementbyid
.修改内容并不总是可能的,例如,如果您正在抓取网站。
你不能只是 str_replace '&'与 '&'因为 html 可能包含有效的 html 实体,并且您会得到类似“&”的内容
这是一个正则表达式,它应该用 & 符号的 htmlentities 替换 & 符号,而不破坏良好的 htmlentities:
我用它抓取了大约 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 "&"
Here's a regex which should replace ampersands with htmlentiries for ampersands, without breaking good htmlentities:
I used it to scrape about 700 pages without any problems :)