2017-02-09 52 views
1

在我的情況我XSD應該有一個根標籤, #XSD# 如何在XSD中聲明多個組?

<xs:group name="location"> 
    <xs:sequence> 
    <xs:element name="city" type="xs:string"/> 
    <xs:element name="flat_num" type="xs:string"/> 
    <xs:element name="landmark" type="xs:string"/> 
    <xs:element name="street" type="xs:string"/> 
    </xs:sequence> 
</xs:group> 

<xs:group name="student"> 
    <xs:sequence> 
    <xs:element name="firstname" type="xs:string"/> 
    <xs:element name="lastname" type="xs:string"/> 
    <xs:element name="mothername" type="xs:string"/> 
    <xs:element name="fathername" type="xs:string"/> 
    </xs:sequence> 
</xs:group> 

<xs:element name="Student_details" type="details"/> 

<xs:complexType name="details"> 
    <xs:group ref="location"/> 
    <xs:group ref="student"/> 
</xs:complexType> 
</xs:schema> 

當我使用XML驗證,它說這種格式是錯誤的, 任何一個可以幫助在多個組讓我知道如何創建多個組

回答

1

你缺少複雜的數據類型序列

<?xml version="1.0" encoding="utf-8" ?> 
<!--Created with Liquid Studio 2017 - Developer Bundle Edition (Trial) 15.0.0.7089 (https://www.liquid-technologies.com)--> 
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:group name="location"> 
     <xs:sequence> 
      <xs:element name="city" type="xs:string" /> 
      <xs:element name="flat_num" type="xs:string" /> 
      <xs:element name="landmark" type="xs:string" /> 
      <xs:element name="street" type="xs:string" /> 
     </xs:sequence> 
    </xs:group> 
    <xs:group name="student"> 
     <xs:sequence> 
      <xs:element name="firstname" type="xs:string" /> 
      <xs:element name="lastname" type="xs:string" /> 
      <xs:element name="mothername" type="xs:string" /> 
      <xs:element name="fathername" type="xs:string" /> 
     </xs:sequence> 
    </xs:group> 
    <xs:element name="Student_details" type="details" /> 
    <xs:complexType name="details"> 
     <xs:sequence> 
      <xs:group ref="location" /> 
      <xs:group ref="student" /> 
     </xs:sequence> 
    </xs:complexType> 
</xs:schema> 

enter image description here

我也會質疑一個組是否最能實現你想描述的內容。你目前會產生這樣的XML

<?xml version="1.0" encoding="utf-8"?> 
<!-- Created with Liquid Studio 2017 - Developer Bundle Edition (Trial) 15.0.0.7089 (https://www.liquid-technologies.com) --> 
<Student_details xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:noNamespaceSchemaLocation="Schema.xsd"> 
    <city>string</city> 
    <flat_num>string</flat_num> 
    <landmark>string</landmark> 
    <street>string</street> 
    <firstname>string</firstname> 
    <lastname>string</lastname> 
    <mothername>string</mothername> 
    <fathername>string</fathername> 
</Student_details> 
+0

這是工作的感謝,在我的情況下如何將位置組元素設置爲非強制性和學生組爲必修元素。 – Santhosh

+0

Sprotty