Как XSD может представлять другой файл XML? - PullRequest
0 голосов
/ 08 февраля 2011

если у меня есть эти данные XML 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>

и эта схема 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" />
              <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>

становится 2 таких фрагмента 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>

как я могу использовать одну схему, которая может представлять оба фрагмента с сохранением исходной структуры первого XML. Другими словами, есть ли способ заставить Main.xsd представлять Frag1.xsd + frag2.xsd? Я не хочу, чтобы 2 схемы только одна !! Заранее спасибо

Ответы [ 3 ]

1 голос
/ 08 февраля 2011

Схема, которую вы ищете, выглядит примерно так:

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


Эта схема будет работать для всех 3 фрагментов XML.

0 голосов
/ 08 февраля 2011

Мне кажется, что вы должны сделать модель содержимого для SigmodRecord как вариант xs: с двумя альтернативами: «Авторы» или «Проблема».

Вы получите гораздо больше возможности повторного использования определений в схемеесли вы используете более глобальные объявления элементов верхнего уровня (и, возможно, типа).Такая глубоко вложенная схема, с одним объявлением элемента верхнего уровня, может описывать только один формат входного документа - не предусмотрено, чтобы такие элементы, как Author или Affiliation, появлялись в разных местах в разных сообщениях.

Некоторые людихотел бы использовать атрибут xsi: type для этого типа сценария: два альтернативных типа для элемента SigmodRecord, выбранных в экземпляре с помощью <SigmodRecord xsi:type="authors"> или <SigmodRecord xsi:type="issue">.

0 голосов
/ 08 февраля 2011

Похоже, @ Девендра Д. Чаван уже предоставил вам решение.Я просто хочу предоставить общее правило Любой глобальный элемент, определенный в xsd, может быть потенциальным корневым элементом в экземпляре документа

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...