0
我有我的XML中可選的標記,如果可選標記存在,那麼我需要該元素的子標記是必需的,我如何使用XSD(或另一種解決方案,如果這在XSD中是不可能的)?我的XML的XSD:如果可選父項存在,則需要嵌套標記
部的一例的:
<xs:element name="deviceLocation" minOccurs="0" maxOccurs="unbounded" nillable="true">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="xPos"/>
<xs:element type="xs:string" name="yPos"/>
<xs:element type="xs:string" name="zPos"/>
</xs:sequence>
</xs:complexType>
</xs:element>
正如你可以看到deviceLocation
標籤是可選的 - 如果此標記存在,我再要孩子標籤需要xPos
,yPos
和zPos
。
編輯:這樣的要求
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="homeML">
<xs:complexType>
<xs:sequence>
<xs:element name="inhabitantDetails" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="inhabitantID" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
<xs:element name="name" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
<xs:element name="carePlan" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
<xs:element name="comment" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="location" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="locationID" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
<xs:element name="locationDescription" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
<xs:element name="referenceObjects" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
<xs:element name="locationDevice" minOccurs="0" maxOccurs="unbounded" nillable="true">
<xs:complexType>
<xs:sequence>
<xs:element name="lDeviceID" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
<xs:element name="deviceDescription" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
<xs:element name="deviceLocation" minOccurs="0" maxOccurs="unbounded" nillable="true">
<xs:complexType>
<xs:sequence>
<xs:element name="xPos" type="xs:string" minOccurs="0"/>
<xs:element name="yPos" type="xs:string" minOccurs="0"/>
<xs:element name="zPos" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="units" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
<xs:element name="realTimeInformation" minOccurs="0" maxOccurs="unbounded" nillable="true">
<xs:complexType>
<xs:sequence>
<xs:element name="runID" type="xs:string" minOccurs="0"/>
<xs:element name="sampleRate" type="xs:string" minOccurs="0"/>
<xs:element name="startTimeStamp" type="xs:dateTime" minOccurs="0"/>
<xs:element name="endTimeStamp" type="xs:dateTime" minOccurs="0"/>
<xs:element name="data" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="event" minOccurs="0" maxOccurs="unbounded" nillable="true">
<xs:complexType>
<xs:sequence>
<xs:element name="lEventID" type="xs:string" minOccurs="0"/>
<xs:element name="timeStamp" type="xs:dateTime" minOccurs="0"/>
<xs:element name="data" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="mobileDevice" minOccurs="0" maxOccurs="unbounded" nillable="true">
<xs:complexType>
<xs:sequence>
<xs:element name="mDeviceID" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
<xs:element name="deviceDescription" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
<xs:element name="units" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
<xs:element name="devicePlacement" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
<xs:element name="quantisationResolution" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
<xs:element name="realTimeInformation" minOccurs="0" maxOccurs="unbounded" nillable="true">
<xs:complexType>
<xs:sequence>
<xs:element name="runID" type="xs:string" minOccurs="0"/>
<xs:element name="sampleRate" type="xs:string" minOccurs="0"/>
<xs:element name="startTimeStamp" type="xs:dateTime" minOccurs="0"/>
<xs:element name="endTimeStamp" type="xs:dateTime" minOccurs="0"/>
<xs:element name="data" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="event" minOccurs="0" maxOccurs="unbounded" nillable="true">
<xs:complexType>
<xs:sequence>
<xs:element name="mEventID" type="xs:string" minOccurs="0"/>
<xs:element name="timeStamp" type="xs:dateTime" minOccurs="0"/>
<xs:element name="data" type="xs:string" minOccurs="0"/>
<xs:element name="support" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="annotationDetails" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="annotationID" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="experimentType" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="lDevice" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
<xs:element name="mDevice" type="xs:string" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
<xs:element name="startTimeStamp" type="xs:dateTime" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="endTimeStamp" type="xs:dateTime" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
我可以確認它確實是' '而不是' '嗎?我總是把XSL理解爲XSD的一個完全獨立的工具。 –
Colin747
對不起,是的,我的手指自動輸入「xsl:」。將更正。 –
感謝您的幫助,只是爲了澄清當你說'type =「someType」'你的意思是簡單的類型,如'type =「xs:string」'或者你的意思是一個用戶定義的類型? – Colin747