Jump to content

"Any" constraints?


qduda

Recommended Posts

Hello Community,I have a question about any Element in XML Schemas. I'd like to set some kind of constraint... What ever in first my example:

<xsd:element name="BookCatalogue">	<xsd:complexType>		<xsd:sequence>			<xsd:element name="Book" maxOccurs="unbounded">				<xsd:complexType>					<xsd:sequence>						<xsd:element name="Title" 									 type="xsd:string"/>						<xsd:element name="Author" 									 type="xsd:string"/>						<xsd:element name="Date" 									 type="xsd:string"/>						<xsd:element name="ISBN" 									 type="xsd:string"/>						<xsd:element name="Publisher" 									 type="xsd:string"/>						<xsd:any namespace="##any" minOccurs="0"/>					</xsd:sequence>				</xsd:complexType>			</xsd:element>		</xsd:sequence>	</xsd:complexType></xsd:element>

(Got this code from here: http://www.xfront.com/ExtensibleContentModels.html)Well as I understand at the any element I can add any element in a xml file.Okay, so my Problem is that I got a XSD Schema with many "any" elements and my task is it to extend this xsd file. This file got the attribute namespace="##other" which means I can't use the same Namespace as the "basic" xsd file, but this is fine - I'm going to create a new xsd which contains a new namespace of my company. So now I wonder If there is only one Element in this new XSD, per Example:

<xsd:element name="Reviewer">	<xsd:complexType>		 <xsd:sequence>			 <xsd:element name="Name">				 <xsd:complexType>					 <xsd:sequence>						 <xsd:element name="First" 									  type="xsd:string"/>						 <xsd:element name="Last" 									  type="xsd:string"/>					 </xsd:sequence>				 </xsd:complexType>			 </xsd:element>		</xsd:sequence>	</xsd:complexType></xsd:element>

So it is clear that this Element "Reviewer" will be the sub element for "book" and is placed at the "any" location. But how can I define such an element that it can only be placed as sub-element of a specific element. The next example will show what I mean...//// basis.xsd

<xsd:element name="BookCatalogue">	<xsd:complexType>		<xsd:sequence>			<xsd:element name="Book" maxOccurs="unbounded">				<xsd:complexType>					<xsd:sequence>						<xsd:element name="Title" 									 type="xsd:string"/>						<xsd:element name="Author" 									 type="xsd:string"/>						<xsd:element name="Date" 									 type="xsd:string"/>						<xsd:element name="ISBN" 									 type="xsd:string"/>						<xsd:element name="Publisher" 									 type="xsd:string"/>						<xsd:any namespace="##any" minOccurs="0"/>					</xsd:sequence>				</xsd:complexType>			</xsd:element><!-- E-Book element -->			<xsd:element name="E-Book" maxOccurs="unbounded">				<xsd:complexType>					<xsd:sequence>						<xsd:element name="Title" 									 type="xsd:string"/>						<xsd:element name="Author" 									 type="xsd:string"/>						<xsd:element name="Date" 									 type="xsd:string"/>						<xsd:element name="ISBN" 									 type="xsd:string"/>						<xsd:element name="Publisher" 									 type="xsd:string"/>						<xsd:any namespace="##any" minOccurs="0"/>					</xsd:sequence>				</xsd:complexType>			</xsd:element>		</xsd:sequence>	</xsd:complexType></xsd:element>

/// extension XSD

<xsd:element name="Reviewer">	<xsd:complexType>		 <xsd:sequence>			 <xsd:element name="Name">				 <xsd:complexType>					 <xsd:sequence>						 <xsd:element name="First" 									  type="xsd:string"/>						 <xsd:element name="Last" 									  type="xsd:string"/>					 </xsd:sequence>				 </xsd:complexType>			 </xsd:element>		</xsd:sequence>	</xsd:complexType></xsd:element><xsd:element name="Onlinestore">	<xsd:complexType>		 <xsd:sequence>			 <xsd:element name="Website">				 <xsd:complexType>					 <xsd:sequence>						 <xsd:element name="URL" 									  type="xsd:string"/>						 <xsd:element name="Comment" 									  type="xsd:string"/>					 </xsd:sequence>				 </xsd:complexType>			 </xsd:element>		</xsd:sequence>	</xsd:complexType></xsd:element>

So how can I define that the Element "Onlinestore" is only valid as element of E-book and the Element "Reviewer" is valid for Book and Ebook?--> Reviewer is Sub-Element of "Book", "Ebook"--> Onlinestore is Sub-Element of "Ebook"All the Tutorials and Books I read are only pointing out the great advantage of the "any" element and it's possibility... What I see is that there seems to be no way to define what I want, so you can add any "bull######" Element at the "any" element and every XML --> XSD validation tool will validate it as okay. :)I'm pretty sure that I just overseen something and you can point me the correct direction! :)I'll be glad-full for every help and hint. Thanks in advance, Q

Link to comment
Share on other sites

  • 2 weeks later...

Sorry, but is my problem/question so simple/retarded that no one want to answer it, or is it a bigger problem that no one can explain?I checked two more books, from galileo press, w/o anything about this problem... It is just ignored, as everywhere. :/I hope for any advice. :)

Link to comment
Share on other sites

We only have a few seniors who do this sort of thing regularly. (Alas, I'm not one of them.) You may simply need to wait for their timezones to come around. Obviously, they'll have to show up today, also.

Link to comment
Share on other sites

Unfortunatly, you can't do what you're trying to with XML Schema alone. XML Schema 1.0 AFAIK doesn't have such a facility. At the point of the xs:any, the Schema simply lets of control to whatever other schema could represent the other namespace, if any. It can let go control to a specific namespace(s), or simply state ##other as it does.If you have full control over the parent schema (the one with the xs:any element), you could tell each xs:any element to accept only a certain namespace within it, and then make each namespace correspond to the kind of elements you want limited.I'm assuming the basic schema is not authored by you, so modifications to it aren't advisable, right? If that's the case, the other solution is to use Schematron as an extra Schema that will validate those particular constrains. This will require you to have a Schematron validator in addition to the Schema one though.

Link to comment
Share on other sites

Thanks for the reply!Yeah, you are correct, I am not the author of the parent schema and I'm not allowed to modify it, I need to take it "as it is".So what I got is that the only way to achieve what I want is to use Schematron as an extra/master schema which will validate the "dependency" of the parent schema and my child schema. So I'll go this way. By the way I still wonder why there is nothing about this problem in all the literature I read since this problem seems pretty common to me when multiple departments or companies are cooperating in development. Sure you can declare the "any" to use specific namespace, but still ever element from within this namespace might be inserted at the "any"-location.What ever... Thanks for advice! :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...