2013-07-01 2 views
0

Для следующего файла XSD:Ошибка при создании XSD схемы для конкретного формата XML

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

Я хочу, чтобы иметь возможность использовать с XML, как это:

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

Где

  • глобальные настройки могут появляться только один раз
  • template-content = неограниченное время
  • местная-настройка & лексема - один раз в каждый
  • в elemens в пределах лексем = неограниченного в любом порядке (даже 0 вхождений)
  • лог-файл один раз & обязательного

    . Я чувствую, что я делаю много вещей здесь не так ..

ответ

2

Проблема заключается в том, что у вас есть xs:element тега, который не закрыт:

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

Я также вижу ошибки в этом разделе:

<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> 

типа атрибут не должен быть пустым, а MaxOccurs должен быть 0 или 1, в xs:all группа элементов.

Чтобы соответствовать вашим требованиям, я думаю, что схема для этой секции должна выглядеть примерно так:

<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> 

На верхнем уровне у вас есть выбор - ваш первый элемент будет либо global-settings (первая последовательность) или template-content (вторая последовательность).

Если первым элементом является global-settings, то за ним могут следовать 0 или более template-content элементов, и это все, что ему нужно.

Если первый элемент равен template-content, потенциально может быть много таких (при этом он неограничен). И за ним необязательно может следовать элемент global-settings (вложенная последовательность). Если есть элемент global-settings, то за ним, в свою очередь, может следовать 0 или более дополнительных template-content элементов.

Я думаю, что это охватывает все возможности. Оба global-settings и template-content являются необязательными. Может быть не более одного global-settings элементов. И они могут появляться в любом порядке.

+0

Да, правильно, я заметил сейчас, я собираюсь обновить свой ответ, но остается вопрос, является ли мой семантический подход в отношении того, что я хочу достичь? – AlexandruC

+0

Да, спасибо, вы правы. – AlexandruC

+0

@AK Я не эксперт по XML-схемам, но для меня это выглядит нормально, кроме «xs: all» в * ApplcationConfigurationType *, который, вероятно, не является тем, что вы хотите, если вам нужно поддерживать несколько шаблонов-содержимого элементы.Кроме того, некоторые из ваших имен элементов не соответствуют вашему фактическому XML, но я уверен, что вы заметите это, когда начнете проверку. –

Смежные вопросы