Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 8 of 8
  1. #1
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    XML & XSD Primer/Tutorial

    by Steven Moseley

    I decided to put this little XML tutorial / primer together for those of you who are interested in learning XML, but don't want to spend weeks doing so. This 15-minute course should give you guys a good basic understanding of how XML works without much time investment. I suggest that if you find this primer interesting, go read up more on XML (and the associative standards) and learn more. There's a lot more you can do with it than what I'm showing here.

    XML (Extensible Markup Language)

    There's not much to XML, other than some basic rules. Here is a summary:
    1. Valid XML documents must begin with <?xml version="1.0"?> (obviously, with the appropriate version of XML specified).
    2. There can only be one "root" element in an XML document (for instance, the <html> element of an HTML document).
    3. Elements must be named with <> syntax (the same as HTML).
    4. Elements must all be closed. For instance, if you open a <table>, you must close it with a </table>. On the other hand, elements such as <img> cannot be closed, because there is no child node to the element. In cases like that, you can close the element in the element declaration with a trailing slash like so: <img src="xyz.gif" />
    5. Attribute values must be contained within quotes. For instance, you can't say cellpadding=0 - you must say cellpadding="0" instead.
    6. You may not use the greater than (>), ampersand (&), or quote (") characters in your text nodes (the text contained within an element or attribute - for instance, <p>This is a text node!</p>. You must use the alternate representations &gt;, &amp;, and &quot;.


    XSD (XML Schema Definition)

    XSD is a very simple markup language that is used to define an XML standard (schema). It looks a lot like HTML, but with different elements. Here are some of the basic elements of a schema:

    schema
    This is the root element of the schema (like the "html" element in an HTML document). The schema must contain an xmlns Namespace URI pointing to the schema that defines how XSD must be structured. It should also contain a target namespace, which is the uri for the standard that we're creating in this schema.
    Code:
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://www.mydomain.com/mySchema">
    </xsd:schema>
    The prefix on the elements (xsd) is a reference to the xmlns defined in the schema.

    element
    An "element" is used to define an tag that will be allowed in the XML standard you are creating. For instance, if I want to create a standard for CDs, I could create an element called "cd" like so:
    Code:
    <xsd:element name="cd" type="xsd:string" />
    This definition will allow me to create an element like this:
    Code:
    <cd>Eminem</cd>
    attribute
    The "attribute" element is used to define an attribute. Attributes must be defined within the context of a complexType (see below). For instance, if I wanted to give my cd element above attributes like "title" and "artist", I could create the following:
    Code:
    <xsd:element name="cd">
      <xsd:complexType>
        <attribute name="title" type="string" />
        <attribute name="artist" type="string" />
      </xsd:complexType>
    </xsd:element>
    Note that the "type" attribute was removed from the element tag and a new "complexType" element was added within the context of the element, containing the desired attributes. Here's what the output might look like:
    Code:
    <cd name="The Eminem Show" artist="Eminem">
    simpleType
    The "simpleType" element is used to define simple types that extend or restrict the basic XSD types. Basic XSD types are string, integer, decimal, datetime, etc. Here are some simpleType restrictions:

    minLength / maxLength - sets the minimum or maximum length of a restricted simpleType. Here's an extension of the example above to make the base string restricted to only allow strings of 9 characters.
    Code:
    <xsd:simpleType name="ssnType">
      <xsd:restriction base="xsd:string">
        <xsd:minLength value="9" />
        <xsd:maxLength value="9" />
      </xsd:restriction>
    </xsd:simpleType>
    In the case of integers, you can also use the minInclusive / maxInclusive elements, which limit the numeric value contained in the integer.

    pattern
    Patterns allow definition of a regular expression pattern to limit the allowed content of a restricted type. For instance, the following pattern will match any string consisting only of letters and numbers.
    Code:
    <xsd:simpleType name="serialNumberType">
      <xsd:restriction base="xsd:string">
        <xsd:pattern value="[a-zA-Z0-9]*" />
      </xsd:restriction>
    </xsd:simpleType>
    enumeration
    Enumerations allow you to define a list of possible values allowed for a simpleType. Here's an example:
    Code:
    <xsd:simpleType name="genreType">
      <xsd:restriction base="xsd:string">
        <xsd:enumeration value="classical" />
        <xsd:enumeration value="country" />
        <xsd:enumeration value="gospel" />
        <xsd:enumeration value="rap" />
        <xsd:enumeration value="rock" />
      </xsd:restriction>
    </xsd:simpleType>
    complexType
    The "complexType" element is used to define complex types, which are composed of one or more attributes and elements put together in sequences or choice lists. An example of a complexType element would be an HTML <table> type. It contains attributes (cellpadding, etc.) and child elements (<tr>, etc.). Here's an example of a few different ways of composing complexTypes:

    sequence
    A "sequence" specifies a list of elements that may occur in a complexType. They must all occur (unless specified with a minOccurs value of 0) in the order arranged in the sequence.
    Code:
    <element name="person" type="personType" />
    
    <xsd:complexType name="personType">
      <xsd:sequence>
        <xsd:element name="firstName" type="xsd:string" />
        <xsd:element name="middleInitial" minOccurs="0">
          <xsd:simpleType>
            <xsd:restriction base="xsd:string">
              <xsd:pattern value="[a-zA-Z]">
            </xsd:restriction>
          </xsd:simpleType>
        </xsd:element>
        <xsd:element name="lastName" type="xsd:string" />
      </xsd:sequence>
    </xsd:complexType>
    The resulting schema would allow something like this:
    Code:
    <person>
      <firstName>Steven</firstName>
      <middleInitial>G</middleInitial>
      <lastName>Moseley</lastName>
    </person>
    Notice in the XSD example above that I'm inheriting the named complexType in the "type" attribute of the definition for the "person" element. This is a good habit to get into for defining data types that will be used by many elements.

    all
    An "all" element is similar to a "sequence", except that it doesn't require that the child elements be arranged in any specific manner. For instance, in the example above, <lastName> could be placed before <firstName> if an <xsd:all> were used in place of the <xsd:sequence>

    any
    An "any" element is similar to an "all" element, except that it doesn't require that all child elements be used. For instance, in the example above, the <person> could have only a <firstName> specified if <xsd:any> were used.

    choice
    A "choice" element is similar to an any element, except that it requires that only one child element be used. For instance, in the example above, the resulting <person> element could only have one of <firstName>, <middleInitial>, or <lastName> specified.

    attributes
    If you're going to define attributes in a complexType, you must place them after the sequence, all, any, or choice element.

    Example
    Here's a complete example of XSD and XML in action. I decided to go with the CD example that I started above.

    Code:
    <?xml version="1.0"?>
    <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.mydomain.com/cdSchema">
      <xsd:element name="cd">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="artist" type="personType" />
            <xsd:element name="track" type="trackType" maxOccurs="100" />
          </xsd:sequence>
          <attribute name="title" type="xsd:string" />
        </xsd:complexType>
      </xsd:element>
      <xsd:complexType name="personType">
        <xsd:sequence>
          <xsd:element name="firstName" type="xsd:string" />
          <xsd:element name="middleInitial" minOccurs="0">
            <xsd:simpleType>
              <xsd:restriction base="xsd:string">
                <xsd:pattern value="[a-zA-Z]">
              </xsd:restriction>
            </xsd:simpleType>
          </xsd:element>
          <xsd:element name="lastName" type="xsd:string" />
        </xsd:sequence>
      </xsd:complexType>
      <xsd:complexType name="trackType">
        <xsd:attribute name="number" type="xsd:integer" />
        <xsd:attribute name="name" type="xsd:string" />
      </xsd:complexType>
    </xsd:schema>
    And here's an example of a resulting XML document:
    Code:
    <?xml version="1.0"?>
    <cd name="The Eminem Show" xmlns="http://www.mydomain.com/cdSchema">
      <artist>
        <firstName>Marshal</firstName>
        <lastName>Mathers</lastName>
      </artist>
      <track number="1" name="Hi, My Name Is" />
      <track number="2" name="White America" />
      <track number="3" name="Slim Shady" />
    </cd>

  2.  

  3. #2
    WDF Staff Wired's Avatar
    Join Date
    Apr 2003
    Posts
    7,656
    Member #
    1234
    Liked
    137 times
    Is there something wrong with the blue stuff? < and > didn't come out exactly right.
    The Rules
    Was another WDF member's post helpful? Click the like button below the post.

    Admin at houseofhelp.com

  4. #3
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    I fixed it.

  5. #4
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    I guess no one's interested in XML.... too bad, I was gonna follow this up with an XSLT tutorial.

  6. #5
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Originally posted by transio
    I guess no one's interested in XML.... too bad, I was gonna follow this up with an XSLT tutorial.
    Go ahead!

    I just got home from 5 days at work, and discovered your tutorial right now!
    I'm trying to read up on XML these days (dragged two books along for the mentioned five day period).

    Right now, I'm struggling through the SAX and DOM API. Still somewhat confusing, but I'm determined to get some rudimentary grip on it.

    The stylesheet part of it would be very interesting!
    :classic:
    S. Rosland

  7. #6
    Senior Member glyakk's Avatar
    Join Date
    Nov 2003
    Location
    USA
    Posts
    1,263
    Member #
    3828
    Liked
    6 times
    I dont often surf the tutorials section. I would defenatly be interested in a followup on XSLT.


  8. #7
    Senior Member skrlin's Avatar
    Join Date
    Apr 2003
    Location
    Illinois
    Posts
    562
    Member #
    1063
    Go ahead and do the XSLT tut, I'll take a gander.

    You've got a small following here now, it'll grow over time after the tutorial is actually here.

    Go for it!
    - Brian

  9. #8
    Junior Member
    Join Date
    Dec 2004
    Posts
    9
    Member #
    8387
    Your tutorial was great. I've read about XML as sort of an extension to HTML, but now I understand a lot better how it works. Could you describe what type of applications you could use this for?


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 02:04 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com