Embedding Foreign Key Relationships in an XSD Schema

I'm trying to wrap my head around xml schemas, and one thing I'm trying to figure out is how to make relational type schemas when an element refers to another, possibly in a different schema altogether. I looked at xsd: key and xsd: keyref, and it seems to me that I'm interested, but I'm not sure. Initially, I simply set attributes with type xs: ID abd xs: IDREF, which obviously does not necessarily refer to a specific element, as far as I could tell.

Basically, I have several different xml files where the elements refer to other elements either in the same file or in other files. It is very similar to a relationship database, and I would like to use it, but the requirement is to use only XML files, and therefore I am at least trying to establish some sanity instead of seemingly random strings relying on xml comments to determine the relationship. It works for small projects, but it certainly does not scale.

Any thoughts?

+3
source share
3 answers

XML-, XML . xs:id xs:key ( ..) xpath . XML- 1: , .

-XML , (, , -), , XML- . XML, ( xs:import xs:include) - XML , XML- XML. - .

, , , , , , , XML , , , , , , , , . , :

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

-XML , Entity. , 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>

XML, , . XML-, XML , xpath .

+5

If I remember correctly, it xs:IDmust be globally unique within the entire document, and it xs:keymust be unique for the element for which it was defined. So it key/keyrefactually looks more like PK / FK. PK must be unique within the same table.

0
source

Source: https://habr.com/ru/post/1704267/


All Articles