简体   繁体   English

XSD支持具有不同名称的顶级元素

[英]XSD to support top element with different names

I need an XSD to support an XML that may have different top element name, but all elements within XML are the same. 我需要一个XSD来支持可能具有不同顶级元素名称的XML,但XML中的所有元素都是相同的。

Not sure if possible, worth asking. 不确定是否可能,值得问。

For example: 例如:

<abc>
  <name></name>
  <address></address>
  <phone>
    <home></home>
    <cell></cell>
  </phone>
</abc>

<xyz>
  <name></name>
  <address></address>
  <phone>
    <home></home>
    <cell></cell>
  </phone>
</zyx>

Example XSD: 示例XSD:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="abc">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="xs:string" name="name"/>
        <xs:element type="xs:string" name="address"/>
        <xs:element name="phone">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="home"/>
              <xs:element type="xs:string" name="cell"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Sure, simply name the complexType , define it globally, and reference it in the declarations of the possible root elements: 当然,只需命名complexType ,全局定义它,并在可能的根元素的声明中引用它:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified"
           elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="abc" type="CommonType"/>
  <xs:element name="xyz" type="CommonType"/>
  <xs:complexType name="CommonType">
    <xs:sequence>
      <xs:element type="xs:string" name="name"/>
      <xs:element type="xs:string" name="address"/>
      <xs:element name="phone">
        <xs:complexType>
          <xs:sequence>
            <xs:element type="xs:string" name="home"/>
            <xs:element type="xs:string" name="cell"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM