Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: XML Schema?

  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question XML Schema?

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

    Code:
    <?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>

    Code:
    <?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>

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by babylikesburgh View Post
    Code:
    <?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>

    Code:
    <?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

  • #3
    New Coder
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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...

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by babylikesburgh View Post
    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:

    Code:
    <?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:
    Code:
    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

  • Users who have thanked oesxyl for this post:

    babylikesburgh (08-19-2008)

  • #5
    New Coder
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    7
    Thanked 0 Times in 0 Posts
    This coupled with reading some tutorials is definitely helping me catch the hang of it =) Thanks a bunch!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •