1

My xml does not match because of xs:alternative from xsd.

XML code

<?xml version="1.0" encoding="UTF-8"?>
<install xmlns="h4a:install" xmlns:h4a="https://www.hive-4-apps.org/xml-shemas/install.xsd" >
    <database>
        <tables>
           <table name="foo">
               <column name="foo_id"    type="bigint(20)" />
                <column name="foo_name"     type="varchar(255)" />
                <column name="foo_type"     type="varchar(35)" />
                <column name="foo_type_id"  type="int(40)"/>
                <key type="unique">
                    <column ref="foo_name"/>
                    <column ref="foo_type"/>
                </key>
                <key type="index" name="foo_type_id" col_name="form_type_id" />
            </table>
        </tables>
    </database>
</install>

XSD code

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema elementFormDefault="qualified" targetNamespace="h4a:install" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="install" type="h4a:installType" xmlns:h4a="h4a:install"/>
    <xs:complexType name="installType">
        <xs:sequence>
            <xs:element type="databaseType" name="database"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="columnType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                 <xs:attribute type="xs:string"  name="name"     use="required"/>
                 <xs:attribute type="xs:string"  name="type"     use="required"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    <xs:complexType name="columnKeyType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute type="xs:string"  name="ref" use="required"/> 
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    <xs:complexType name="tableType">
        <xs:sequence>
            <xs:element type="columnType" name="column" maxOccurs="unbounded" minOccurs="0"/>
            <xs:element type="keyType" name="key" maxOccurs="unbounded" minOccurs="0">
                <xs:alternative type="keyIndexType" test="@type='index'"/>
                <xs:alternative test="@type='unique'" type="KeyUniqueType"/>
            </xs:element>
        </xs:sequence>
        <xs:attribute type="xs:string" name="name" use="required"/>
    </xs:complexType>
    <xs:complexType name="tablesType">
        <xs:sequence>
            <xs:element type="tableType" name="table" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="databaseType">
        <xs:sequence>
            <xs:element type="tablesType" name="tables" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="keyType">
         <xs:attribute name="type" use="required">
             <xs:simpleType>
                 <xs:restriction base="xs:string">
                     <xs:pattern value="index|unique"/>
                 </xs:restriction>
             </xs:simpleType>
        </xs:attribute>   
    </xs:complexType>
    <xs:complexType name="keyIndexType">
        <xs:simpleContent>
            <xs:extension base="keyType">
                <xs:attribute type="xs:string" name="name" use="required"/>
                <xs:attribute type="xs:string" name="col_name" use="required"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
   <xs:complexType name="KeyUniqueType">
       <xs:complexContent>
            <xs:extension base="keyType">
                <xs:sequence>
                    <xs:element type="columnKeyType" name="column" maxOccurs="unbounded"/>  
                </xs:sequence>
            </xs:extension>
       </xs:complexContent>
    </xs:complexType>
</xs:schema>

I tried several possibilities without success. What is wrong in my xsd ?

J.BizMai
  • 2,621
  • 3
  • 25
  • 49
  • 1
    Don't put images of XML in a question about XML - put a complete, useful example of your input XML **as text**. – Matt Hogan-Jones Mar 09 '18 at 16:16
  • Your example input XML is not a **complete** or useful example. It needs to be a valid XML document, not just a fragment - if I'm going to try validating it with your XSD I don't want to have to try and make it complete myself. Help us to help you. – Matt Hogan-Jones Mar 09 '18 at 16:54
  • I also just tried using an XSD tool to generate a sample XML file from your XSD and your XSD itself is not a valid XSD document. – Matt Hogan-Jones Mar 09 '18 at 17:33
  • @MattJones I put the complete xml and xsd, I had to remove all unuseful tags before to be a clearer code. – J.BizMai Mar 09 '18 at 17:48
  • @MattJones, I used this [tool](http://www.xmlforasp.net/SchemaValidator.aspx) with current code I put in this page and I go this error : **Status: The 'http://www.w3.org/2001/XMLSchema:alternative' element is not supported in this context.** – J.BizMai Mar 09 '18 at 17:56
  • 1
    As I [answered to your previous question](https://stackoverflow.com/a/49196350/290085), `xs:alternative` requires an XSD **1.1** processor. – kjhughes Mar 09 '18 at 19:19
  • @kjhughes, yes the tool seems to run with XSD 1.0 processor but in PHPStorm, there are no erros in the xsd file (so the XSD 1.1 processor is running) but in xml file there are some errors. – J.BizMai Mar 09 '18 at 19:31

1 Answers1

1

It seems to be a problem of XSD 1.1 processor (again). Actually, PHPStorm support so a part of XML Shema 1.1.

See here

According to PHPStorm, there are no error in my xsd, but it does not validate correctly XML files with xs:alternative.

All XML Shema 1.1 validator are paid ( oxygen, Altova ) except a java tool not really useful for my case : xerces

XML tool of Notepad++ support only XSD 1.0.

Conclusion : either find a workaround with xsd 1.0, or accept to get errors until PHPStorm support correctly it.

J.BizMai
  • 2,621
  • 3
  • 25
  • 49