469,366 Members | 2,236 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

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:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <letter date="2005/1/1"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:noNamespaceSchemaLocation="http://some-where letter.xsd">
  5.  
  6. <to>
  7. <first>smith</first>
  8. <last>Lee</last>
  9. </to>
  10. <from>
  11. <first>Sylvie</first>
  12. <middle>S.</middle>
  13. </from>
  14. <title>Example</title>
  15. <msg>
  16. <paragraph>
  17. I have a question.
  18. <paragraph>
  19. Can you infer an XML Schema?
  20. </paragraph>
  21. </paragraph>
  22. </msg>
  23.  
  24. </letter>
  25.  
My XSD file:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  3. <xs:element name="letter">
  4. </xs:element>
  5. <xs:complexType>
  6. <xs:element name="to" type="xs:normalizednormalizedString"/>
  7. <xs:element name="from" type="xs:normalizedString"/>
  8. <xs:element name="heading" type="xs:normalizedString"/>
  9. <xs:element name="body" type="xs:normalizedString"/>
  10. </xs:complexType>
  11. <xs:attribute>
  12. <xs:attribute name="last" type="xs:normalizedString"/>
  13. <xs:attribute name="first" type="xs:normalizedString"/>
  14. <xs:attribute name="middle" type="xs:normalizedString"/>
  15. <xs:attribute name="title" type="xs:normalizedString"/>
  16. <xs:attribute name="msg" type="xs:normalizedString"/>
  17. <xs:attribute name="paragraph" type="xs:normalizedString"/>
  18. </xs:attribute>
  19. </xs:schema>
  20.  
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.
Sep 15 '10 #1
10 7803
jkmyoung
2,057 Expert 2GB
Your formation of schema doesn't fit standards. See this example for where to place your elements and attributes within a complexType:
Expand|Select|Wrap|Line Numbers
  1. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  2.     <xs:element name="beer">
  3.         <xs:complexType>
  4.             <xs:sequence>
  5.                 <xs:element name="a"/>
  6.                 <xs:element name="b"/>
  7.             </xs:sequence>
  8.             <xs:attribute name="attb"/>
  9.             <xs:attribute name="atta"/>
  10.         </xs:complexType>
  11.     </xs:element>
  12. </xs:schema>
  13.  
Sep 17 '10 #2
Thanks for your reply.
Here is what I have now.
My XSD file:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  3. <xs:element name="letter">
  4.     <xs:complexType>
  5.     <xs:sequence>
  6.     <xs:element name="to" type="xs:normalizednormalizedString"/>
  7.       <xs:element name="from" type="xs:normalizedString"/>
  8.     <xs:element name="title" type="xs:normalizedString"/>
  9.     <xs:element name="msg" type="xs:normalizedString"/>
  10.     </xs:sequence>
  11.     <xs:attribute name="date" type="xs:normalizedString"/>
  12.       <xs:attribute name="last" type="xs:normalizedString"/>
  13.     <xs:attribute name="first" type="xs:normalizedString"/>
  14.     <xs:attribute name="middle" type="xs:normalizedString"/>
  15.     <xs:attribute name="paragraph" type="xs:normalizedString"/>
  16.     </xs:complexType>        
  17. </xs:element>
  18. </xs:schema>
My errors:
Ln 6 Col 62 - src-resolve.4.2: Error resolving component 'xs:normalizednormalizedString'. It was detected that 'xs:normalizednormalizedString' 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:normalizednormalizedString' 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].
Sep 17 '10 #3
jkmyoung
2,057 Expert 2GB
You probably mean 'xs:normalizedString' (one normalized)

Your from and msg nodes are actually a complex element containing other elements. eg 'from' from your code:
Expand|Select|Wrap|Line Numbers
  1. <from> 
  2.   <first>Sylvie</first> 
  3.   <middle>S.</middle> 
  4. </from> 
  5.  
Put in another element definition inside much like your first one.
Example of nesting element:
Expand|Select|Wrap|Line Numbers
  1. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
  2.     <xs:element name="beer"> 
  3.         <xs:complexType> 
  4.             <xs:sequence> 
  5.                 <xs:element name="a">
  6.                 <xs:complexType>
  7.                   <xs:sequence>
  8.                     <xs:element name="a_child1"/>                      
  9.                                   <xs:element name="a_child2"/>                      
  10.                   </xs:sequence>
  11.                 </xs:complexType>
  12.                 </xs:element> 
  13.                 <xs:element name="b"/> 
  14.             </xs:sequence> 
  15.             <xs:attribute name="attb"/> 
  16.             <xs:attribute name="atta"/> 
  17.         </xs:complexType> 
  18.     </xs:element> 
  19. </xs:schema> 
Example of using reference instead:
Expand|Select|Wrap|Line Numbers
  1. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  2.     <xs:element name="beer">
  3.         <xs:complexType>
  4.             <xs:sequence>
  5.                 <xs:element ref="a"/>
  6.                 <xs:element name="b"/>
  7.             </xs:sequence>
  8.             <xs:attribute name="attb"/>
  9.             <xs:attribute name="atta"/>
  10.         </xs:complexType>
  11.     </xs:element>
  12.     <xs:element name="a">
  13.         <xs:complexType>
  14.             <xs:sequence>
  15.                 <xs:element name="a_child1"/>
  16.                 <xs:element name="a_child2"/>
  17.             </xs:sequence>
  18.         </xs:complexType>
  19.     </xs:element>
  20. </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."/>
Sep 17 '10 #4
Ok, here is my code now:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  3. <xs:element name="letter">
  4. <xs:complexType>
  5.     <xs:sequence>
  6.         <xs:element name="to" type="xs:normalizednormalizedString"/>
  7.                 <xs:element name="first" type="xs:normalizedString"/>
  8.                 <xs:element name="last" type="xs:normalizedString"/>
  9.                 </xs:sequence>
  10.                 </xs:complexType>
  11.     <xs:complexType>
  12.     <xs:sequence>
  13.         <xs:element name="from" type="xs:normalizedString"/>
  14.                 <xs:element name="first" type="xs:normalizedString"/>
  15.                   <xs:element name="middle" type="xs:normalizedString"/>
  16.                   </xs:sequence>
  17.                   </xs:complexType>
  18.       <xs:complexType>
  19.       <xs:sequence>
  20.           <xs:element name="title" type="xs:normalizedString"/>
  21.         <xs:element name="msg" type="xs:normalizedString"/>
  22.         <xs:element name="paragraph" type="xs:normalizedString"/>
  23.                 </xs:sequence>
  24.                   </xs:complexType>        
  25. </xs:element>
  26. </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:normalizednormalizedString'. It was detected that 'xs:normalizednormalizedString' 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:normalizednormalizedString' 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.
Sep 17 '10 #5
Here is an update for those of you helping me out:
I have updated my code to the following:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  3.  
  4.  
  5. <xs:element name="letter">
  6. <xs:attribute name="date" type="xs:normalizedString"/>
  7. <xs:complexType>
  8. <xs:sequence>
  9. <xs:element name="to" type="xs:normalizednormalizedString"/>
  10. <xs:element name="first" type="xs:normalizedString"/>
  11. <xs:element name="last" type="xs:normalizedString"/>
  12. </xs:sequence>
  13. </xs:complexType>
  14. <xs:complexType>
  15. <xs:sequence>
  16. <xs:element name="from" type="xs:normalizedString"/>
  17. <xs:element name="first" type="xs:normalizedString"/>
  18. <xs:element name="middle" type="xs:normalizedString"/>
  19. </xs:sequence>
  20. </xs:complexType>
  21. <xs:complexType>
  22. <xs:sequence>
  23. <xs:element name="title" type="xs:normalizedString"/>
  24. <xs:element name="msg" type="xs:normalizedString"/>
  25. <xs:element name="paragraph" type="xs:normalizedString"/>
  26. </xs:sequence>
  27. </xs:complexType>
  28.  
  29. </xs:element>
  30. </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.
Sep 17 '10 #6
jkmyoung
2,057 Expert 2GB
Less errors is not always better...

<xs:attribute name="date" type="xs:normalizedString"/>
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.
Sep 17 '10 #7
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?
Sep 17 '10 #8
jkmyoung
2,057 Expert 2GB
The attribute still has to be moved inside the complexType.
Pseudo (sorry, in a bit of a rush)
Expand|Select|Wrap|Line Numbers
  1. <element>
  2.   <complexType>
  3.     other elements
  4.     <attribute/> put this here. 
  5.   </complexType>
  6. </element>
No, if you used any, there's no real point in having a schema. Almost anything would validate.
Sep 17 '10 #9
Thanks for your help.
I think I am close to having this done.
Here is my code now:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  3. <xs:element name="letter">
  4.     <xs:complexType>
  5.         <xs:attribute name="date" type="xs:string"/>
  6.         <xs:element name="to" type="xs:normalizednormalizedString"/>
  7.                 <xs:complexType>
  8.                     <xs:attribute name="first" type="xs:normalizedString"/>
  9.                     <xs:attribute name="last" type="xs:normalizedString"/>
  10.                     </xs:complexType>
  11.     <xs:complexType>
  12.         <xs:element name="from" type="xs:normalizedString"/>
  13.                     <xs:attribute name="first" type="xs:normalizedString"/>
  14.                       <xs:attribute name="middle" type="xs:normalizedString"/>
  15.                       </xs:complexType>
  16.       <xs:complexType>
  17.           <xs:element name="title" type="xs:string"/>
  18.         <xs:element name="msg" type="xs:string"/>
  19.             <xs:attribute name="paragraph" type="xs:string"/>
  20.                      </xs:complexType>        
  21. </xs:complexType>
  22. </xs:element>
  23. </xs:schema>
  24.  
Here is my error now:
Ln 6 Col 63 - s4s-elt-invalid-content.1: The content of '#AnonType_letter' 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.
Sep 18 '10 #10
jkmyoung
2,057 Expert 2GB
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.
Sep 21 '10 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Michael George Lerner | last post: by
1 post views Thread by Glenn Coyle | last post: by
1 post views Thread by Mr fahad | last post: by
4 posts views Thread by Jim | last post: by
1 post views Thread by =?Utf-8?B?Y2phbXBz?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.