XML 解析错误:未定义的实体 - 特殊字符
为什么 XML 对某些特殊字符显示错误,而有些则正常?
例如,下面会产生错误,
<?xml version="1.0" standalone="yes"?>
<Customers>
<Customer>
<Name>Löic</Name>
</Customer>
</Customers>
但这没关系,
<?xml version="1.0" standalone="yes"?>
<Customers>
<Customer>
<Name>&</Name>
</Customer>
</Customers>
我顺便通过 php - htmlentities('Löic',ENT_QUOTES)
转换特殊字符。
我该如何解决这个问题?
谢谢。
编辑:
我发现如果我使用诸如Lóic
之类的数字字符,效果很好
,现在我必须找到如何使用php将特殊字符转换为数字人物!
Why does XML display error on certain special characters and some are ok?
For instance, below will create error,
<?xml version="1.0" standalone="yes"?>
<Customers>
<Customer>
<Name>Löic</Name>
</Customer>
</Customers>
but this is ok,
<?xml version="1.0" standalone="yes"?>
<Customers>
<Customer>
<Name>&</Name>
</Customer>
</Customers>
I convert the special character through php - htmlentities('Löic',ENT_QUOTES)
by the way.
How can I get around this?
Thanks.
EDIT:
I found that it works fine if I use numeric character such as Lóic
now I have to find how to use php to convert special characters into numeric characters!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
XML 规范中定义了五个实体 -
&
、<
、>
、& ;apos;
和"
有 HTML DTD 中定义了很多实体。
您不能在通用 XML 中使用 HTML 中的内容。
您可以使用数字引用,但最好只获取 字符编码 直接(基本上可以归结为:
)
There are five entities defined in the XML specification —
&
,<
,>
,'
and"
There are lots of entities defined in the HTML DTD.
You can't use the ones from HTML in generic XML.
You could use numeric references, but you would probably be better off just getting your character encodings straight (which basically boils down to:
)
因为它不是内置实体,而是需要在 DTD 中声明的外部实体。
Because it is not an built-in entity, it is instead an external entity that needs declaration in DTD.
TLDR 解决方案
您可以使用
解决此问题html_entity_decode()
(来源:PHP.net),就像这样...完整的在线工作演示
在这个演示中,我使用
’
以及《道德经》中的一行来演示上述html_entity_decode()
的使用...不要忘记替换回那些
<
和>
字符,不过!工作演示沙盒
您如何知道它有效?
想要验证它是否正常工作?然后前往W3C RSS Feed Validator,并看到上面的代码被批准就好了。
TLDR Solution
You can solve this problem with
html_entity_decode()
(Source: PHP.net), like so...Full, Working Demo Online
In this demo, I use
’
and a line from the Tao teh Ching to demonstrate the above use ofhtml_entity_decode()
...Don't forget to replace back those
<
and>
chars, though!Working Demo Sandbox
How Do You Know It Worked?
Want to verify it worked just fine? Then head on over to the W3C RSS Feed Validator, and see the above code being approved as just fine.