Simple Question - Simple Answer?

D

Daniel Frey

Hello,

Maybe you know howto. I want to have an element which takes a
subelement. Depending on an attribute within this subelement, a second
subelement may have different attributes allowed:

001 <Service>
002 <Header Kind="1"/>
003 <Input Data="asdasd"/>
004 </Service>
005 <Service>
006 <Header Kind="2"/>
007 <Input User="me" App="main"/>
008 </Service>

How can I specify that in an XML Schema? I tried the following:

001 <xsd:complexType name="service">
002 <xsd:choice minOccurs="1" maxOccurs="1">
003 <xsd:element name="Service" type="kind1"/>
004 <xsd:element name="Service" type="kind2"/>
005 </xsd:choice>
006 </xsd:complexType>
007
008 <xsd:complexType name="kind1">
009 <xsd:sequence>
010 <xsd:element name="Header" type="header1"/>
011 <xsd:element name="Input" type="input1"/>
012 </xsd:sequence>
013 </xsd:complexType>
014
015 <xsd:complexType name="kind2">
016 <xsd:sequence>
017 <xsd:element name="Header" type="header2"/>
018 <xsd:element name="Input" type="input2"/>
019 </xsd:sequence>
020 </xsd:complexType>
021
022 <xsd:complexType name="header1">
023 <xsd:attribute name="Kind" use="required"
type="header1Att"/>
024 </xsd:complexType>
025
026 <xsd:simpleType name="header1Att">
027 <xsd:restriction base="xsd:string">
028 <xsd:pattern value="1"/>
029 </xsd:restriction>
020 </xsd:simpleType>
031
032 <xsd:complexType name="input1">
033 <xsd:attribute name="Data" use="required"
type="xsd:string"/>
034 </xsd:complexType>
035
036 <xsd:complexType name="header2">
037 <xsd:attribute name="Kind" use="required"
type="header2Att"/>
038 </xsd:complexType>
039
030 <xsd:simpleType name="header2Att">
041 <xsd:restriction base="xsd:string">
042 <xsd:pattern value="2"/>
043 </xsd:restriction>
044 </xsd:simpleType>
045
046 <xsd:complexType name="input2">
047 <xsd:attribute name="User" use="required"
type="xsd:string"/>
048 <xsd:attribute name="App" use="required"
type="xsd:string"/>
049 </xsd:complexType>

However, doublicate definitions of an element of the same name seem
not to be allowed, which makes sense. But I don't know how to define
this sort of structur e then. Any ideas?

Thanks in advance
Daniel Frey
 
M

Martin Honnen

Daniel Frey wrote:

Maybe you know howto. I want to have an element which takes a
subelement. Depending on an attribute within this subelement, a second
subelement may have different attributes allowed:

W3C XML schema doesn't allow you to model such a restriction (that is
attribute of one element defining attributes of another element).
 
D

Daniel Frey

Ok, I'd like to make a slight different document structure

001 <Service Kind="1"/>
002 <Input Data="asdasd"/>
003 </Service>
004 <Service Kind="2"/>
005 <Input User="me" App="main"/>
006 </Service>

I put the kind into the parent element. What I'd like here is that the
kind is defining which attributes of the *contained* element are
allowed. Is this possible (by the W3C standard)?

Thanks
Daniel
 
M

Martin Honnen

Daniel said:
Ok, I'd like to make a slight different document structure

001 <Service Kind="1"/>
002 <Input Data="asdasd"/>
003 </Service>
004 <Service Kind="2"/>
005 <Input User="me" App="main"/>
006 </Service>

What you have there is not even well-formed, it has no root element, the
<Service Kind="1"/> is an empty tag but later on you have a closing tag
I put the kind into the parent element. What I'd like here is that the
kind is defining which attributes of the *contained* element are
allowed. Is this possible (by the W3C standard)?

It is certainly allowed by the XML specification to have different
attributes.
Neither DTD nor W3C schema allow you to put restrictions on the type of
attributes of one element based on the value of the parent element.
 
D

Daniel Frey

You are totally right with the closing element. And thanks for ansering
the question so fast!

I think then I'll have to switch definitively to another structure like
that:

001 <ServiceA>
002 <Input Data="asdasd"/>
003 </ServiceA>
004 <ServiceB>
005 <Input User="me" App="main"/>
006 </ServiceB>

I think in this case this is the only solution to validate it against a
schema.

Thanks
Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top