Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with XML and Schemas

  1. Oct 3, 2012 #1
    I Need to create an xml file that involves having a gallery root element and photos inside that have multiple elements to them. I am not quite sure if im doing anything right because im so new to xml and there are not much tutorials out that are helpful.

    instructions:
    Extend your XML representation of photos in three ways:
    a. The creator of a photo is now more than a name, it's a complex type <creator>
    with three subelements: the name (as before), an optional email and an
    optional homepage.
    b. Photos are getting some more properties: A photo also has
    • a title which is a string,
    • a copyright which is also a string
    c. Photos are collected in a gallery element

    Develop an XML Schema that captures this new structure

    Can someone view my current progress and tell me if I am on track thank you.

    P.S. I am also using Netbeans to create and write these documents.
     
  2. jcsd
  3. Oct 3, 2012 #2

    Mark44

    Staff: Mentor

    Did you intend to attach a file with your schema definition?
    You can use an ordinary text editor to create your schema definition (XSD) file.

    Here's a link to a site that has some pretty good tutorials - http://www.w3schools.com/Schema/default.asp [Broken]. I have found them very helpful resources for learning CSS (cascading style sheets) and HTML.
     
    Last edited by a moderator: May 6, 2017
  4. Oct 3, 2012 #3
    Code (Text):

    <?xml version="1.0" encoding="UTF-8"?>
    <gallery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Schema.xsd">

          <photo>
            <title>first</title>
            <copyright>CC BY-ND</copyright>
            <creator>
                <name>John Smithr</name>
                <email>Joker123@yahoo.com</email>
                <homepage></homepage>
            </creator>
            <mimetype>image/png</mimetype>
            <location>[PLAIN]http://cache.ohinternet.com/images/thumb/2/2d/Trollface_HD.png/618px-Trollface_HD.png</location>[/PLAIN] [Broken]
            <width>618</width>
            <height>564</height>
            <keywords>
                <keyword>blackandwhite</keyword>
                <keyword>trolling</keyword>
                <keyword>face</keyword>
            </keywords>
            </photo>
       
    </gallery>
    That is my XML file and I do not know if my schema is on par with it... any help would be appreciated.


    Code (Text):

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="gallery">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="photo">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element type="xs:string" name="title"/>
                                <xs:element type="xs:string" name="copyright"/>
                                <xs:element name="creator">
                                    <xs:complexType>
                                        <xs:sequence>
                                            <xs:element type="xs:string" name="name"/>
                                            <xs:element type="xs:string" name="email"/>
                                            <xs:element type="xs:string" name="homepage"/>
                                        </xs:sequence>
                                    </xs:complexType>
                                </xs:element>
                                <xs:element type="xs:string" name="mimetype"/>
                                <xs:element type="xs:anyURI" name="location"/>
                                <xs:element type="xs:short" name="width"/>
                                <xs:element type="xs:short" name="height"/>
                                <xs:element name="keywords">
                                    <xs:complexType>
                                        <xs:sequence>
                                            <xs:element type="xs:string" name="keyword" maxOccurs="unbounded" minOccurs="0"/>
                                        </xs:sequence>
                                    </xs:complexType>
                                </xs:element>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>            
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
     
    Last edited by a moderator: May 6, 2017
  5. Oct 3, 2012 #4

    gabbagabbahey

    User Avatar
    Homework Helper
    Gold Member

    Please don't multi-post the same problem both here and in the homework subforums.
     
  6. Oct 4, 2012 #5

    Mark44

    Staff: Mentor

    This looks like a good start, but you haven't taken into account that some of the elements are optional. Here is some code copied from the site whose link I gave earlier.
    Code (Text):
    <xs:element name="person">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="full_name" type="xs:string"/>
          <xs:element name="child_name" type="xs:string" maxOccurs="10"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
     
    To make something optional, set minOccurs to 0.
     
  7. Oct 4, 2012 #6
    Ahh, thank you. Now that my XML file validates with my XSD file, how can I add onto the original XML file, for example I want to add 2 more photo elements. I tried putting in the same code 2 more times for my XML but then it does not validate correctly with my XSD. What is the catch here?
     
  8. Oct 4, 2012 #7

    Mark44

    Staff: Mentor

    In case you saw the post I just deleted, disregard it. I wasn't understanding the role of sequence in a complexType element.
     
  9. Oct 4, 2012 #8

    DavidSnider

    User Avatar
    Gold Member

    Mark44: The <sequence> indicator specifies that the child elements must appear in a specific order.
     
  10. Oct 4, 2012 #9

    Mark44

    Staff: Mentor

    See if you can find something similar at the w3wschools link I posted earlier.
     
  11. Oct 8, 2012 #10
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Help with XML and Schemas
  1. XQuery XML help (Replies: 1)

  2. Understanding XML (Replies: 3)

  3. MySQL: schema, model (Replies: 1)

Loading...