我想在我的xml文件中创建类似的内容:
<myfile>
<screen>
<img src="a.jpg"/>
</screen>
</myfile>
我尝试以这种方式做到这一点: XML文件
<?xml version="1.0" encoding="UTF-8"?>
<myfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="myfile.xsd">
<screen>
<img src="a.jpg"/>
</screen>
</myfile>
XSD文件
<?xml version="1.0"?>
<xsd:schema version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xsd:import namespace="http://www.w3.org/1999/xhtml" schemaLocation="xhtml.xsd"/>
<xsd:element name="myfile">
<xsd:element name="screen">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="xhtml:img"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:element>
但firefox不显示任何错误,但我看不到任何图像:/ 有人可以帮忙吗?
答案 0 :(得分:2)
显示图片
Firefox作为浏览器,显示用XHTML或HTML编写的网页。在这种情况下,它将显示标记为<img>
元素的图像。
否则,它不会对<img>
元素执行任何特殊操作。例如,如果您在自己的自定义XML文档中间有<img>
个元素,则Firefox不知道它是什么。
为了正确显示图像,解决方案是创建an XHTML document。
<强>命名空间强>
由于您特别询问了名称空间...您的XML文档不会针对您的模式进行验证,因为您的模式需要XHTML命名空间中的元素,但XML文档中的元素不在命名空间中。
要解决此问题,请更改XML文档的以下行
<myfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="myfile.xsd">
到
<myfile xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="myfile.xsd">
默认名称空间声明xmlns="http://www.w3.org/1999/xhtml"
表示“此元素,所有没有名称空间前缀的后代都在XHTML名称空间中。”
请注意,位于特定名称空间且正在由某个架构验证是XML文档的独立属性。 (实际上前者是元素或属性的属性,而不是整个文档的属性。)模式使用名称空间,但两者不一样。