Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSD validation - choose between an attributeGroup 2

Status
Not open for further replies.

nwruiz

Programmer
Jun 22, 2005
60
US
Hello,

I am relatively new to designing XSD's and have been fairly successful, aside from the problem listed below:

Code:
<!-- Numeric tags -->
	<xs:attributeGroup name="Number">
		<xs:attribute name="precision" use="optional">
			<xs:simpleType>
				<!-- Precision must be 0-10 decimals -->
				<xs:restriction base="xs:integer">
					<xs:minInclusive value="0" />
					<xs:maxInclusive value="10" />
				</xs:restriction>
			</xs:simpleType>
		</xs:attribute>
		<xs:attribute name="value" type="xs:decimal" use="optional" />
	</xs:attributeGroup>
	<!-- Textual tags -->
	<xs:attributeGroup name="Text">
		<xs:attribute name="justification" use="optional">
			<xs:simpleType>
				<!-- Justification must be left/right/center -->
				<xs:restriction base="xs:string">
					<xs:enumeration value="left" />
					<xs:enumeration value="center" />
					<xs:enumeration value="right" />
				</xs:restriction>
			</xs:simpleType>
		</xs:attribute>
		<xs:attribute name="value" type="xs:string" use="optional" />
	</xs:attributeGroup>
	<!-- Column data type -->
	<xs:element name="Column">
		<xs:complexType>
			<xs:choice>
				<xs:group ref="Text" />
				<xs:group ref="Number" />
				<xs:attribute name="value" type="xs:date"/>
			</xs:choice>
			<xs:attribute name="name" type="xs:string" />
		</xs:complexType>
	</xs:element>

In this code, I am attempting to define a Column tag that has attributes that pertain either to the Text or Number attributeGroups, or the "value" attribute denoted as xs:date type. What is the syntax necessary to perform this choice selection on a group of attributes, as opposed to elements?

Thank you for your time.

Nick Ruiz
 
FYI...my schema header is defined as

Code:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="[URL unfurl="true"]http://tempuri.org/XMLSchema.xsd"[/URL] elementFormDefault="qualified"
	xmlns="[URL unfurl="true"]http://tempuri.org/XMLSchema.xsd"[/URL] xmlns:mstns="[URL unfurl="true"]http://tempuri.org/XMLSchema.xsd"[/URL]
	xmlns:xs="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema">[/URL]

Just wanted to include that before someone yells at me...:)

Nick Ruiz
 
This requirement tests the border of the co-occurence constraint wxsl can barely handle. To do so you have to be prepared to change the xml document to accommodate XMLSchema-instance namespace. Maybe this previous thread may give you the idea.
 
So, instead of being able to define the schema as this:
Code:
	<xs:element name="Column">
		<xs:complexType>
			<xs:choice>
				<xs:group ref="Text" />
				<xs:group ref="Number" />
				<!-- <xs:attribute name="value" type="xs:date"/> -->
			</xs:choice>
			<xs:attribute name="name" type="xs:string" />
		</xs:complexType>
	</xs:element>

I would need to describe a Column tag via XML with one of these?
Code:
<Column xsi:type="Text" justification="left"/>
<Column xsi:type="Number" precision="4"/>

I assume this means that I would not be able to strongly type my Column element, correct? Please let me know if I am mistaken.

Nick Ruiz
 
[1]>So, instead of being able to define the schema as this:
<!-- etc etc excerpt -->
<xs:choice>
<xs:group ref="Text" />
<xs:group ref="Number" />
<!-- <xs:attribute name="value" type="xs:date"/> -->
</xs:choice>
<!-- etc etc excerpt -->

Without looking more into the meaning, the use of xs:choice and xs:group here is already incorrectly applied. xs:choice wouldn't take xs:attributeGroup as its child. The xs:group could only be xs:element and some others defined by the recommendation. So fundamentally, the construction is problematic.

[2]>I assume this means that I would not be able to strongly type my Column element,...
As far as I am concerned, if you succeed in setting up a correct schema to validate the Column against, it is "enough" strongly type (whatever the meaning).

[3] I can show you by isolating the critical part of parent and child where define Column how this is/can be done.

[3.0] The xml document you would like to build... (incomplete)
[tt]
<root xmlns=" <Column name="pi" precision="3" value="3.1416" />
<Column name="abc" justification="left" value="xyz" />
</root>
[/tt]
[3.1] This would be the schema to validate the complete (adjusted) xml document (shown in [3.2])
[tt]
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace=" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns=" xmlns:xs=" <xs:attributeGroup name="Number">
<xs:attribute name="precision" use="optional">
<xs:simpleType>
<!-- Precision must be 0-10 decimals -->
<xs:restriction base="xs:integer">
<xs:minInclusive value="0" />
<xs:maxInclusive value="10" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="value" type="xs:decimal" use="optional" />
</xs:attributeGroup>
<!-- Textual tags -->
<xs:attributeGroup name="Text">
<xs:attribute name="justification" use="optional">
<xs:simpleType>
<!-- Justification must be left/right/center -->
<xs:restriction base="xs:string">
<xs:enumeration value="left" />
<xs:enumeration value="center" />
<xs:enumeration value="right" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="value" type="xs:string" use="optional" />
</xs:attributeGroup>

[green]<!-- [1] First you construct an abstract type which contains some common attribute(s), ie name here and later extend it into different complexType -->[/green]
<xs:complexType name="abstract_columnType" abstract="true">
<xs:attribute name="name" type="xs:string" use="optional" />
</xs:complexType>

[green]<!-- [2] Then you build the schema based on this abstract type -->[/green]
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="Column" type="abstract_columnType" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

[green]<!-- [3] And then you build the top-level bare complexType not directly referred to anywhere in the schema document, but only referred to in the xml document instance to be validated -->[/green]
<xs:complexType name="numberType">
<xs:complexContent>
<xs:extension base="abstract_columnType">
<xs:attributeGroup ref="Number" />
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="textType">
<xs:complexContent>
<xs:extension base="abstract_columnType">
<xs:attributeGroup ref="Text" />
</xs:extension>
</xs:complexContent>
</xs:complexType>

</xs:schema>
[/tt]
[3.3] The final form of an instance of the xml document is this.
[tt]
<root xmlns=" xmlns:xsi=" <Column xsi:type="numberType" name="pi" precision="3" value="3.1416" />
<!-- etc etc (minOccurs maxOccurs) -->
<Column xsi:type="textType" name="abc" justification="left" value="xyz" />
<!-- etc etc (minOccurs maxOccurs) -->
</root>
[/tt]
I think the document would then be validated with the xsd. Maybe you could check it and find it validated too.
 
Ah, type inheritance...got it. Much like an interface in object-oriented language. Thanks to you, the XSD is now valid.

Nick Ruiz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top