Schema for simple XML-based scripting language

Discussion in 'XML' started by Chris Lieb, Jun 1, 2006.

  1. Chris Lieb

    Chris Lieb Guest

    I am new to XML Schema and am running into a bit of a snag. I have
    defined an XML-based scripting language for an updater program that I
    am working on. I would like to make a schema for this language since
    malformed XML documents break the updater. (I did not have time to add
    good error-handling code, so run-time errors can abound if the document
    is malformed.)

    A sample doc might look something like this:

    <manifest>
    <version major="1" minor="0" revision="1" base="1.0.1\files\"
    doprevious="true">
    <file action="copy" name="text.doc"/>
    <folder action="create" name="temp"/>
    <updater>
    <file name="update.exe"/>
    <file name="updaterlib.dll"/>
    </updater>
    </version>
    <version major="1" minor="0" revision="0" base="1.0.0\files\"
    doprevious="false">
    <dll action="register" name="updaterlib.dll"/>
    <cmd location="server" command="patch.bat">
    <file name="patch.dat"/>
    <file name="patch.bat"/>
    </cmd>
    </version>
    </manifest>

    Problem is that the second version element in the document, the updater
    element, the dll element, and the cmd element all get flagged as
    invalid. I guess that this stems from using a sequence in the complex
    type. What should I use to allow all children elements of version
    (file, folder, dll, msi, cmd, updater) to appear in any order and in
    whatever quantity (except updater, which can only appear once)?

    'all' looks like it comes close since it allows you to use elements in
    any order, except my reading tells me that it only allows each element
    to be used once and forces you to use all elements. 'choice' also
    appears to come close since it allows you to choose which element you
    want, but it only lets you choose one element from the set.

    Also, one last thing: How do you set the max occurrence of an element
    to be infinite?

    Thanks in advance,
    Chris

    PS
    I'm not posting the XSD right now. If you want it, I would be more
    than happy to provide it. I am a little embarassed that I am designing
    the XSD in Visual Studio 2005 instead of hand-coding it. I have no
    idea if the XSD generated by VS2005 is considered to be of good quality.
    Chris Lieb, Jun 1, 2006
    #1
    1. Advertising

  2. Hi Chris,

    To specify that children of an element can appear in any order and in any
    quantity, you can use xs:choice with maxOccurs="unbounded":

    <xs:element name="top">
    <xs:complexType>
    <xs:choice maxOccurs="unbounded">
    <xs:element name="e1" type="myType"/>
    <xs:element name="e2" type="myType"/>
    <xs:element name="e3" type="myType"/>
    <xs:element name="e4" type="myType"/>
    </xs:choice>
    </xs:complexType>
    </xs:element>

    This, however, does not enforce any of the elements to appear only once. In
    general, XML Schema does not allow for this type of restriction. It is
    possible for simple elements or simple content elements (elements with no
    child elements) using a uniqueness constraint:

    <xs:element name="top">
    <xs:complexType>
    <xs:choice maxOccurs="unbounded">
    <xs:element name="e1" type="myType"/>
    <xs:element name="e2" type="myType"/>
    <xs:element name="e3" type="myType"/>
    <xs:element name="e4" type="myType"/>
    </xs:choice>
    </xs:complexType>
    <xs:unique name="oneE1">
    <xs:selector xpath="."/>
    <xs:field xpath="mstns:e1"/>
    </xs:unique>
    </xs:element>

    This snippet will enforce only one occurance of "e1" element. However, in
    your case updater element is a complex type element and you won't be able to
    do this. Can you force it to be either the first or the last element? If
    so, the following should do the trick:

    <xs:element name="top">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="updater" type="myComplexType"/>
    <xs:choice maxOccurs="unbounded">
    <xs:element name="e1" type="myType"/>
    <xs:element name="e2" type="myType"/>
    <xs:element name="e3" type="myType"/>
    <xs:element name="e4" type="myType"/>
    </xs:choice>
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    Finally, if you were asking about "Generate Schema" functionality in VS, its
    purpose is to help you get started with your xsd when you have an instance
    document. It generates a schema for you by taking a large number of guesses
    (is it a choice or a sequence? is "1" a number or a string? there're two
    occurances of "x" element, should it always be 2 or should it be unbounded?
    etc.) Given a single instance document, you can generate a number of
    schemas that would validate it. So no matter what schema generation tool
    you use, you will have to go back and twick the schema to do exactly what
    you want.

    Hope this answers your questions. Let me know if something's not clear.

    --
    Stan Kitsis
    Program Manager, XML Tools & Technologies
    Microsoft Corporation

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at
    http://www.microsoft.com/info/cpyright.htm


    "Chris Lieb" <> wrote in message
    news:...
    >I am new to XML Schema and am running into a bit of a snag. I have
    > defined an XML-based scripting language for an updater program that I
    > am working on. I would like to make a schema for this language since
    > malformed XML documents break the updater. (I did not have time to add
    > good error-handling code, so run-time errors can abound if the document
    > is malformed.)
    >
    > A sample doc might look something like this:
    >
    > <manifest>
    > <version major="1" minor="0" revision="1" base="1.0.1\files\"
    > doprevious="true">
    > <file action="copy" name="text.doc"/>
    > <folder action="create" name="temp"/>
    > <updater>
    > <file name="update.exe"/>
    > <file name="updaterlib.dll"/>
    > </updater>
    > </version>
    > <version major="1" minor="0" revision="0" base="1.0.0\files\"
    > doprevious="false">
    > <dll action="register" name="updaterlib.dll"/>
    > <cmd location="server" command="patch.bat">
    > <file name="patch.dat"/>
    > <file name="patch.bat"/>
    > </cmd>
    > </version>
    > </manifest>
    >
    > Problem is that the second version element in the document, the updater
    > element, the dll element, and the cmd element all get flagged as
    > invalid. I guess that this stems from using a sequence in the complex
    > type. What should I use to allow all children elements of version
    > (file, folder, dll, msi, cmd, updater) to appear in any order and in
    > whatever quantity (except updater, which can only appear once)?
    >
    > 'all' looks like it comes close since it allows you to use elements in
    > any order, except my reading tells me that it only allows each element
    > to be used once and forces you to use all elements. 'choice' also
    > appears to come close since it allows you to choose which element you
    > want, but it only lets you choose one element from the set.
    >
    > Also, one last thing: How do you set the max occurrence of an element
    > to be infinite?
    >
    > Thanks in advance,
    > Chris
    >
    > PS
    > I'm not posting the XSD right now. If you want it, I would be more
    > than happy to provide it. I am a little embarassed that I am designing
    > the XSD in Visual Studio 2005 instead of hand-coding it. I have no
    > idea if the XSD generated by VS2005 is considered to be of good quality.
    >
    Stan Kitsis [MSFT], Jun 1, 2006
    #2
    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. Markus
    Replies:
    1
    Views:
    1,495
    Markus
    Nov 23, 2005
  2. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,169
    Stanimir Stamenkov
    Apr 25, 2005
  3. Chris Lieb
    Replies:
    3
    Views:
    573
    Chris Lieb
    Jun 2, 2006
  4. Ron Stephens
    Replies:
    23
    Views:
    2,788
    Ron Stephens
    Apr 12, 2004
  5. DaveInSidney
    Replies:
    0
    Views:
    399
    DaveInSidney
    May 9, 2005
Loading...

Share This Page