Developing a contrained based XSD?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • conradwt@runbox.com

    Developing a contrained based XSD?

    Hi, I was wondering, is it possible to restrict the value of an element
    based on the type element? For example,

    <mytypes>
    <mytype>
    <name>value1</name>
    <type>double</type>
    <value>123.89 </value>
    </mytype>

    <mytype>
    <name>value2</name>
    <type>integer </type>
    <value>123</value>
    </mytype>

    <mytype>
    <name>value3</name>
    <type>short</type>
    <value>123</value>
    </mytype>
    </mytypes>

    Now, the specification would for the XSD would be as follows:

    a) 1 or more 'mytype'
    b) 'type' element should constrain 'value'

    If anyone has any ideas as to how to develop this in XSD, please post
    to the group.

    Thanks in advance,

    -Conrad

  • Martin Honnen

    #2
    Re: Developing a contrained based XSD?



    conradwt@runbox .com wrote:
    [color=blue]
    > Hi, I was wondering, is it possible to restrict the value of an element
    > based on the type element? For example,
    >
    > <mytypes>[/color]
    <mytypes xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    [color=blue]
    > <mytype>
    > <name>value1</name>
    > <type>double</type>
    > <value>123.89 </value>[/color]

    You can use stuff like
    <value xsi:type="xs:do uble">123.89</value>
    if you want to refer to the types XML schema knows.


    --

    Martin Honnen

    Comment

    • Con

      #3
      Re: Developing a contrained based XSD?

      Hi, thanks to responding to my post. Anyway, if type tag is of the
      following:

      short
      int
      long int
      float
      double

      How does enforce the type of value in the value tag?

      Thanks in advance,

      -Conrad

      Comment

      • Stanimir Stamenkov

        #4
        Re: Developing a contrained based XSD?

        /Con/:
        [color=blue]
        > Hi, thanks to responding to my post. Anyway, if type tag is of the
        > following:
        >
        > short
        > int
        > long int
        > float
        > double
        >
        > How does enforce the type of value in the value tag?[/color]

        Define a base type for your "mytype" element, like:

        <xs:complexTy pe name="MyType">
        <xs:sequence>
        <xs:element name="name" type="xs:string " />
        <xs:element name="type" type="ValueType s" />
        <xs:element name="value" type"xs:anySimp leType" />
        </xs:sequence>
        </xs:complexType>

        <xs:element name="mytype" type="MyType" />

        Define the "ValueTypes ":

        <xs:simpleTyp e name="ValueType s">
        <xs:restricti on base="xs:string ">
        <xs:enumerati on value="short" />
        <xs:enumerati on value="int" />
        <xs:enumerati on value="long int" />
        <xs:enumerati on value="float" />
        <xs:enumerati on value="double" />
        </xs:restriction>
        </xs:simpleType>

        Define the derived types:

        <xs:complexTy pe name="MyShortTy pe">
        <xs:complexCont ent>
        <xs:restricti on base="MyType">
        <xs:sequence>
        <xs:element name="name" type="xs:string " />
        <xs:element name="type" type="ValueType s" fixed="short" />
        <xs:element name="value" type"xs:short" />
        </xs:sequence>
        </xs:restriction>
        <xs:complexCont ent>
        </xs:complexType>

        <!-- and so on -->

        Then use the xsi:type attribute in the instance document:

        <mytype>
        <name>value2</name>
        <type>int</type>
        <value>ivalid still valid</value>
        </mytype>

        <mytype xsi:type="MySho rtType">
        <name>value3</name>
        <type>short</type>
        <value>123</value>
        </mytype>

        I haven't tried it but I think it should work. :-) Similar example
        <http://www.w3.org/TR/xmlschema-0/#abstract> is given in the "XML
        Schema Part 0: Primer", although it forces the usage of the xsi:type
        attribute, declaring the base type abstract.

        --
        Stanimir

        Comment

        • Con

          #5
          Re: Developing a contrained based XSD?

          Hey Stanimir, is it possible to not use the xsi:type="MySho rtType"? I
          would like to check validation based on the value of type tag and the
          value tag.

          Thanks in advance,

          -Conrad

          Comment

          • Stanimir Stamenkov

            #6
            Re: Developing a contrained based XSD?

            /Con/:
            [color=blue]
            > Hey Stanimir, is it possible to not use the xsi:type="MySho rtType"? I
            > would like to check validation based on the value of type tag and the
            > value tag.[/color]

            AFAIK, using XML Schema - it is not possible.

            --
            Stanimir

            Comment

            • Stanimir Stamenkov

              #7
              Re: Developing a contrained based XSD?

              /Stanimir Stamenkov/:[color=blue]
              > /Con/:
              >[color=green]
              >> Hey Stanimir, is it possible to not use the xsi:type="MySho rtType"? I
              >> would like to check validation based on the value of type tag and the
              >> value tag.[/color]
              >
              > AFAIK, using XML Schema - it is not possible.[/color]

              You may use substitution groups, however... where you get
              differently named elements for the different types:

              ...

              <xs:element name="mytype" type="MyType" abstract="true" />

              ...

              <xs:element name="myshortty pe" substitutionGro up="mytype">
              <xs:complexType >
              <xs:complexCont ent>
              <xs:restricti on base="MyType">
              <xs:sequence>
              <xs:element name="name" type="xs:string " />
              <xs:element name="type" minOccurs="0"
              type="ValueType s" fixed="short" />
              <xs:element name="value" type"xs:short" />
              </xs:sequence>
              </xs:restriction>
              </xs:complexConte nt>
              </xs:complexType>
              </xs:element>

              <xs:element name="myinteger type" substitutionGro up="mytype">
              ...
              </xs:element>

              ...

              Now wherever there's a "mytype" element particle in the schema,
              there should be one of the elements from the substitution group
              headed by "mytype" in the instance document. The "mytype" itself
              can't appear in the instance when declared abstract in the schema.
              You would notice the "type" child becomes obsolete, in the above
              example.

              <xs:schema ...
              <xs:element name="mytypes">
              <xs:complexType >
              <xs:sequence>
              <xs:element ref="mytype" maxOccurs="unbo unded" />
              </xs:sequence>
              </xs:complexType>
              </xs:element>
              ...
              </xs:schema>


              <mytypes>

              <myshorttype>
              <name>value1</name>
              <value>123</value>
              </myshorttype>

              <myintegertyp e>
              <name>value2</name>
              <value>12345678 9</value>
              <myintegertyp e>

              </mytypes>


              Still, I suppose it is not exactly what you want. :-)


              * http://www.w3.org/TR/xmlschema-0/#SubsGroups

              --
              Stanimir

              Comment

              Working...