Restrictions For Elements With The Same Names

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jamil@onepost.net

    Restrictions For Elements With The Same Names

    Take a look at the following XML doc:

    <main>
    <a>
    <element>123</element>
    </a>
    <b>
    <element>abc</element>
    </b>
    <c>
    <element>true </element>
    </c>
    </main>

    With this document, is it possible to create an XSD schema with
    restrictions for the element "element" depending on its ancestor?

    /main/a/element must be an integer containing only three digits.
    /main/b/element must be a string containing only three characters.
    /main/c/element must be a boolean.

    I can define this in a schema if the elements had different names, but
    I do not see a way to do it with the same name of element.

    Thanks.
  • Martin Honnen

    #2
    Re: Restrictions For Elements With The Same Names

    jamil@onepost.n et wrote:
    Take a look at the following XML doc:
    >
    <main>
    <a>
    <element>123</element>
    </a>
    <b>
    <element>abc</element>
    </b>
    <c>
    <element>true </element>
    </c>
    </main>
    >
    With this document, is it possible to create an XSD schema with
    restrictions for the element "element" depending on its ancestor?
    >
    /main/a/element must be an integer containing only three digits.
    /main/b/element must be a string containing only three characters.
    /main/c/element must be a boolean.
    >
    I can define this in a schema if the elements had different names, but
    I do not see a way to do it with the same name of element.
    As those 'element' elements are children of different elements (e.g.
    'a', 'b', 'c') it should not be a problem:

    <xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="main">
    <xs:complexType >
    <xs:sequence>
    <xs:element name="a">
    <xs:complexType >
    <xs:sequence>
    <xs:element name="element">
    <xs:simpleTyp e>
    <xs:restricti on base="xs:int">
    <xs:totalDigi ts value="3"/>
    </xs:restriction>
    </xs:simpleType>
    </xs:element>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:element name="b">
    <xs:complexType >
    <xs:sequence>
    <xs:element name="element">
    <xs:simpleTyp e>
    <xs:restricti on base="xs:string ">
    <xs:length value="3"/>
    </xs:restriction>
    </xs:simpleType>
    </xs:element>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:element name="c">
    <xs:complexType >
    <xs:sequence>
    <xs:element name="element" type="xs:boolea n"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    </xs:schema>

    --

    Martin Honnen

    Comment

    • jamil@onepost.net

      #3
      Re: Restrictions For Elements With The Same Names

      On Mon, 29 Sep 2008 12:59:28 +0200, Martin Honnen <mahotrash@yaho o.de>
      wrote:
      >As those 'element' elements are children of different elements (e.g.
      >'a', 'b', 'c') it should not be a problem:
      An example of doing this was very helpful. Thank you.

      My problem was that I was attempting to do this with a globally named
      type, which wasn't working due to all elements sharing the same name.

      Comment

      Working...