#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Am I missing something here? (XSD)


    Hey everyone,

    For the past few days I've been trying to produce an XSD schema that will validate my simple products XML. At first I tried it with the full XML and ran into several issues, so I tried to take things back a step.

    For this very simple XML:

    Code:
    <?xml version="1.0"?>
    <products>
        <product>
            <title>Product A</title>
        </product>
    </products>
    I have this schema:

    Code:
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="products">
            <xs:sequence>
                <xs:element name="product">
                    <xs:sequence>
                        <xs:complexType>
                            <xs:element name="title" type="xs:string" />
                        </xs:complexType>
                    </xs:sequence>
                </xs:element>
            </xs:sequence>
        </xs:element>
    </xs:schema>
    However when I try and validate this using an online tool (tried several), I get back this error message:

    4s-elt-must-match.1: The content of 'products' must match (annotation?, (simpleType | complexType)?, (unique | key | keyref)*)). A problem was found starting at: sequence.
    I feel like I'm missing something really fundamental here, but just can't figure out what?

    Edit

    I also tried the following schema:

    Code:
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="products">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="product">
                        <xs:sequence>
                            <xs:complexType>
                                <xs:element name="title" type="xs:string" />
                            </xs:complexType>
                        </xs:sequence>
                    </xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    Neither work.

    Any help would be extremely appreciated!

    Thanks

    Adam
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    191
    Rep Power
    49
    Code:
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
      <xs:element name="products">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="product"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="product">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="title"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="title" type="xs:string"/>
    </xs:schema>
    Helmut Hagemann Germany

    fallen to the bottom of the facts?
    I reach my hand and we go together


    wer lesen und google kann ist klar im Vorteil
    who read and google is able is clear in the advantage
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0
    Thanks. Looking at that, I'm guessing you can't have a complexType within another?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0
    Okay I managed to take things a step further, and add in some of the missing values:

    Code:
    <?xml version="1.0"?>
    <products>
        <product>
            <title>Product A</title>
            <stock>
                <available>123</available>
                <ordered>123</ordered>
                <delivery>2011-06-01</delivery>
            </stock>
            <stats>
                <total_sold>123</total_sold>
                <per_order_average>123.12</per_order_average>
                <all_order_average>123.12</all_order_average>
            </stats>
        </product>
    </products>
    XSD:

    Code:
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    
        <xs:element name="products">
            <xs:complexType>
                <xs:sequence>
                    <xs:element ref="product" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    
        <xs:element name="product">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="title" type="xs:string" />
                    <xs:element ref="stock" />
                    <xs:element ref="stats" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    
        <xs:element name="stock">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="available" type="xs:integer" />
                    <xs:element name="ordered" type="xs:integer" />
                    <xs:element name="delivery" type="xs:date" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    
        <xs:element name="stats">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="total_sold" type="xs:integer" />
                    <xs:element name="per_order_average" type="xs:decimal" />
                    <xs:element name="all_order_average" type="xs:decimal" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    
    </xs:schema>
    This validates! Great stuff. How could I add an attribute to <product> though?

    I've tried adding it like:

    Code:
        <xs:element name="product">
            <xs:complexType>
                <xs:attribute name="id" type="xs:integer" />
    And several other variations, but no luck!

    Thanks for your help.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    191
    Rep Power
    49
    Code:
    <xs:element name="product">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="title"/>
          </xs:sequence>
    	  <xs:attribute ref="id" use="required"/>
        </xs:complexType>
    	
      </xs:element>
      
      <xs:attribute name="id" type="xs:integer"/>
    Helmut Hagemann Germany

    fallen to the bottom of the facts?
    I reach my hand and we go together


    wer lesen und google kann ist klar im Vorteil
    who read and google is able is clear in the advantage

IMN logo majestic logo threadwatch logo seochat tools logo