我不知道 XML Schema 中有任何东西可以让您相互验证多个 XML 文档。在 xs:id 和 xs:key (etc) 约束中,您使用 xpath 来应用约束。您可以转到 XML Schema Part 1: Structures 并向下滚动一点以查看示例以查看这些约束的实际作用。
如果您有能力定义一个包含您的其他文件的元 XML 文件(如果没有其他方式,可能通过实体引用)然后使用该元文件的架构,那么您应该能够使用 XML 架构来应用你的约束。如果您为每种 XML 文件类型定义一个模式,您应该能够轻松地(通过xs:import 或xs:include)为一个 XML 文件定义一个元模式,该 XML 文件在一个 XML 文件中包含您的所有 XML 内容。此元模式可以成功应用您想要的约束。
假设您必须验证一个包含许多帖子的 Wiki,其中每个帖子都有一个作者,可能还有许多 cmets,其中每个评论也有一个作者,并且您有一个用于所有帖子的 XML 文件,一个用于所有 cmets,一个适用于所有作者,并且您想要验证这些文件之间的约束,每个帖子使用存在的作者和 cmets,每个评论使用存在的作者,等等。假设您有以下三个文件:
文件/home/username/posts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<posts>
<post>
<author name="author1"/>
<comment id="12345" pos="1"/>
<comment id="12346" pos="2"/>
<body>I really like my camera...</body>
</post>
...
</posts>
文件/home/username/comments.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<comments>
<comment id="12345" author="kindguy">
That was a very good post
</comment>
...
</comments>
文件/home/username/authors.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<authors>
<author name="kindguy" id="1"/>
<author name="author1" id="2"/>
...
</authors>
我的建议是使用Entity References 创建一个元 XML 文件。例如,您可以创建以下 XML 文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!ENTITY postfile SYSTEM "file:///home/username/posts.xml">
<!ENTITY commentfile SYSTEM "file:///home/username/comments.xml">
<!ENTITY authorfile SYSTEM "file:///home/username/authors.xml">
<root>
&postfile1;
&commentfile;
&authorfile;
</root>
这个元 XML 文件(实际上,一个普通的旧 XML 文件......“元”只是从您定义的三个 XML 文件的角度来看,而不是任何 XML 意义上的)与以下文件完全等效,并且 XML 解析器将像您真正拥有以下文件一样工作:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<posts>
<post>
<author name="author1"/>
<comment id="12345" pos="1"/>
<comment id="12346" pos="2"/>
<body>I really like my camera...</body>
</post>
...
</posts>
<comments>
<comment id="12345" author="kindguy">
That was a very good post
</comment>
...
</comments>
<authors>
<author name="kindguy" id="1"/>
<author name="author1" id="2"/>
...
</authors>
</root>
从 this 文件中,您可以定义一个 XML 模式来应用所需的约束,即使对于单个文件没有办法应用约束。由于使用 XML 实体表示法,您已将所有 XML “包含”到一个文件中,因此您可以在约束引用中使用 xpath。