Laden...

Problem mit XSD und <xscoice>

Erstellt von kunsti vor 10 Jahren Letzter Beitrag vor 9 Jahren 584 Views
K
kunsti Themenstarter:in
89 Beiträge seit 2013
vor 10 Jahren
Problem mit XSD und <xscoice>

Hallo, ich habe folgende XSD Datei mit der ich eine XML Datei validieren möchte.

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="BookList" type ="Media">

</xs:element>
		<xs:complexType name ="Media">	
			<xs:choice maxOccurs="unbounded" minOccurs="0">		
				<xs:element name="Book" maxOccurs="unbounded" minOccurs="0" >
					<xs:complexType>
						<xs:choice>
							
							<xs:element name="ISBN" minOccurs="0" maxOccurs="1">
								<xs:simpleType>
									<xs:restriction base="xs:string">
										<!--ISBN-13 '|' means "or nothing"-->
										<xs:pattern value="([0-9]{3}[\-][0-9]{1,5}[\-][0-9]{2,7}[\-][0-9]{2,7}[\-][0-9]{1})|"/>
										<!--ISBN-10 '|' "means or nothing"-->
										<xs:pattern value="([0-9]{1,5}[\-][0-9]{2,7}[\-][0-9]{2,7}[\-][0-9Xx]{1})|"/>
									</xs:restriction>
								</xs:simpleType>
							</xs:element>
							<xs:element type="NonEmptyString" name="Author" minOccurs="1" maxOccurs="1" nillable="false"/>
							<xs:element type="NonEmptyString" name="Title" minOccurs="1" maxOccurs="1" nillable="false"/>
							<xs:element  name="YearOfPublication" minOccurs="0" maxOccurs="1" nillable="true">
								<xs:simpleType>
									<xs:restriction base="xs:string">
										<xs:pattern value="\s*[0-9]*"/>
									</xs:restriction>
								</xs:simpleType>
							</xs:element>
							<xs:element type="xs:string" name="Genre" minOccurs="0" maxOccurs="1" nillable="true"/>
							<xs:element type="xs:string" name="Bond" minOccurs="0" maxOccurs="1" nillable="true"/>
							<xs:element type="xs:string" name="Catchword" minOccurs="0" maxOccurs="unbounded" nillable="false"/>							
						</xs:choice>					
					</xs:complexType>				
				</xs:element>



				<xs:element name="CD" maxOccurs="unbounded" minOccurs="0" >
					<xs:complexType>
						<xs:choice >
							<xs:element type="NonEmptyString" name="Interpret" minOccurs="1" maxOccurs="1" nillable="false"/>
							<xs:element type="NonEmptyString" name="Title" minOccurs="1" maxOccurs="1" nillable="false"/>
							<xs:element  name="YearOfPublication" minOccurs="0" maxOccurs="1" nillable="true">
								<xs:simpleType>
									<xs:restriction base="xs:string">
										<xs:pattern value="\s*[0-9]*"/>
									</xs:restriction>
								</xs:simpleType>
							</xs:element>
							<xs:element type="xs:string" name="Genre" minOccurs="0" maxOccurs="1" nillable="true"/>
							<xs:element type="xs:string" name="Catchword" minOccurs="0" maxOccurs="unbounded" nillable="false"/>
						</xs:choice>
					</xs:complexType>
				</xs:element>
			</xs:choice>
		</xs:complexType>

	<!-- Non-empty string -->
	<xs:simpleType name="NonEmptyString">
		<xs:restriction base="xs:string">
			<xs:whiteSpace value="collapse"/>
			<xs:minLength value="1" />
			<xs:pattern value=".*[^\s].*" />
		</xs:restriction>
	</xs:simpleType>
</xs:schema>




Da die Reihenfolget der Elemente egal sein soll habe ich choice genommen. Das Funktioniert auch solange ich maxoccurs auf unbounded setze. Dabei wird allerdings maxouccurs der elemente nicht mehr berücktsicht. Wenn ich in meiner XML Datei zum Beispiel zwei Titel habe, sollte die Validierung fehlschlagen.
Lösche ich das aus choice raus, so schlägt die Validierung fehl, auch wenn die XML Datei korrekt ist.

Hier mal eine XML Datei die gültig sein soll:

<BookList>
  <Book>
    <ISBN>333-33-33-X</ISBN>
    <Author>Joanne K. Rowling</Author>
    <Title>Harry Potter und die Kammer des Schreckens</Title>
    <YearOfPublication>1999</YearOfPublication>
  </Book>
  <Book>
    <ISBN>444-44-44-x</ISBN>
    <Author>Joanne K. Rowling</Author>
    <Title>Harry Potter und der Gefangene von Askaban</Title>
  </Book>
  <Book>
    <ISBN>111-111-11-11-1</ISBN>
    <Author>Joanne K. Rowling</Author>
    <Title>Harry Potter und der Stein der Weisen</Title>
    <YearOfPublication>1998</YearOfPublication>
    <Genre>Fantasy</Genre>
    <Catchword>spannend</Catchword>
  </Book>
  <Book>
    <Author>Joanne K. Rowling</Author>
    <Title>Harry Potter und der Halblutprinz</Title>
    <YearOfPublication>1999</YearOfPublication>
  </Book>
</BookList>

Meine Frage ist nun ob ich die Reihenfolge der Elemente beliebige halten kann, ohne maxoccurs und so zu überschreiben.

Gruß
kunsti

476 Beiträge seit 2004
vor 9 Jahren

hallo kunsti,

verwende in deinem Element book xs:all anstatt xs:choice. Weitere Erklärungen findest du auch hier: Wikipedia XML_Schema

-yellow

Selbst ein Weg von tausend Meilen beginnt mit einem Schritt (chinesisches Sprichwort).

Mein Blog: Yellow's Blog auf sqlgut.de