how xsd can represent different xml file?

前端 未结 3 588
心在旅途
心在旅途 2021-01-28 11:57

if I have this XML data Main.xml

  
 
  11 
  1 
 
 &         


        
相关标签:
3条回答
  • 2021-01-28 12:50

    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

    0 讨论(0)
  • 2021-01-28 12:52

    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 讨论(0)
  • 2021-01-28 12:53

    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.

    0 讨论(0)
提交回复
热议问题