Issues writing XSD file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stephen1313
    New Member
    • Sep 2010
    • 9

    Issues writing XSD file

    Learning XML and need some help.
    I have am xml file and I have want to create an xsd file for it.
    I have written my file, but am experiencing errors when trying to validate the xml to my xsd.
    Any pointing in the right direction you can give would be appreciated.

    XML file:
    Code:
    <?xml version="1.0"?>
    <letter date="2005/1/1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://some-where letter.xsd">
    
    <to>
    <first>smith</first>
    <last>Lee</last>
    </to>
    <from>
    <first>Sylvie</first>
    <middle>S.</middle>
    </from>
    <title>Example</title>
    <msg>
    <paragraph>
    I have a question.
    <paragraph>
    Can you infer an XML Schema?
    </paragraph>
    </paragraph>
    </msg>
    
    </letter>
    My XSD file:
    Code:
    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="letter">
    </xs:element>
    <xs:complexType>
    <xs:element name="to" type="xs:normalizednormalizedString"/>
    <xs:element name="from" type="xs:normalizedString"/>
    <xs:element name="heading" type="xs:normalizedString"/>
    <xs:element name="body" type="xs:normalizedString"/>
    </xs:complexType>
    <xs:attribute>
    <xs:attribute name="last" type="xs:normalizedString"/>
    <xs:attribute name="first" type="xs:normalizedString"/>
    <xs:attribute name="middle" type="xs:normalizedString"/>
    <xs:attribute name="title" type="xs:normalizedString"/>
    <xs:attribute name="msg" type="xs:normalizedString"/>
    <xs:attribute name="paragraph" type="xs:normalizedString"/>
    </xs:attribute>
    </xs:schema>
    Here are the errors I am getting:

    Ln 8 Col 64 - s4s-elt-invalid-content.1: The content of 'null' is invalid. Element 'element' is invalid, misplaced, or occurs too often.
    Ln 7 Col 18 - s4s-att-must-appear: Attribute 'name' must appear in element 'complexType'.
    Ln 13 Col 15 - s4s-att-must-appear: Attribute 'name' must appear in element 'attribute'.
    Ln 14 Col 58 - s4s-elt-must-match.1: The content of '(no name)' must match (annotation?, (simpleType?)). A problem was found starting at: attribute.
    Last edited by jkmyoung; Sep 17 '10, 01:31 PM. Reason: code tags
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Your formation of schema doesn't fit standards. See this example for where to place your elements and attributes within a complexType:
    Code:
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    	<xs:element name="beer">
    		<xs:complexType>
    			<xs:sequence>
    				<xs:element name="a"/>
    				<xs:element name="b"/>
    			</xs:sequence>
    			<xs:attribute name="attb"/>
    			<xs:attribute name="atta"/>
    		</xs:complexType>
    	</xs:element>
    </xs:schema>

    Comment

    • stephen1313
      New Member
      • Sep 2010
      • 9

      #3
      Thanks for your reply.
      Here is what I have now.
      My XSD file:
      Code:
      <?xml version="1.0"?>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
      <xs:element name="letter">
      	<xs:complexType>
      	<xs:sequence>
      	<xs:element name="to" type="xs:normalizednormalizedString"/>
        	<xs:element name="from" type="xs:normalizedString"/>
      	<xs:element name="title" type="xs:normalizedString"/>
      	<xs:element name="msg" type="xs:normalizedString"/>
      	</xs:sequence>
      	<xs:attribute name="date" type="xs:normalizedString"/>
        	<xs:attribute name="last" type="xs:normalizedString"/>
      	<xs:attribute name="first" type="xs:normalizedString"/>
      	<xs:attribute name="middle" type="xs:normalizedString"/>
      	<xs:attribute name="paragraph" type="xs:normalizedString"/>
      	</xs:complexType> 	   
      </xs:element>
      </xs:schema>
      My errors:
      Ln 6 Col 62 - src-resolve.4.2: Error resolving component 'xs:normalizedn ormalizedString '. It was detected that 'xs:normalizedn ormalizedString ' is in namespace 'http://www.w3.org/2001/XMLSchema', but components from this namespace are not referenceable from schema document 'file:///C:/XML/lettersl.xsd'. If this is the incorrect namespace, perhaps the prefix of 'xs:normalizedn ormalizedString ' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:///C:/XML/lettersl.xsd'.
      Ln 13 Col 10 - cvc-type.3.1.2: Element 'from' is a simple type, so it must have no element information item [children].
      Ln 22 Col 9 - cvc-type.3.1.2: Element 'msg' is a simple type, so it must have no element information item [children].
      Last edited by jkmyoung; Sep 17 '10, 06:48 PM.

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        You probably mean 'xs:normalizedS tring' (one normalized)

        Your from and msg nodes are actually a complex element containing other elements. eg 'from' from your code:
        Code:
        <from> 
          <first>Sylvie</first> 
          <middle>S.</middle> 
        </from>
        Put in another element definition inside much like your first one.
        Example of nesting element:
        Code:
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
            <xs:element name="beer"> 
                <xs:complexType> 
                    <xs:sequence> 
                        <xs:element name="a">
                        <xs:complexType>
                          <xs:sequence>
        	                <xs:element name="a_child1"/>                      
                      	                <xs:element name="a_child2"/>                      
                          </xs:sequence>
                        </xs:complexType>
                        </xs:element> 
                        <xs:element name="b"/> 
                    </xs:sequence> 
                    <xs:attribute name="attb"/> 
                    <xs:attribute name="atta"/> 
                </xs:complexType> 
            </xs:element> 
        </xs:schema>
        Example of using reference instead:
        Code:
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        	<xs:element name="beer">
        		<xs:complexType>
        			<xs:sequence>
        				<xs:element ref="a"/>
        				<xs:element name="b"/>
        			</xs:sequence>
        			<xs:attribute name="attb"/>
        			<xs:attribute name="atta"/>
        		</xs:complexType>
        	</xs:element>
        	<xs:element name="a">
        		<xs:complexType>
        			<xs:sequence>
        				<xs:element name="a_child1"/>
        				<xs:element name="a_child2"/>
        			</xs:sequence>
        		</xs:complexType>
        	</xs:element>
        </xs:schema>

        Other option, change your source xml to attributes:
        First and middle are elements not attributes. An equivalent with attributes might be:
        <from first="Sylivie" middle="S."/>

        Comment

        • stephen1313
          New Member
          • Sep 2010
          • 9

          #5
          Ok, here is my code now:
          Code:
          <?xml version="1.0"?>
          <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
          <xs:element name="letter">
          <xs:complexType>
          	<xs:sequence>
          		<xs:element name="to" type="xs:normalizednormalizedString"/>
          				<xs:element name="first" type="xs:normalizedString"/>
          				<xs:element name="last" type="xs:normalizedString"/>
          				</xs:sequence>
          				</xs:complexType>
          	<xs:complexType>
          	<xs:sequence>
          		<xs:element name="from" type="xs:normalizedString"/>
          				<xs:element name="first" type="xs:normalizedString"/>
            				<xs:element name="middle" type="xs:normalizedString"/>
            				</xs:sequence>
            				</xs:complexType>
            	<xs:complexType>
            	<xs:sequence>
            		<xs:element name="title" type="xs:normalizedString"/>
          		<xs:element name="msg" type="xs:normalizedString"/>
          		<xs:element name="paragraph" type="xs:normalizedString"/>
          				</xs:sequence>
            				</xs:complexType>		
          </xs:element>
          </xs:schema>
          Here are my errors now:
          Ln 11 Col 18 - s4s-elt-must-match.1: The content of 'letter' must match (annotation?, (simpleType | complexType)?, (unique | key | keyref)*)). A problem was found starting at: complexType.
          Ln 6 Col 63 - src-resolve.4.2: Error resolving component 'xs:normalizedn ormalizedString '. It was detected that 'xs:normalizedn ormalizedString ' is in namespace 'http://www.w3.org/2001/XMLSchema', but components from this namespace are not referenceable from schema document 'file:///C:/XML/lettersl.xsd'. If this is the incorrect namespace, perhaps the prefix of 'xs:normalizedn ormalizedString ' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:///C:/XML/lettersl.xsd'.
          Ln 4 Col 61 - cvc-complex-type.3.2.2: Attribute 'date' is not allowed to appear in element 'letter'.
          Ln 10 Col 9 - cvc-complex-type.2.4.a: Invalid content was found starting with element 'from'. One of '{first}' is expected.
          Last edited by jkmyoung; Sep 17 '10, 06:48 PM.

          Comment

          • stephen1313
            New Member
            • Sep 2010
            • 9

            #6
            Here is an update for those of you helping me out:
            I have updated my code to the following:

            Code:
            <?xml version="1.0"?>
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
            
            
            <xs:element name="letter">
            <xs:attribute name="date" type="xs:normalizedString"/>
            <xs:complexType>
            <xs:sequence>
            <xs:element name="to" type="xs:normalizednormalizedString"/>
            <xs:element name="first" type="xs:normalizedString"/>
            <xs:element name="last" type="xs:normalizedString"/>
            </xs:sequence>
            </xs:complexType>
            <xs:complexType>
            <xs:sequence>
            <xs:element name="from" type="xs:normalizedString"/>
            <xs:element name="first" type="xs:normalizedString"/>
            <xs:element name="middle" type="xs:normalizedString"/>
            </xs:sequence>
            </xs:complexType>
            <xs:complexType>
            <xs:sequence>
            <xs:element name="title" type="xs:normalizedString"/>
            <xs:element name="msg" type="xs:normalizedString"/>
            <xs:element name="paragraph" type="xs:normalizedString"/>
            </xs:sequence>
            </xs:complexType>
            
            </xs:element>
            </xs:schema>
            Now I am down to only 1 error on validation of my XML file:
            Ln 6 Col 56 - s4s-elt-must-match.1: The content of 'letter' must match (annotation?, (simpleType | complexType)?, (unique | key | keyref)*)). A problem was found starting at: attribute.
            Last edited by jkmyoung; Sep 17 '10, 06:49 PM.

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              Less errors is not always better...

              <xs:attribute name="date" type="xs:normal izedString"/>
              Should be inside the complexType.

              Have you changed your source file at all? Your 'from' element is currently set to a simple element as opposed to a complex one.

              Comment

              • stephen1313
                New Member
                • Sep 2010
                • 9

                #8
                Editing the XML file is not an option. I have to make my XSD file conform to the XML.
                I did move the date attribute as you instructed.
                I am still getting this error:
                Ln 6 Col 56 - s4s-elt-must-match.1: The content of 'letter' must match (annotation?, (simpleType | complexType)?, (unique | key | keyref)*)). A problem was found starting at: attribute.

                Would I be better off using ANY rather than complexType?

                Comment

                • jkmyoung
                  Recognized Expert Top Contributor
                  • Mar 2006
                  • 2057

                  #9
                  The attribute still has to be moved inside the complexType.
                  Pseudo (sorry, in a bit of a rush)
                  Code:
                  <element>
                    <complexType>
                      other elements
                      <attribute/> put this here. 
                    </complexType>
                  </element>
                  No, if you used any, there's no real point in having a schema. Almost anything would validate.

                  Comment

                  • stephen1313
                    New Member
                    • Sep 2010
                    • 9

                    #10
                    Thanks for your help.
                    I think I am close to having this done.
                    Here is my code now:
                    Code:
                    <?xml version="1.0"?>
                    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
                    <xs:element name="letter">
                    	<xs:complexType>
                    		<xs:attribute name="date" type="xs:string"/>
                    		<xs:element name="to" type="xs:normalizednormalizedString"/>
                    				<xs:complexType>
                    					<xs:attribute name="first" type="xs:normalizedString"/>
                    					<xs:attribute name="last" type="xs:normalizedString"/>
                    					</xs:complexType>
                    	<xs:complexType>
                    		<xs:element name="from" type="xs:normalizedString"/>
                    					<xs:attribute name="first" type="xs:normalizedString"/>
                      					<xs:attribute name="middle" type="xs:normalizedString"/>
                      					</xs:complexType>
                      	<xs:complexType>
                      		<xs:element name="title" type="xs:string"/>
                    		<xs:element name="msg" type="xs:string"/>
                    			<xs:attribute name="paragraph" type="xs:string"/>
                    		 			</xs:complexType>		
                    </xs:complexType>
                    </xs:element>
                    </xs:schema>
                    Here is my error now:
                    Ln 6 Col 63 - s4s-elt-invalid-content.1: The content of '#AnonType_lett er' is invalid. Element 'element' is invalid, misplaced, or occurs too often.

                    If you can help, I would really appreciate it. I have to get this done so I can move on to other content.
                    Last edited by jkmyoung; Sep 21 '10, 06:02 PM.

                    Comment

                    • jkmyoung
                      Recognized Expert Top Contributor
                      • Mar 2006
                      • 2057

                      #11
                      2 Things

                      1. If you're going to put elements inside a complex type, you need a sequence, choice, or all node.

                      2. attributes have to come after the sequence of elements in the complexType.

                      Comment

                      Working...