views:

135

answers:

2
+1  Q: 

XML Relationship

I am unable to define a relationship between paper and author. Is it possible to define one?

<xsd:complextype name="Researcher'>
</xsd:complextype>
<xsd:complexType name = "Paper" >
      <xsd:extension base = " Researcher " >
      </xsd:extension>
  </xsd:complexType>
<xsd:complexType name = "Author">
      <xsd:extension base = " Researcher ">
      </xsd:extension>
</xsd:complexType>
A: 

It does seem to be a duplicate question.

I think of things in terms of "is a" or "has a" relationships. I guess that C++ class stuck with me.

The relationships that I see are 1. A Paper has an Author. 2. An Author is a Researcher (or perhaps an Author may be a Researcher).

I don't see how a Paper can be a Researcher. It can have an Author, who may be a Researcher.

Michael Mathews
+1  A: 

I assume you want a 'paper' to have an 'author'. The way I do this in my schemas is to have a list of authors and a list of papers. Something like this:

<papers>

<authorlist>
    <author>Bob Barr</author>
    <author>Ron Paul</author>
    <author>Ralph Nader</author>
</authorlist>

<paperlist>
    <paper>
       <title>How to Revert Your Economy to the Gold Standard</title>
       <author>Ron Paul</author>
    </paper>
    <paper>
        <title>Unsafe at any Speed</title>
        <author>Ralph Nader</author>
    </paper>
     <paper>
         <title>How to be a Viable 3rd Party Candidate</title>
         <author>Bob Barr</author>
     </paper>
 </paperlist>
 </papers>

The tabbing is messed up, but in my example every paper/author has to refer to an authorlist/author. I would use schema code similar to this to achieve the desired effect:

<xsd:element name="paper" type="Papers_Type">
    <xsd:unique name="Author_Key">
        <xsd:selector xpath="authorlist/author">
        <xsd:field xpath="text()"/>
    </xsd:key>
    <xsd:keyref name="Paper_Author_AuthorRef" refer="AuthorKey">
         <xsd:selector xpath="paperlist/paper/author"/>
         <xsd:field xpath="text()"/>
    </xsd:keyref>
</xsd:element>

<xsd:complextype name="Papers_Type">
    <!--Enter element definitions to your liking here-->
</xsd:complextype>

So papers/paperlist/paper/author has to correspond to papers/authorlist/author otherwise validation will throw an error. Good luck!

Stephen Friederichs
Nicely done. However, you closed your xsd:unique with xsd:key :)
Eddie