...

View Full Version : XML Schema?



babylikesburgh
08-19-2008, 05:01 PM
I created the below XML document, and now I have to create a schema for use with the document. This is my absolute first time ever learning about or working with XML, so I'm needing a little guidance. Any input would be GREATLY appreciated so that I can at least figure out where I'm going wrong. I've used several validators but coupled with my inexperience on this topic they are doing me no good. Thanks a bunch!!


<?xml version = "1.0"?>

<!-- Assignment 14-7: nutrition.xml-->
<!--Marking up documents-->


<whitescookie>
<serving>1 package</serving>
<calories>260 calories</calories>
<fatcals>100 fat calories</fatcals>
<fat>11 g of fat</fat>
<saturatedfat>2 g of saturated fat</saturatedfat>
<cholesterol>5 mg of cholesterol</cholesterol>
<sodium>210 mg of sodium</sodium>
<carbohydrates>36 g of carbs</carbohydrates>
<fiber>2 g of fiber</fiber>
<sugar>15 g of sugar</sugar>
<protein>5 g of protein</protein>
</whitescookie>



<?xml version = "1.0"?>

<schema xmlns = "http://www.w3.org/2001/XMLSchema"
xmnls:whitescookie = "whitescookiesnutrition"
targetNamespace = "whitescookiesnutrition">

<element name = "nutrinfo" type = "whitescookie:NutriContent" />

<complexType name = "NutriContent">
<sequence>
<element name = "serving" type = "string" />
<element name = "calories" type = "string" />
<element name = "fatcals" type = "string" />
<element name = "fat" type = "string" />
<element name = "saturatedfat" type = "string" />
<element name = "cholesterol" type = "string" />
<element name = "sodium" type = "string" />
<element name = "carbohydrates" type = "string" />
<element name = "fiber" type = "string" />
<element name = "sugar" type = "string" />
<element name = "protein" type = "string" />
</sequence>
</complexType>
</schema>

oesxyl
08-19-2008, 05:27 PM
<?xml version = "1.0"?>
<!-- Assignment 14-7: nutrition.xml-->
<!--Marking up documents-->

<whitescookie>
<serving>1 package</serving>
<calories>260 calories</calories>
<fatcals>100 fat calories</fatcals>
<fat>11 g of fat</fat>
<saturatedfat>2 g of saturated fat</saturatedfat>
<cholesterol>5 mg of cholesterol</cholesterol>
<sodium>210 mg of sodium</sodium>
<carbohydrates>36 g of carbs</carbohydrates>
<fiber>2 g of fiber</fiber>
<sugar>15 g of sugar</sugar>
<protein>5 g of protein</protein>
</whitescookie>



<?xml version = "1.0"?>

<schema xmlns = "http://www.w3.org/2001/XMLSchema"
xmnls:whitescookie = "whitescookiesnutrition"
targetNamespace = "whitescookiesnutrition">

<element name = "nutrinfo" type = "whitescookie:NutriContent" />

<complexType name = "NutriContent">
<sequence>
<element name = "serving" type = "string" />
<element name = "calories" type = "string" />
<element name = "fatcals" type = "string" />
<element name = "fat" type = "string" />
<element name = "saturatedfat" type = "string" />
<element name = "cholesterol" type = "string" />
<element name = "sodium" type = "string" />
<element name = "carbohydrates" type = "string" />
<element name = "fiber" type = "string" />
<element name = "sugar" type = "string" />
<element name = "protein" type = "string" />
</sequence>
</complexType>
</schema>

you mix value, units and property in same fields and this make fields hard to validate because all become 'string'. I can't suggest how to avoid this because if you separate them, xml could become very complicated and hard to manipulate so you must find yourself a balance between that two problems.
xml schema have some problem and even w3c recommends relaxng.

http://relaxng.org/

here is a good tutorial about compact:

http://relaxng.org/compact-tutorial-20030326.html

once you define the schema is very easy to convert from one type to others. I use trang for that.

http://thaiopensource.com/relaxng/trang.html

also with trang you can extract the schema direct from xml.

I hope this help.

best regards

babylikesburgh
08-19-2008, 06:07 PM
Honestly what I used above is the majority of what we learned. And, this is all we'll be learning about XML and schemas, which is why I'm having such a hard time. It only says to make a schema for the structure of the xml document, but doesn't specify what the structure should be, so I just set everything to a string. I'll try to figure out some other way to format the layout, I'm just not exactly sure how to get to the point where I understand how to use the suggestions on the web sites you directed me to. I've worked with VB.NET, JavaScript and XHTML, but yeah... I'm not getting very far with xml...

oesxyl
08-19-2008, 06:44 PM
Honestly what I used above is the majority of what we learned. And, this is all we'll be learning about XML and schemas, which is why I'm having such a hard time. It only says to make a schema for the structure of the xml document, but doesn't specify what the structure should be, so I just set everything to a string. I'll try to figure out some other way to format the layout,
the schema describe the data model and the layout must follow this model. Usualy you must have the model first and then data, :)
how strict you make the model depend of what you want to do with him.


I'm just not exactly sure how to get to the point where I understand how to use the suggestions on the web sites you directed me to. I've worked with VB.NET, JavaScript and XHTML, but yeah... I'm not getting very far with xml...
I used trang to extract schema from your xml and give me this:



<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="whitescookie">
<xs:complexType>
<xs:sequence>
<xs:element ref="serving"/>
<xs:element ref="calories"/>
<xs:element ref="fatcals"/>
<xs:element ref="fat"/>
<xs:element ref="saturatedfat"/>
<xs:element ref="cholesterol"/>
<xs:element ref="sodium"/>
<xs:element ref="carbohydrates"/>
<xs:element ref="fiber"/>
<xs:element ref="sugar"/>
<xs:element ref="protein"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="serving" type="xs:string"/>
<xs:element name="calories" type="xs:string"/>
<xs:element name="fatcals" type="xs:string"/>
<xs:element name="fat" type="xs:string"/>
<xs:element name="saturatedfat" type="xs:string"/>
<xs:element name="cholesterol" type="xs:string"/>
<xs:element name="sodium" type="xs:string"/>
<xs:element name="carbohydrates" type="xs:string"/>
<xs:element name="fiber" type="xs:string"/>
<xs:element name="sugar" type="xs:string"/>
<xs:element name="protein" type="xs:string"/>
</xs:schema>


this is very closer to what you already post. Doing the same thing but for a compact schema give me this:


default namespace = ""

start =
element whitescookie {
element serving { text },
element calories { text },
element fatcals { text },
element fat { text },
element saturatedfat { text },
element cholesterol { text },
element sodium { text },
element carbohydrates { text },
element fiber { text },
element sugar { text },
element protein { text }
}


shorter and easy to read. Try to read the tutorial and see how you can adjust this to your case. This two schema are equivalent, so having one you have the other one and if you can run java download and install trang.

best regards

babylikesburgh
08-19-2008, 11:51 PM
This coupled with reading some tutorials is definitely helping me catch the hang of it =) Thanks a bunch!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum