在XSD模式中实现外键类型关系 [英] Implementing foreign key type relationships in XSD schema

查看:176
本文介绍了在XSD模式中实现外键类型关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把我的头围绕XML模式,我试图弄清楚的一件事是如何做关系型模式的元素引用另一个,可能在另一个架构完全。我看过xsd:key和xsd:keyref,看起来像是我感兴趣的东西,但我不确定。最初,我只是用类型xs:ID abd xs:IDREF来设置属性,就我所知,这显然不一定指向特定的元素。



基本上,我有几个不同的XML文件,其中元素引用其他元素在同一个文件或其他文件。它看起来很像一个关系数据库,我很乐意使用它,但是要求只使用XML文件,所以我至少要试图建立一些完整性,而不是看起来随机的字符串依靠xml注释来定义关系。它适用于较小的项目,但它肯定不是可扩展的。



有什么想法?

解决方案 div>

我不知道XML Schema中的任何内容,这将允许您验证多个XML文档。在 xs:id xs:key (etc)约束中,使用xpath来应用约束。您可以访问 XML Schema Part 1:Structures 并向下滚动如果你有能力定义一个元XML文件,包括你的其他人(也许通过实体引用,如果通过没有其他的方式),然后使用该元文件的模式,那么你应该能够使用XML模式来应用你的约束。如果你为每个XML文件类型定义一个模式,你应该可以平凡地(通过 xs:import xs:include )为一个XML文件定义一个元模式,在一个XML文件中包含所有的XML内容。这个元模式可以成功地应用你想要的约束。假设你必须验证一个有很多帖子的Wiki,每个帖子都有一个作者,也许很多评论每个评论也有一个作者,并且你有一个XML文件的所有职位,一个为所有评论,一个为所有作者,你想验证这些文件之间的约束,每个职位使用作者和评论存在,每个评论都使用存在的作者,等等。假设您有以下三个文件:
$ b

文件 /home/username/posts.xml

p>

 <?xml version =1.0encoding =UTF-8?> 
< posts>
< post>
< author name =author1/>
< comment id =12345pos =1/>
< comment id =12346pos =2/>
< body>我真的很喜欢我的相机...< / body>
< / post>
...
< / posts>

文件 /home/username/comments.xml

 <?xml version =1.0encoding =UTF-8?> 
<评论>
< comment id =12345author =kindguy>
这是一个非常好的帖子
< / comment>
...
< / comment>

文件 /home/username/authors.xml

 <?xml version =1.0encoding =UTF-8?> 
< authors>
< author name =kindguyid =1/>
< author name =author1id =2/>
...
< /作者>

我的建议是使用实体参考资料。例如,您可以创建以下XML文件:

 <?xml version =1.0encoding =UTF-8 ?> 
<!ENTITY postfile SYSTEMfile:///home/username/posts.xml>
<!ENTITY commentfile SYSTEMfile:///home/username/comments.xml>
<!ENTITY authorfile SYSTEMfile:///home/username/authors.xml>
< root>
& postfile1;
& commentfile;
& authorfile;
< / root>

这个元XML文件(实际上是一个普通的旧XML文件...元仅从您定义的三个XML文件的角度来看,而不是以任何XML的方式)与以下文件的确切等价物,并且XML解析器的行为就像您确实拥有以下文件一样:

 <?xml version =1.0encoding =UTF-8?> 
< root>
< posts>
< post>
< author name =author1/>
< comment id =12345pos =1/>
< comment id =12346pos =2/>
< body>我真的很喜欢我的相机...< / body>
< / post>
...
< / posts>
<评论>
< comment id =12345author =kindguy>
这是一个非常好的帖子
< / comment>
...
< / comment>
< authors>
< author name =kindguyid =1/>
< author name =author1id =2/>
...
< /作者>
< / root>

这个文件中,您可以定义一个XML模式,即使对于单个文件也没有办法应用约束。由于使用XML实体表示法,您已将所有XML包含到一个文件中,因此可以在contraint引用中使用xpath。


I'm trying to wrap my head around xml schemas and one thing I'm trying to figure out is how to do relational type schemas where on element refers to another, possibly in another schema altogether. I've looked at the xsd:key and xsd:keyref and it seems like the sort of thing I'm interested in, but I'm not sure. Initially I just set attributes with the type xs:ID abd xs:IDREF, which obviously doesn't necessarily refer to a specific element as far as I could tell.

Basically, I have several different xml files where elements refer to other elements either in the same file or other files. It looks a lot like a relation database and I would love to use one, but the requirement is to only use XML files and so I'm at least trying to establish some sanity instead of just seemingly random strings relying on xml comments to define the relationships. It works for smaller projects, but it's certainly not scalable.

Any thoughts?

解决方案

I'm not aware of anything within XML Schema that will allow you to validate multiple XML documents against one another. In the xs:id and xs:key (etc) constraints, you use xpath to apply the constraints. You can go to XML Schema Part 1: Structures and scroll down a little bit for the example to see these constraints in action.

If you have the ability to define a meta-XML file that includes your others (perhaps by entity references if by no other way) and then use a schema for that meta file, then you should be able to use XML Schema to apply your constraints. If you define a schema for each of your XML file types, you should be able to trivially (by xs:import or xs:include) define a meta-schema for an XML file that includes all of your XML content in one XML file. This meta-schema could successfully apply the constraints you want.

Let's say you have to validate a Wiki that has many posts, where each post has an author and maybe many comments where each comment also has an author, and that you have one XML file for all posts, one for all comments, one for all authors, and you want to validate constraints between these files, that each post uses authors and comments that exist, that each comment uses authors that exist, and so on. Let's say you have the following three files:

The file /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>

The file /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>

The file /home/username/authors.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<authors>
  <author name="kindguy" id="1"/>
  <author name="author1" id="2"/>
   ...
</authors>

What I am suggesting is that you make a meta-XML file by using Entity References. For example, you could create the following XML file:

<?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>

This meta-XML file (actually, a plain old XML file ... the "meta" is only from the perspective of your three defined XML files, and not in any XML sense) is the exact equivalent of the following file, and XML parsers will act as if you truly had the following file:

<?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>

From this file, you can define an XML schema that will apply the desired constraints, even though with the individual files there is no way to apply constraints. Since using XML entity notation you have "included" all the XML into one file, you can use xpath in the contraint references.

这篇关于在XSD模式中实现外键类型关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆