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?
If I remember correctly,
xs:ID
has to be globally unique within whole document, whilexs:key
only has to be unique for the element for which it was defined. So thekey/keyref
is actually more like PK/FK. PK only have to be unique within one table.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
andxs: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
orxs: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
:The file
/home/username/comments.xml
:The file
/home/username/authors.xml
: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:
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:
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.
This issue is discussed in http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/structures.html note section 3.11.