INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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!

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

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Jobs from Indeed

XSD validation - choose between an attributeGroup
2

XSD validation - choose between an attributeGroup

XSD validation - choose between an attributeGroup

(OP)
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

RE: XSD validation - choose between an attributeGroup

(OP)
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

RE: XSD validation - choose between an attributeGroup

2
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

RE: XSD validation - choose between an attributeGroup

(OP)
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

RE: XSD validation - choose between an attributeGroup

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

RE: XSD validation - choose between an attributeGroup

(OP)
Ah, type inheritance...got it. Much like an interface in object-oriented language. Thanks to you, the XSD is now valid.

Nick Ruiz

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

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!

Resources

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