• Implied Attribute Values
  • Fixed Attribute Values
  • Required Attribute Values




    Download 2,96 Mb.
    Pdf ko'rish
    bet83/131
    Sana14.05.2024
    Hajmi2,96 Mb.
    #232039
    1   ...   79   80   81   82   83   84   85   86   ...   131
    Bog'liq
    Ceponkus, Hoodbhoy - Applied XML - Toolkit for Programmers

    Required Attribute Values
    Required attributes tell the parser that the XML document has to have a value specified 
    for the attribute. If it doesn’t, then it detects an error. This is a useful way for ensuring that 
    you get a value from your document.
    Here’s how to declare the presence of a value for a string type attribute called 
    license_number
    belonging to an element called 
    car
    :
    #REQUIRED
    >
    Note that we no longer have any italics. This is a complete attribute list declaration.
    Here’s what a car’s license number can look like:

    If your car element looks like this:

    the parser issues an error. However, you should be aware that if your element looked like 
    this:

    the parser does not issue an error. Why? Because CDATA attribute types can contain 
    zero or more characters.
    Implied Attribute Values


    - 87 -
    An implied default is one that doesn’t have to be explicitly defined: The parser is told to 
    pass on a blank value to the XML application and let it decide what to do next. In doing 
    so, you give yourself as an application developer more flexibility (and responsibility).
    Here’s how you assign an implied default to an attribute.
    #IMPLIED
    >
    Essentially, we’ve told the parser that a person element has a gender attribute of type 
    CDATA and that no value is supplied; therefore, let the XML application decide what to 
    do next (for example, issue an error or toss in a default value). Thus, although the parser 
    doesn’t mark it as an error, the XML application can if the developer wants it to.
    Fixed Attribute Values
    Attributes with fixed defaults completely define an attribute and do not allow any changes 
    to the attributes values in the XML document. If the attribute value in the XML document 
    is left blank, the parser passes the specified default value to the parser. If the attribute 
    has a value other than that specified, the parser reports an error. Finally, if the attribute 
    value in the document matches the specified value, no error is detected, and the 
    specified value is passed on to the application.
    Here’s how to fix an attribute:
    #FIXED
    "Happy">
    This tells the parser that by default every person element’s mood attribute 
    must
    be Happy 
    (wouldn’t that be great!) Thus, there are two possible ways of representing the person 
    element:
    or
    In both cases, the XML parser tells the XML application the same thing—that the person 
    element’s mood attribute value is happy.

    Download 2,96 Mb.
    1   ...   79   80   81   82   83   84   85   86   ...   131




    Download 2,96 Mb.
    Pdf ko'rish