Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

XSD validation - choose between an attributeGroupHelpful Member!(2) 

nwruiz (Programmer)
7 May 07 16:12
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

nwruiz (Programmer)
7 May 07 16:20
FYI...my schema header is defined as

CODE

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

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

Nick Ruiz

Helpful Member!(2)  tsuji (TechnicalUser)
9 May 07 2:05
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.
    http://tek-tips.com/viewthread.cfm?qid=1249253
nwruiz (Programmer)
9 May 07 9:57
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

tsuji (TechnicalUser)
9 May 07 11:01
[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)

<root xmlns="http://tempuri.org/XMLSchema.xsd">
    <Column name="pi" precision="3" value="3.1416" />
    <Column name="abc" justification="left" value="xyz" />
</root>

[3.1] This would be the schema to validate the complete (adjusted) xml document (shown in [3.2])

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <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>

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

    <!-- [2] Then you build the schema based on this abstract type -->
    <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>

    <!-- [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 -->
    <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>

[3.3] The final form of an instance of the xml document is this.

<root xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <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>

I think the document would then be validated with the xsd. Maybe you could check it and find it validated too.
nwruiz (Programmer)
9 May 07 11:27
Ah, type inheritance...got it. Much like an interface in object-oriented language. Thanks to you, the XSD is now valid.

Nick Ruiz

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close