Jump to content

Help with xsd file


stephen1313

Recommended Posts

Learning XML and I need help.I have an xml file and I want to create a 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:<?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:<?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.

Link to comment
Share on other sites

It seems like what you really want is to define "letter" as the root element, and keep going on with its inner contents... but look at what you have instead:

<xs:element name="letter"></xs:element>

You're defining an element called "letter" with no datatype of any kind, and nothing related to it. And then you're defining the rest of the thing as if it is part of that element when it's not.Move the </xs:element> down to right before </xs:schema>, and see if that helps.

Link to comment
Share on other sites

It seems like what you really want is to define "letter" as the root element, and keep going on with its inner contents... but look at what you have instead:
<xs:element name="letter"></xs:element>

You're defining an element called "letter" with no datatype of any kind, and nothing related to it. And then you're defining the rest of the thing as if it is part of that element when it's not.Move the </xs:element> down to right before </xs:schema>, and see if that helps.

Thanks for the help. When I did that, it really helped.I now have the following errors:Ln 5 Col 64 - s4s-elt-invalid-content.1: The content of '#AnonType_letter' is invalid. Element 'element' is invalid, misplaced, or occurs too often.Ln 10 Col 15 - s4s-elt-must-match.1: The content of 'letter' must match (annotation?, (simpleType | complexType)?, (unique | key | keyref)*)). A problem was found starting at: attribute.
Link to comment
Share on other sites

You need a separate attribute element for each attribute indeed... but you mustn't wrap them in another <xs:attribute>. Notice:

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

Should be just:

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

Also, the first child of <xs:complexType> must be an element that specified the order of the elements to follow within. If you don't care about the order, use <xs:all>, like:

<xs:complexType><xs:all><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:all></xs:complexType>

(if you want them to appear only in the order specified, use <xs:sequence> instead, and if you want only one of them to be present, use <xs:choice>.)

Link to comment
Share on other sites

You need a separate attribute element for each attribute indeed... but you mustn't wrap them in another <xs:attribute>. Notice:
<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>

Should be just:

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

Also, the first child of <xs:complexType> must be an element that specified the order of the elements to follow within. If you don't care about the order, use <xs:all>, like:

<xs:complexType><xs:all><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:all></xs:complexType>

(if you want them to appear only in the order specified, use <xs:sequence> instead, and if you want only one of them to be present, use <xs:choice>.)

Ok, here are the errors I get now:Ln 12 Col 44 - s4s-elt-must-match.1: The content of 'letter' must match (annotation?, (simpleType | complexType)?, (unique | key | keyref)*)). A problem was found starting at: attribute.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:/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:/lettersl.xsd'.Ln 4 Col 54 - cvc-complex-type.3.2.2: Attribute 'date' is not allowed to appear in element 'letter'.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 14 Col 10 - cvc-complex-type.2.4.a: Invalid content was found starting with element 'title'. One of '{heading}' is expected.Here is my updated xsd file:<?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="heading" type="xs:normalizedString"/> <xs:element name="body" type="xs:normalizedString"/> </xs:sequence> </xs:complexType> <xs:attribute name="date" type="xs:date"/> <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:element></xs:schema>
Link to comment
Share on other sites

I think <xs:attribute> should be part of <xs:complexType>... just a huntch based on the error message:

The content of 'letter' must match (annotation?, (simpleType | complexType)?, (unique | key | keyref)*)). A problem was found starting at: attribute.
This grammar description - the math - means that <xs:complexType> can only contain zero or one <xs:annotation> element, zero or one of either <xs:simpleType> or <xs:complexType>, and a zero or more of either <xs:unique> or <xs:key> or <xs:keyref>... it doesn't say anything about <xs:attribute> being allowed.
Link to comment
Share on other sites

I think <xs:attribute> should be part of <xs:complexType>... just a huntch based on the error message:This grammar description - the math - means that <xs:complexType> can only contain zero or one <xs:annotation> element, zero or one of either <xs:simpleType> or <xs:complexType>, and a zero or more of either <xs:unique> or <xs:key> or <xs:keyref>... it doesn't say anything about <xs:attribute> being allowed.
Thanks for helping with this.I feel like I am getting there. It is not quite like the DTD stuff I did where you could nest the attributes for the elements.Here is what I have now and my errors:XSD file:<?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>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].
Link to comment
Share on other sites

Thanks for helping with this.I feel like I am getting there. It is not quite like the DTD stuff I did where you could nest the attributes for the elements.Here is what I have now and my errors:XSD file:<?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>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].
Here is an update for those of you helping me out:I have updated my code to the following:<?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.
Link to comment
Share on other sites

Same thing as before - <xs:attribute> must be a child of <xs:complexType>, not <xs:element>. Look here:

<xs:element name="letter"> <xs:attribute name="date" type="xs:normalizedString"/>

Link to comment
Share on other sites

Same thing as before - <xs:attribute> must be a child of <xs:complexType>, not <xs:element>. Look here:
<xs:element name="letter"> <xs:attribute name="date" type="xs:normalizedString"/>

Thanks for your help.I think I am close to having this done.Here is my code now:<?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_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.
Link to comment
Share on other sites

Search this page for "Also, the first child of <xs:complexType>", and you'll find your answer... I've already told you.You should really get yourself a good XML Schema editor... Ideally one with visual editing capabilities... I'd reccomend StylusStudio.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...