Simple Question - Simple Answer?

Discussion in 'XML' started by Daniel Frey, Dec 28, 2004.

  1. Daniel Frey

    Daniel Frey Guest

    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
     
    Daniel Frey, Dec 28, 2004
    #1
    1. Advertising

  2. 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).

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 28, 2004
    #2
    1. Advertising

  3. Daniel Frey

    Daniel Frey Guest

    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


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Daniel Frey, Jan 12, 2005
    #3
  4. Daniel Frey wrote:

    > 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
    </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)?


    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.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jan 12, 2005
    #4
  5. Daniel Frey

    Daniel Frey Guest

    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


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Daniel Frey, Jan 12, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. suzy
    Replies:
    3
    Views:
    334
  2. Robocop
    Replies:
    2
    Views:
    445
    Andrew Hobbs
    Feb 22, 2004
  3. Greg Corradini

    Simple mx.ODBC prob seeks simple answer

    Greg Corradini, Apr 6, 2007, in forum: Python
    Replies:
    0
    Views:
    317
    Greg Corradini
    Apr 6, 2007
  4. Steve Holden
    Replies:
    1
    Views:
    406
    Dennis Lee Bieber
    Apr 6, 2007
  5. Kirk

    Simple Question = simple answer?

    Kirk, Sep 23, 2003, in forum: Javascript
    Replies:
    1
    Views:
    159
    Martin Honnen
    Sep 23, 2003
Loading...

Share This Page