Notations

The word notation means a note or an annotation. It's a means of communicating additional information. That's really what a notation is in XML, as well—it is a means for an XML parser, reading the XML file, to pass additional information along to the program that will be using the XML file.

Notations come in handy when you want to include binary data with your XML documents. For example, if you wanted to include a GIF in your XML document, you couldn't just cut and paste the GIF into a text file. Instead, you would need to point to the GIF somehow. You can actually include a reference to the external graphic, either as an attribute or an entity.

Note

XML Schemas actually provide two datatypes specifically for this purpose. If you are going to be working with large amounts of binary data often, XML Schemas would be a better choice for validating your XML documents than DTDs.


However, even if you do include a reference to the binary data, how does the application know what to do with the information? Well, you can point it to helpful information using a Notation.

Attributes and Entities

The idea of a notation is quite simple; sometimes it is necessary to explain the meaning of data in an attribute or entity. That can be done through a notation.

For example, let's say that you wanted to include a GIF image in an XML file, such as a company logo. You could include the URL to the file in an attribute:

<image source="http://www.mysite.com/myimage.gif"> 

That is perfectly legal, and, in fact, a common way to reference files, through an external reference. However, to the XML processor reading the file, the filename itself is no more than a string of text. It does not know that the string of text actually refers to an image, and specifically to a GIF image.

In fact, the XML Parser is not even required to know what the data means; after all, it is just processing text. However, it might be nice if the parser could pass along specific information about referenced files to outside applications that could in turn deal with the data. That is what Notations enable us to do.

So, with a Notation declaration, we could say

<!NOTATION gif  SYSTEM "gif"> 

This defines a notation for "gif", and that in turn can be used to define a Notation Attribute Type, which would represent a GIF image. First, we define the notation:

<!NOTATION gif SYSTEM "gif"> 

Second, we define the attribute type:

<!ATTLIST image 
                        source CDATA #REQUIREDtype NOTATION (gif) #REQUIRED>

Now we are free to use the notation in our XML attribute:

<image type="gif" source="http://www.mysite.com/myimage.gif" > 

Of course, using a notation really isn't something you would do in well-formed XML. It does require that you define the notation and attributes in a DTD.

The notation is declared in the DTD by using the notation declaration, which takes the form

<!NOTATION name SYSTEM "notation information"> 

The name of the notation is what you use in the ATTLIST declaration, or in the entity declaration. The notation information is the information that will be passed along to the application dealing with the binary data format. It could be a simple keyword, such as the “gif” in our example. However, it could be a URL or some other type of description.

Because the XML parser isn't actually dealing with the binary data, XML doesn't really care what type of data it is. As long as it has a notation to pass along, it's perfectly happy. So, if you are using a notation in your document, it's important to make sure that the application that is going to use your XML document can adequately handle the notation.

Notations can also be used with entities. Making use of an unparsed entity is another way in which you might include a piece of binary data in your XML file. Let's revisit the example of including a company logo in your document. One easy way to do this would be to include the notation and entity declarations in an internal DTD, assuming you didn't have any other need for validation.

Let's say that we have two versions of the company logo, one as a GIF and another as a JPEG. First, we would start with the notation declarations:

<!NOTATION gif SYSTEM "gif"> 
<!NOTATION jpeg SYSTEM "jpeg">

Now that the notations of "GIF" and "JPEG" have been declared, it is possible to declare entities for the logos themselves:

<!ENTITY logo-gif SYSTEM "images/company-logo.gif" NDATA gif> 
<!ENTITY logo-jpeg SYSTEM "images/company-logo.jpg" NDATA jpeg>

Now you have two entities that can be used for the logos. If we put these declarations into an XML document, in the internal DTD:

<?xml version="1.0" ?> 
<!DOCTYPE document [
   <!NOTATION gif SYSTEM "gif">
   <!NOTATION jpeg SYSTEM "jpeg">
        <!ENTITY logo-gif SYSTEM "images/company-logo.gif" NDATA gif>
        <!ENTITY logo-jpeg SYSTEM "images/company-logo.jpg" NDATA jpeg>
]>

you are now ready to use the entities in that document:

<?xml version="1.0" ?> 
<!DOCTYPE document [
    <!NOTATION gif SYSTEM "gif">
    <!NOTATION jpeg SYSTEM "jpeg">
<!ENTITY logo-gif SYSTEM "images/company-logo.gif" NDATA gif>
<!ENTITY logo-jpeg SYSTEM "images/company-logo.jpg" NDATA jpeg>
<!ELEMENT document ANY>
]>

<document>
Welcome to our company!
&logo-gif;
</document>

The notation information would be passed by the parser to the application, and assuming that the application was properly set up to deal with the notation, your company logo would be inserted.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.94.153