Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 18th, 2007, 03:51 PM
Authorized User
 
Join Date: Nov 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default Schema: element is choice of complex types?

I'm trying to declare an element which is one of 2 complex types.

I have 2 complex types, say
Code:
<xs:complexType name="ElemType1">
    <xs:sequence>
        <xs:element name="child1"/>
    </xs:sequence>
</xs:complexType>
<xs:complexType name="ElemType2">
    <xs:sequence>
        <xs:element name="child2"/>
    </xs:sequence>
</xs:complexType>
However, I can't quite get the syntax down for the element declaration.
Code:
<xs:element name="root">
    <xs:complexType>
            <xs:choice>
                <xs:complexType name="ElemType1"/>
                <xs:complexType name="ElemType2"/>
            </xs:choice>
    </xs:complexType>
</xs:element>
This will not validate obviously. How is it possible to achieve this?

Note: the actual types are imported from other files, I don't have control over them; otherwise I would simply rewrite the type.

Thanks.
 
Old July 18th, 2007, 04:02 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can copy the content models of the two types:

<xs:choice>
    <xs:sequence>
        <xs:element name="child1"/>
    </xs:sequence>
    <xs:sequence>
        <xs:element name="child2"/>
    </xs:sequence>
</xs:choice>

(actually you can omit the xs:sequence if the content models are as simple as that).

If you don't want to replicate the code, use named model groups

<xs:choice>
  <xs:group ref="g1"/>
  <xs:group ref="g2"/>
</xs:choice>

<xs:complexType name="ElemType1">
   <xs:group ref="g1"/>
</xs:complexType>

<xs:group name="g1">
   <xs:sequence>
        <xs:element name="child1"/>
    </xs:sequence>
</xs:group>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 18th, 2007, 04:30 PM
Authorized User
 
Join Date: Nov 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

I may have mislead you about my access to the other types. These types are created by a 3rd party, and as such can change based on their needs.

So, I can't replicate the content models. Moreover, they are not declared as group s. Is there still a solution?
 
Old July 18th, 2007, 04:54 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

No, you can derive a complex type by extension from an existing type but you can't combine two types into one.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem adding element to the previous element dani1 XSLT 5 September 10th, 2008 01:38 AM
Schema Complex element with restriction problem [email protected] XML 1 June 27th, 2006 11:45 AM
a choice Ritt VS.NET 2002/2003 2 August 25th, 2005 06:33 AM
Is Treeview the right choice? Renu ASP.NET 1.0 and 1.1 Basics 3 October 11th, 2004 03:08 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.