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

February 26th, 2008, 12:19 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
XML Schema complexType union
Hi all!
Let's say we have these types:
<xs:complexType name="litteralType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="xml:lang" use="required"/>
<xs:attributeGroup ref="ns:attGroupOne"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="gradeType">
<xs:choice maxOccurs="unbounded">
<xs:element name="A"/>
<xs:element name="B"/>
<xs:element name="C"/>
<xs:element name="D"/>
</xs:choice>
</xs:complexType>
How can I declare a new complexType which would be the union of these two complex types? I'm sure there's many solutions but I just can't get it right...
__________________
Dijkstra's law on Programming and Inertia:
If you don't know what your program is supposed to do, don't try to write it.
|
|

February 26th, 2008, 12:32 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
I'm not very familiar with XML schema, but how can you have a type which is an extension of xs:string, and also contains elements? This would seem to be invalid?
If you could give some examples of what valid XML should look like we might be able to work out the schema to validate it, but without more to go on its very hard to guess what you are trying to achieve.
/- Sam Judson : Wrox Technical Editor -/
|
|

February 26th, 2008, 01:00 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Sam!
Yes in fact, my example isn't a good one.
Let's rewind and and have these two types:
<xs:complexType name="tipanouType" mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="chien"/>
<xs:element name="chat"/>
<xs:element name="mouette"/>
<xs:element name="crapaud"/>
</xs:choice>
</xs:complexType>
<xs:complexType name="tipaeuxType" mixed="true">
<xs:choice maxOccurs="unbounded">
<xs:element name="barbotsse"/>
<xs:element name="crapotsse"/>
<xs:element name="marmotsse"/>
</xs:choice>
</xs:complexType>
What I'm trying to do is to create a unified type that embrace both the previous types. Something like:
<xs:complexType name="unifiedType">
<!-- here I'd like to have a way of saying that
"unifiedType" is either a "tipanouType" or
a "tipeaeuxType"
-->
</xs:complexType>
I surely can use an <xs:extension base="ns:tipanouType"> and put the tipaeuxType definition inside, but I'd rather make a "reference" to the tipaeuxType, so if the need comes, I only have to change the tipeaeuxType definition to automatically update the schema.
|
|

February 26th, 2008, 01:08 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Again, not completed familiar with XSD but does the following work:
Code:
<xs:complexType name="newType">
<xs:choice maxOccurs="unbound">
<xs:element ref="ns:tipanouType"/>
<xs:element ref="ns:tipaeuxType"/>
</xs:choice>
</xs:complexType>
/- Sam Judson : Wrox Technical Editor -/
|
|

February 26th, 2008, 01:13 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I don't know if we can ref a named type. I know it works with existing elements, but not types...
Dijkstra's law on Programming and Inertia:
If you don't know what your program is supposed to do, don't try to write it.
|
|

February 26th, 2008, 01:17 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Of course you can. You did in your first post (<xs:attributeGroup ref="ns:attGroupOne"/>)
/- Sam Judson : Wrox Technical Editor -/
|
|

February 26th, 2008, 02:31 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I was referring to an existing attribute group somewhere in the schema...
|
|

February 26th, 2008, 11:49 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
|
|
Quote:
quote:Originally posted by rushman
Dijkstra's law on Programming and Inertia:
If you don't know what your program is supposed to do, don't try to write it.
|
I think this is a misapplication. Dijkstra's law is meant to help steer you away from projects that have failed before they've even begun. If a client comes to you with a lot of talk and you're still not sure what the hell it's supposed to do... pass.
This is totally different. You're learning how to program and the only way to do that is to try different things and see how they break. The more stuff you break, the more you learn. Programming is not for the faint hearted. ;)
-------------------------
Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe
When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper
Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
|
|

February 27th, 2008, 04:56 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>You're learning how to program and the only way to do that is to try different things and see how they break.
I'm not sure how we got here, but I would suggest that when you are learning programming, or a new programming language, it is particularly important to choose problems that a very well defined in terms of their expected output. However, I suspect the quote from Dijkstra was a general-purpose sig and nothing specifically related to this thread.
Getting back to the original question:
(a) both the types look legal. It is entirely reasonable to declare a complex-type-with-simple-content as an extension of a simple type.
(b) how do you declare the union, that is, a type that accepts everything accepted by either of these types and nothing else? I don't think you can. Since one type has simple (string) content, and the other has element content, the union will have mixed content, and this will inevitably allow things that neither type allows individually, for example a sequence containing both a string and an element node.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

February 28th, 2008, 05:51 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hello again!
a) Dijkstra's quote IS in fact part of my sig (which I won't include this time because it seems to confuse some people...)
b) The first example I've posted is NOT good. I'm looking for an answer for the SECOND post I've made on this thread (an union of two complex types).
I can use <xs:extension base="ns:tipanouType"> and extend it with ns:tipaeuxType, or vice versa. That would permit what I'm looking for (using elements from both types).
My concern is more from a semantic point of view. Extending tipanouType or tipaeuxType looks like giving more signifiance to one type than the other. I would like to reflect the notion of UNION of both types. Is it appropriate to use a <xs:group> to reflect this?
I know I may sound picky with the semantics of all this but, eh, it doesn't make me a bad person anyway.
Mister Kay, I hope I'm clear enough.
Thanks,
Rushman
|
|
 |