我正在尝试定义一个架构,该架构允许在某些地方使用特定的(X)HTML元素。我面临的问题是架构无法编译。
这是模式:
<?xml version="1.0"?>
<xs:schema
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:import
namespace="http://www.w3.org/1999/xhtml"
schemaLocation="http://www.w3.org/MarkUp/Schema/xhtml11.xsd"
/>
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element
ref="html:blockquote"
minOccurs="0"
maxOccurs="unbounded"
/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
那是XML文件:
<foo xmlns:html="http://www.w3.org/1999/xhtml">
<html:blockquote>The quick brown fox jumped over the lazy dog.</html:blockquote>
</foo>
那就是我得到的错误信息:
christian@armor01:~/testCase$ xmllint -schema foo.xsd -noout foo.xml
foo.xsd:12: element element: Schemas parser error : Element \
'{http://www.w3.org/2001/XMLSchema}element', attribute 'ref': The QName value \
'{http://www.w3.org/1999/xhtml}blockquote' does not resolve to a(n) element \
declaration.
WXS schema foo.xsd failed to compile
我理解错误消息的含义,但是我不理解为什么会发生错误。该错误消息表示当我引用blockquote时,找不到它quotequote。但是我不明白为什么由于导入XHTML 1.1模式而发生此错误。
我还试图找出这是否是特定于的问题xmllint
。所以我写了一个Java程序来执行Schema验证,但是基本上我得到了同样的错误。
这是Java程序:
import java.io.*;
import java.net.*;
import javax.xml.*;
import javax.xml.parsers.*;
import javax.xml.validation.*;
import org.w3c.dom.*;
public class Validate {
public static void main(final String... args) throws Exception {
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//dbf.setValidating(true);
dbf.setSchema(schemaFactory.newSchema(new File(args[0]).toURI().toURL()));
final DocumentBuilder db = dbf.newDocumentBuilder();
final Document doc = db.parse(args[1]);
}
}
这是我从该程序中得到的错误:
christian@armor01:~/testCase$ java Validate foo.xsd foo.xml
Exception in thread "main" org.xml.sax.SAXParseException; \
systemId: file:/home/christian/testCase/foo.xsd; \
lineNumber: 12; \
columnNumber: 88; \
src-resolve: Cannot resolve the name 'html:blockquote' to a(n) 'element declaration' component.
很明显,我在模式中做错了什么-但是呢?定义可重用来自另一个Schema / Namespace(例如XHTML)的元素的Schema(针对不带名称空间的目标语法)的正确方法是什么?
顺便说一句,我也尝试过,<xs:include/>
但是这对我来说似乎不合适,并且失败了,因为它要求包含模式和包含模式以相同的名称空间为目标。
似乎“ blockquote”元素未定义为全局元素,因此您不能直接引用它。如果您查看xhtml模式http://www.w3.org/MarkUp/SCHEMA/xhtml11-model-1.xsd的子部分,您会注意到它的类型为xhtml.blockquote.type
。
因此,一种解决方法是像这样声明您的blockquote:
<xs:schema xmlns:html="http://www.w3.org/1999/xhtml" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://www.w3.org/1999/xhtml" schemaLocation="http://www.w3.org/MarkUp/Schema/xhtml11.xsd"/>
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="blockquote" type="html:xhtml.blockquote.type" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
连同此有效的XML实例:
<?xml version="1.0"?>
<foo xmlns:html="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">
<blockquote><html:p>quick brown fox jumped over the lazy dog.</html:p></blockquote>
</foo>
您会注意到blockquote元素未绑定到html命名空间。由于您导入了其他名称空间的元素,因此我认为将默认目标名称空间设置为您自己的元素也是一种更好的做法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句