如何使用 XML 架构重用来自另一个架构/命名空间(即 XHTML)的元素? [英] How to reuse elements from another Schema / Namespace (i.e. XHTML) with XML Schema?
问题描述
我正在尝试定义一个允许在某些地方使用特定 (X)HTML 元素的架构.我面临的问题是架构无法编译.
I'm trying to define a Schema which allows the use of specific (X)HTML elements in certain places. The problem that I'm facing is that the Schema fails compiling.
这是架构:
<?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>
这是我收到的错误消息:
That's the error message that I get:
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 架构.
I understand what the error message means, but I do not understand why the error happens. The error message means that when I refer to blockquote, it cannot find blockquote. But I do not understand why this error happens since I'm importing the XHTML 1.1 schema.
我还试图找出这是否是 xmllint
特有的问题.所以我写了一个小 Java 程序来执行 Schema 验证,但我基本上得到了同样的错误.
I also tried to find out whether this is a problem specific to xmllint
. So I wrote a little Java program to perform the Schema validation, but I'm basically getting the same error.
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]);
}
}
这是我从该程序中得到的错误:
Here's the error I'm getting from that program:
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)元素的架构(针对没有命名空间的目标语法)的正确方法是什么?
So clearly, I am doing something wrong in the Schema - but what? What is the correct way to define a Schema (for a target syntax without namespace) that reuses elements from another Schema/Namespace like XHTML?
顺便说一句,我也尝试过 <xs:include/>
但这对我来说似乎不合适,并且它失败了,因为它要求包含架构和包含的架构针对相同的命名空间.>
BTW I also tried <xs:include/>
but that seemed inappropriate to me, and it failed because it requires that the including schema and the included schema target the same namespace.
推荐答案
blockquote"元素好像没有定义为全局元素,所以不能直接引用.如果您查看 xhtml 架构的子部分 http://www.w3.org/MarkUp/SCHEMA/xhtml11-model-1.xsd,你可以注意到它的类型是xhtml.blockquote.type
.
It seems that the "blockquote" element is not defined as global element, thus you can not refer to it directly.
If you have a look in the sub part of the xhtml schema http://www.w3.org/MarkUp/SCHEMA/xhtml11-model-1.xsd, you can notice that its type is xhtml.blockquote.type
.
因此,一种解决方法是像这样声明您的块引用:
So a workaround would be to declare your blockquote like so :
<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 实例:
along with this valid XML instance:
<?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 命名空间.由于您导入了其他命名空间的元素,我认为将默认目标命名空间设置为您自己的元素也是一种更好的做法.
You will notice that the blockquote element is not bound to the html namespace. Since you import other namespace's elements, I think also it would be a better practice to set a default target namespace to your own elements.
这篇关于如何使用 XML 架构重用来自另一个架构/命名空间(即 XHTML)的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!