xsd如何表示不同的xml文件?

时间:2011-02-08 01:13:04

标签: xml xsd

如果我有这个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 个答案:

答案 0 :(得分:1)

您正在寻找的架构看起来像这样,

<?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片段。

答案 1 :(得分:0)

似乎@ Devendra D. Chavan 已经为您提供了解决方案。我只想提供一般规则 xsd中定义的任何全局元素都可以是实例文档中的潜在根元素

答案 2 :(得分:0)

在我看来,您应该将SigmodRecord的内容模型设为xs:choice,其中两个选项是作者或问题。

如果使用更多全局顶级元素(也许是类型)声明,您将在模式中获得更多的定义可重用性。像这样的深层嵌套模式只有一个顶级元素声明,只能描述一种输入文档格式 - 在不同的消息中不存在出现在不同位置的作者或关联等元素。

有些人喜欢在这种情况下使用xsi:type属性:SigmodRecord元素的两种替代类型,在实例中使用<SigmodRecord xsi:type="authors"><SigmodRecord xsi:type="issue">选择。