Implementing foreign key type relationships in XSD schema

痞子三分冷 提交于 2019-12-05 20:29:49

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.

Tony Wallace

This issue is discussed in http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/structures.html note section 3.11.

If I remember correctly, xs:ID has to be globally unique within whole document, while xs:key only has to be unique for the element for which it was defined. So the key/keyref is actually more like PK/FK. PK only have to be unique within one table.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!