我正在尝试定义一个架构,允许在某些地方使用特定的 (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 时,它找不到 blockquote。但我不明白为什么会发生这个错误,因为我正在导入 XHTML 1.1 架构。
我也试图找出这是否是一个特定的问题xmllint
。所以我写了一个小Java程序来执行模式验证,但我基本上得到了同样的错误。
这是 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.
很明显,我在架构中做错了一些事情 - 但什么?定义重用来自另一个架构/命名空间(如 XHTML)的元素的架构(对于没有命名空间的目标语法)的正确方法是什么?
顺便说一句我也尝试过<xs:include/>
但这对我来说似乎不合适,并且它失败了,因为它要求包含模式和包含模式针对相同的命名空间。