Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom attributes in an xsd-schema

Tags:

xml

xsd

I'm trying to make a simple xml-editor for some basic but specific needs, the thing that I'm not sure how to handle is that I want to be able to have own custom attributes (or something) in the xsd-schema itself.

Something like this is what I had in mind:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:element name="Book">
      <xsd:complexType>
         <xsd:sequence>
            <xsd:element name="Author" type="xsd:string" listable="1" />
            <xsd:element name="Pages" type="xsd:int" />
         </xsd:sequence>
      </xsd:complexType>
   </xsd:element>
</xsd:schema>

Where I want information about whether the element is 'listable' or not in the schema (note that the .xml file have no information or clue as to whether the element is listable or not, the listable attribute is just a way to organize the elements in the editor).

It doesn't need to be it's own attribute. If there's an misc attribute or something that I can play with that would be fine. Problem is just that the schema above doesn't validate (The 'listable' attribute is not supported in this context.)

Is there a way to store this kind of information in the schema?

Seems like it would be possible to create a new namespace but I don't know how that namespace should be declared so that any element may have a special attribute in the xsd (I'd rather avoid messing with the xml file for this). And it seems a bit overkill to create a new namespace just for this?

Or am I going about this the wrong way entirely?

like image 375
Magnus Avatar asked Jun 21 '10 11:06

Magnus


People also ask

What is attribute in XSD?

Attribute represents the attribute of an XML element. XSD defines it as a simple type. Name of the Attribute. For example, defines following rollno attribute which can be used in an XML element.

What is an example of an XSD schema?

In this example, the XSD schema consists of an <Contact> element of complex type with <FName> and <LName> child elements and the ContactID attribute. The sql:relation annotation maps the <Contact> element to the Person.Contact table in the AdventureWorks database.

What is an XML Schema?

The XML schema defines the shape, or structure, of an XML document, along with rules for data content and semantics such as what fields an element can contain, which sub elements it can contain and how many items can be present. It can also describe the type and values that can be placed into each element or attribute.

How do I define an element within an XSD?

An element can be defined within an XSD as follows: <xs:element name="x" type="y" />. Each element definition within the XSD must have a 'name' property, which is the tag name that will appear in the XML document.


1 Answers

This information should sit in its own namespace. The best place to store it would be in an annotation on the attribute. You can attach an annotation to any schema item and they can contain xsd:documentation elements, designed for human readable documentation, and xsd:appinfo, designated for machine-processable information. So your example would look like:

 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:foo="http://www.example.org/bar">
   <xsd:element name="Book">
      <xsd:complexType>
         <xsd:sequence>
            <xsd:element name="Author" type="xsd:string" >
        <xsd:annotation>
            <xsd:appinfo>
                <foo:listable value="true"/>
            </xsd:appinfo>
        </xsd:annotation>
             </xsd:element>
            <xsd:element name="Pages" type="xsd:int" />
         </xsd:sequence>
      </xsd:complexType>
   </xsd:element>
</xsd:schema>
like image 183
Aled G Avatar answered Oct 12 '22 00:10

Aled G