Attributes

When we talked about DTD and Definition we also have attributes in XML. Whad do attributes stand for? Well we may need extra definition(s) for an element of an xml file, studied in previous chapter evaluate the example below:

<?xml version="1.0"?>
<user>
<name>Tugrul</name>
<lastname>ASLAN</lastname>
<country>Turkey</country>
</user>

Let’s think about we want to define a university degree element in our xml like this:

<?xml version="1.0"?>
<user>
<name>Tugrul</name>
<lastname>ASLAN</lastname>
<country>Turkey</country>
<university degree="Associate">University of Gazi</university>
<university degree="BSc">University of Hertfordshire</university>
</user>

To define a such element as above we need to write an attribute in our DTD file newly modified:

<!DOCTYPE user
[
<!ELEMENT user(name,lastname,country,university)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT country (#PCDATA)>
<!ELEMENT university(#PCDATA)>
<!ATTLIST university
degree CDATA #REQUIRED>
]>

as you notice it is very simple to define an attribute within an xml file, one more point is that we also wrote #REQUIRED, as we see that the user has to enter this value because we defined the attribute as required. There are three standards for the attributes:

  1. #REQUIRED: the value as seen required,
  2. #IMPLIED: the value entrance is optional,
  3. #FIXED: the value has been set by default, no additional value necessary

Since I pointed these attribute values, there is a little difference when you use #FIXED value is that you need to enter the default value when you prepare the DTD file it appears:

<!DOCTYPE user
[
<!ELEMENT user(name,lastname,country,university)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT country (#PCDATA)>
<!ELEMENT university(#PCDATA)>
<!ATTLIST university
degree CDATA #FIXED "BSc">
]>

As we specifically defined the value, the value is defined by default when the xml file is being written.

Leave a Reply

Your email address will not be published. Required fields are marked *