开发者

how xsd can represent different xml file?

if I have this XML data Main.xml

  <SigmodRecord>
 <issue>
  <volume>11</volume> 
  <number>1</number> 
 <articles>
 <article>
  <title>Annotated Bibliography on Data Design.</title> 
  <initPage>45</initPage> 
  <endPage>77</endPage> 
 <authors>
  <author position="00">Anthony I. Wasserman</author> 
  <author position="01">Karen Botnich</author> 
  </authors>
  </article>
 <article>
  <title>Architecture of Future Data Base Systems.</title> 
  <initPage>30</initPage> 
  <endPage>44</endPage> 
 <authors>
  <author position="00">Lawrence A. Rowe</author> 
  <author position="01">Michael Stonebraker</author> 
  </authors>
  </article>
 <article>
  <title>Database Directions III Workshop Review.</title> 
  <initPage>8</initPage> 
  <endPage>8</endPage> 
 <authors>
  <author position="00">Tom Cook</author> 
  </authors>
  </article>
 <article>
  <title>Errors in 'Process Synchronization in Database Systems'.</title> 
  <initPage>9</initPage> 
  <endPage>29</endPage> 
 <authors>
  <author position="00">Philip A. Bernstein</author> 
  <author position="01">Marco A. Casanova</author> 
  <author position="02">Nathan Goodman</author> 
  </authors>
  </article>
  </articles>
  </issue>
</SigmodRecord>

and this schema Main.xsd

<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="SigmodRecord">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="issue">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="volume" type="xsd:int" />
              <xsd:element name="number" type="xsd:int" /&开发者_Python百科gt;
              <xsd:element name="articles">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element maxOccurs="unbounded" name="article">
                      <xsd:complexType>
                        <xsd:sequence>
                          <xsd:element name="title" type="xsd:string" />
                          <xsd:element name="initPage" type="xsd:int" />
                          <xsd:element name="endPage" type="xsd:int" />
                          <xsd:element name="authors">
                            <xsd:complexType>
                              <xsd:sequence>
                                <xsd:element maxOccurs="unbounded" name="author">
                                  <xsd:complexType>
                                    <xsd:attribute name="position" type="xsd:int" />
                                  </xsd:complexType>
                                </xsd:element>
                              </xsd:sequence>
                            </xsd:complexType>
                          </xsd:element>
                        </xsd:sequence>
                      </xsd:complexType>
                    </xsd:element>
                  </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

it becomes 2 fragments like this Frag1.xml:

 <SigmodRecord>
 <issue>
  <volume>11</volume> 
  <number>1</number> 
 <articles>
 <article>
  <title>Annotated Bibliography on Data Design.</title> 
  <initPage>45</initPage> 
  <endPage>77</endPage> 
  </article>
 <article>
  <title>Architecture of Future Data Base Systems.</title> 
  <initPage>30</initPage> 
  <endPage>44</endPage> 
  </article>
 <article>
  <title>Database Directions III Workshop Review.</title> 
  <initPage>8</initPage> 
  <endPage>8</endPage> 
  </article>
 <article>
  <title>Errors in 'Process Synchronization in Database Systems'.</title> 
  <initPage>9</initPage> 
  <endPage>29</endPage> 
  </article>
  </articles>
  </issue>
</SigmodRecord>

Frag2.xml:

 <SigmodRecord>
<authors>
  <author position="00">Anthony I. Wasserman</author> 
  <author position="01">Karen Botnich</author> 
  </authors>
 <authors>
  <author position="00">Lawrence A. Rowe</author> 
  <author position="01">Michael Stonebraker</author> 
  </authors>
 <authors>
  <author position="00">Tom Cook</author> 
  </authors>
 <authors>
  <author position="00">Philip A. Bernstein</author> 
  <author position="01">Marco A. Casanova</author> 
  <author position="02">Nathan Goodman</author> 
  </authors>
</SigmodRecord>

how can I use one schema that can represent both of Fragments with keeping the original structure of the first XML. In other words, is there any way to make Main.xsd represent Frag1.xsd+frag2.xsd I do not want 2 schema only one!!? Thanks in advance


The schema you are looking for looks something like this,

<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="SigmodRecord">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="issue">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="volume" type="xsd:int" />
              <xsd:element name="number" type="xsd:int" />
              <xsd:element name="articles">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element maxOccurs="unbounded" name="article">
                      <xsd:complexType>
                        <xsd:sequence>
                          <xsd:element name="title" type="xsd:string" />
                          <xsd:element name="initPage" type="xsd:int" />
                          <xsd:element name="endPage" type="xsd:int" />
                          <xsd:element ref="authors"/>
                        </xsd:sequence>
                      </xsd:complexType>
                    </xsd:element>
                  </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
        <xsd:element ref="authors"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="authors">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element maxOccurs="unbounded" name="author">
          <xsd:complexType>
            <xsd:attribute name="position" type="xsd:int" />
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>


This schema will work for all the 3 xml fragments.


Seems like @Devendra D. Chavan already provided a solution for you. I just want to provide general rule Any global element defined in xsd can be a potential root element in the instance document


It looks to me as if you should make the content model for SigmodRecord be an xs:choice with the two alternatives being Authors or Issue.

You'll get much more reusability of the definitions in the schema if you use more global top-level element (and perhaps type) declarations. A deeply nested schema like this, with only one top-level element declaration, can only describe one input document format - there's no provision for elements such as Author or Affiliation appearing in different places in different messages.

Some people like to make use of the xsi:type attribute for this kind of scenario: two alternative types for the SigmodRecord element, selected in the instance by using <SigmodRecord xsi:type="authors"> or <SigmodRecord xsi:type="issue">.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜