www.webdeveloper.com
Results 1 to 3 of 3

Thread: How to put restrictions on XML attribute in schema

Hybrid View

  1. #1
    Join Date
    Oct 2011
    Posts
    2

    Thumbs down How to put restrictions on XML attribute in schema

    Well, it's official: I'm thick. I have been programming years, but have never had to define an XML schema before and it is driving me up the wall. Despite looking at tens of examples, I just can't get the syntax right for defining an mandatory attribute on an element with the restriction that the attribute must be a number between 1 and 100.

    I can't even get the following simpler example to work, which simply states the attribute must be a positive integer. The schema doesn't report any errors with this but when I try to use the attribute in an XML document, I get an error saying that the attribute is not defined.

    This is my schema:
    <xs:complexType name="ArtworkType">
    <xs:sequence>
    <xs:element name="title" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="ID" type="MyType2" use="required"/>
    </xs:complexType>

    And this is my XML document:
    <artworks
    xmlns="http://www.fourthwish.co.uk/data/Artworks.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.fourthwish.co.uk/data/Artworks.xsd Artworks.xsd"
    >

    <artwork ID="0">
    <title>Title String</title>
    </artwork>

    </artworks>
    <xs:attribute name="MyType2" type="xsositiveInteger"/>

    Please help before I go mad.

    Kind wishes, Patrick

  2. #2
    Join Date
    Oct 2011
    Posts
    2

    Update to thread post

    Very sorry, I have made an error pasting the code into the above example. It should read:

    This is my schema:
    <xs:complexType name="ArtworkType">
    <xs:sequence>
    <xs:element name="title" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="ID" type="MyType2" use="required"/>
    </xs:complexType>
    <xs:attribute name="MyType2" type="positiveInteger"/>

    And this is my XML document:
    <artworks
    xmlns="http://www.fourthwish.co.uk/data/Artworks.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.fourthwish.co.uk/data/Artworks.xsd Artworks.xsd"
    >

    <artwork ID="0">
    <title>Title String</title>
    </artwork>

    </artworks>

  3. #3
    Join Date
    Jul 2011
    Posts
    3
    My advice would be to invest in an O Reilly xml book like XML Schema: The W3C's Object-Oriented Descriptions for XML and maybe even an xml editor to help with your syntax and validation type stuff. I think XML Marker is free.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles