0

For the following XSD file:

    <?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="stringtype">
  <xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="inttype">
  <xs:restriction base="xs:positiveInteger"/>
</xs:simpleType>
<xs:simpleType name="dectype">
  <xs:restriction base="xs:decimal"/>
</xs:simpleType>
<!-- Tokens -->
<xs:complexType name="RelativeText">
    <xs:attribute name="name" type="stringtype" use="required"/>
    <xs:attribute name="flow" type="stringtype" use="required"/>
    <xs:attribute name="amount" type="inttype"  use="required"/>
</xs:complexType>
<xs:complexType name="LineText">
    <xs:attribute name="name" type="stringtype" use="required"/>
</xs:complexType>
<xs:complexType name="BoxText">
    <xs:attribute name="name" type="stringtype" use="required"/>
    <xs:attribute name="width" type="dectype" use="required" />
    <xs:attribute name="height" type="dectype" use="required" />
    <xs:attribute name="x" type="dectype" use="required" />
    <xs:attribute name="y" type="dectype" use="required" />
</xs:complexType> 
<!-- Settings -->
<!-- Local Settings - per file type -->
<!-- Directories  -->
<xs:complexType name="MonitorDirectoryElementType">
    <xs:attribute name="path" type="stringtype" use="required"/>
</xs:complexType>

<xs:complexType name="OutputDirectoryElementType">
    <xs:attribute name="path" type="stringtype" use="required"/>
</xs:complexType>

<xs:complexType name="LoggingDirectoryElementType">
    <xs:attribute name="path" type="stringtype" use="required"/>
</xs:complexType>

<xs:complexType name="FileExtensionElementType">
    <xs:attribute name="extension" type="stringtype" use="required"/>
</xs:complexType>

<xs:complexType name="LocalSettingsType">
     <xs:all>
        <xs:element name="file-type" type="FileExtensionElementType" maxOccurs="1"/>
        <xs:element name="monitor-directory" type="MonitorDirectoryElementType" maxOccurs="1"/>
        <xs:element name="output-directory" type="OutputDirectoryElementType" maxOccurs="1"/>
        <xs:element name="log-directory" type="LoggingDirectoryElementType" maxOccurs="1"/>
     </xs:all>
</xs:complexType>
<!-- Global Settings -->
<xs:complexType name="ApplicationLogFileType">
    <xs:attribute name="path" type="stringtype" use="required"/>
</xs:complexType>

<xs:complexType name="GlobalSettingsType">
    <xs:all>
        <xs:element name="log-file" type="ApplicationLogFileType" maxOccurs="1"/>
    </xs:all>
</xs:complexType>
<!-- Token Type Wrap Around -->
<xs:complexType name="TokensType">
    <xs:choice maxOccurs="unbounded">
        <xs:element name="line-text" type="LineText" />
        <xs:element name="box-text" type="BoxText" />
        <xs:element name="relative-text" type="RelativeText" />
    </xs:choice>
</xs:complexType>
<!-- Template content -->
<xs:complexType name="templatecontenttype">
    <xs:all>
        <xs:element name="local-settings" type="LocalSettingsType" maxOccurs="1" />
        <xs:element name="tokens" type="TokensType" maxOccurs="1"/>
    </xs:all>
</xs:complexType>
<!-- Main application settings -->
<xs:complexType name="ApplicationConfigurationType">
    <xs:choice maxOccurs="unbounded">
        <xs:element name="global-settings" type="GlobalSettingsType" maxOccurs="1"/>
        <xs:element name="template-content" type="templatecontenttype"  />
    </xs:choice>
</xs:complexType>
<xs:element name="ApplicationConfiguration" type="ApplicationConfigurationType"  />
</xs:schema>

I want to be able to use with an xml like this:

    <?xml version='1.0'?>
<ApplicationConfiguration>
    <global-settings >
        <log-file path="D:\applicationLog.log" />
    </global-settings>
    <template-content>
        <local-settings>
            <file-type extension=".txt" />
            <monitor-directory path="D:\monitor\"/>
            <output-directory path="D:\output"/>
            <log-directory path= "D:\ThisInstanceLogs"/>
        </local-settings>
        <tokens>
            <line-text name="xyz1" />
            <line-text name="xyz12" />
            <relative-text name="xyz123" flow="below" amount="1"/>
            <line-text name="xyz1234" />
            <line-text name="xyz12345" />
            <box-text name="thada" width="100" height="100" x="2" y="3"/>
        </tokens>
    </template-content>
</ApplicationConfiguration>

Where

  • global-settings can appear only once

  • template-content = unlimited times

  • local-settings & tokens - once per each

  • the elemens within tokens=unlimited in any order (even 0 occurences)

  • log-file once & mandatory

    . I feel that I doing a lot of things wrong here ..

1 Answer 1

2

The problem is that you've got an xs:element tag that isn't closed:

<xs:element name="LocalSettings" type="LocalSettingsType" maxOccurs="1">

I'm also seeing errors in this section:

<xs:complexType name="ApplicationConfigurationType">
    <xs:all >
        <xs:element name="global-settings" type="" maxOccurs="1"/>
        <xs:element name="template-content" type="templatecontenttype"  maxOccurs="unbounded"/>
    </xs:all>
</xs:complexType>

The type attribute shouldn't be empty, and maxOccurs must be 0 or 1 in an xs:all element group.

To match your requirements, I think the schema for that section should look something like this:

<xs:complexType name="ApplicationConfigurationType">
    <xs:choice minOccurs="0">
        <xs:sequence>
            <xs:element name="global-settings" type="GlobalSettingsType" />
            <xs:element name="template-content" type="templatecontenttype"
              minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:sequence>
            <xs:element name="template-content" type="templatecontenttype"
              maxOccurs="unbounded" />
            <xs:sequence minOccurs="0">
                <xs:element name="global-settings" type="GlobalSettingsType" />
                <xs:element name="template-content" type="templatecontenttype"
                  minOccurs="0" maxOccurs="unbounded" />
            </xs:sequence>
        </xs:sequence>
    </xs:choice>
</xs:complexType>

At the top level you have a choice - your first element will either be global-settings (the first sequence) or template-content (the second sequence).

If the first element is global-settings, then it can be followed with 0 or more template-content elements, and that's all there is to it.

If the first element is template-content, there can potentially be many of those (thus it's unbounded). And it can optionally be followed by a global-settings element (the nested sequence). If there is a global-settings element, then that may in turn be followed by 0 or more additional template-content elements.

I think this covers all possibilities. Both global-settings and template-content are optional. There can be at most one global-settings elements. And they can appear in any order.

Sign up to request clarification or add additional context in comments.

7 Comments

yes, correct, I noticed now, I ws going to update my answer, still the question remains is my semantic ok in regards with what I what I want to achieve?
@A.K I'm not an expert on XML schemas, but it looks ok to me other than the xs:all on the ApplcationConfigurationType which is probably not what you want if you need to support multiple template-content elements. Also some of your element names don't match up with your actual XML, but I'm sure you'll spot those when you start validating.
It has validated now, after some modifications, now all that remains is the semantic ..
this one still remains: Additional information: Line: 22, Position: 3 "Element 'template-content' cannot appear more than once if content model type is "all"." While trying to have multiple template-content, and I wnat just one global settings
@A.K I see you figured out the need for xs:choice instead of xs:all, but I think your attributes aren't quite right. What you have now allows multiple occurrences of global-settings which I don't think is what you want. I've updated my answer with what I think it should be. Although, looking at it again, I'm not sure mine is perfect either.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.