Theory
stranger ના XML ને Read કરો
fest નો sound vendor તેમની equipment list ને XML તરીકે send કરે છે. actual data ની ઉપર 3 mysterious lines sit કરે છે: એક <?xml ...?>, comment જે તેમના software નું name કરે છે, અને odd <?xml-stylesheet ...?> જે તમે ક્યારેય meet નથી કર્યું.
preamble ક્યાં end થાય છે અને data ક્યાં begin થાય છે? કઈ lines ને તમે delete કરી શકો છો?
XML ની anatomy નો official answer છે. દરેક document, તેમનું અને તમારું, exactly 2 sections માં split થાય છે, અને boundary ને know કરવું એ standard 5-mark question છે.
Theory
cover page અને thesis
project report ની cover page હોય છે: title, author, binder માટે formatting notes. Useful, ક્યારેક skippable, પણ ક્યારેય content નહીં: કોઈ examiner cover ને grade નથી કરતો.
પછી thesis itself: દરેક chapter, દરેક mark-earning word, એક binding ની અંદર.
XML document એ જ રીતે bound છે: prolog (cover: declarations અને notes) અને document element section (thesis: root અને બધું data).
Theory
Section 1: prolog
prolog એ root element ની પહેલાં બધું છે. તે contain કરી શકે છે:
- XML declaration:
<?xml version="1.0" encoding="UTF-8"?>, જો present હોય તો first - comments:
<!-- maintained by the fest committee --> - processing instructions (PIs):
<?xml-stylesheet type="text/css" href="fest.css"?>, specific software ને messages - optionally DOCTYPE line જે DTD ને reference કરે છે (validation grammar, આ syllabus થી beyond)
આખો prolog optional છે, અને તે કોઈ data carry નથી કરતો: તેને delete કરો અને information survive કરે છે.
Theory
Section 2: document element section
document element section એ root element અને તેની અંદરનું બધું છે: દરેક element, attribute અને text node. બધું data અહીં lives કરે છે, જેથી root ને document element કહેવાય છે: તે structurally document IS છે.
comments અંદર પણ appear થઈ શકે છે (tricky entry ને annotate કરવા), 2 rules સાથે જ્યાં પણ તેઓ જાય છે: comment ના text માં કોઈ -- નહીં, અને comments ક્યારેય nest નથી થતા.
minimal legal document? ફક્ત <events></events>: કોઈ prolog નહીં, એક empty root. Well-formed.
Practical
events.xml, dissected
<!-- ============ PROLOG SECTION ============ -->
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="fest.css"?>
<!-- events.xml: maintained by the fest committee -->
<!-- ====== DOCUMENT ELEMENT SECTION ====== -->
<events>
<!-- comments may sit inside the data too -->
<event id="1">
<name>Garba Night</name>
<venue>Main Ground</venue>
</event>
</events>
Quiz
આમાંથી કયું XML document ના PROLOG section સાથે belongs કરે છે?
- root element અને તેના attributes
- XML declaration અને કોઈ પણ comments જે root ની પહેલાં લખાયેલા હોય
- child elements નું text content
- file માં દરેક comment, જ્યાં પણ તે appear થાય
Show the answer
XML declaration અને કોઈ પણ comments જે root ની પહેલાં લખાયેલા હોય
prolog ને position દ્વારા define કરવામાં આવે છે: જે કંઈ legally root ની પહેલાં stands કરે છે: declaration, pre-root comments, processing instructions, optional DOCTYPE. root અને તેના attributes (option A) એ document element section ની opening છે, અને child text (option C) તેનું cargo છે. Option D એ subtle trap છે: comments બંને sections માં allowed છે, તેથી root ની અંદરનું comment document element section સાથે belongs કરે છે: boundary એ root નું start tag છે, line નો kind નહીં.
Think first
prolog ને Delete કરો: શું breaks થાય છે?
dissected listing ને લો અને તેના entire prolog ને delete કરો: declaration, PI, comment. tap કરતા પહેલાં: file હજુ well-formed છે, અને શું, concretely, lost થાય છે?
Show the answer
હજુ well-formed: prolog optional છે, અને bare root section એ complete document છે. શું lost થાય છે તે advice છે, data નહીં: કોઈ declared encoding નહીં (parsers UTF-8 ને assume કરે છે, risky day જ્યારે Gujarati event name arrive થાય), browsers માટે કોઈ stylesheet hint નહીં, next student માટે કોઈ maintainer note નહીં. તેથી exam nuance: prolog PARSER માટે dispensable છે પણ PEOPLE અને tools માટે valuable છે. data ક્યારેય ત્યાં lives નથી કરતું, જે exactly છે શા માટે તેને delete કરવું content ને break નથી કરી શકતું.
Watch out
Comment law અને PI clarification
Comments: <!-- like this -->, ક્યારેય અંદર bare -- contain નથી કરતા, ક્યારેય nested નહીં. એક comment બીજા ને swallow કરે છે તે parse error છે, style issue નહીં.
PIs એ declarations નથી: બંને <? ?> પહેરે છે, પણ XML declaration એ fixed first-line announcement છે જ્યારે PI જેમ કે xml-stylesheet એ instruction છે જે particular software ને addressed છે, prolog માં ક્યાં પણ placeable છે. દરેક <? ?> line ને "declaration" કહેવો એ distinction mark ને cost કરે છે.
Theory
Anatomy એ error messages ને readable બનાવે છે
Parser errors positions ને quote કરે છે જેમ કે "line 2, before root element": તમે હવે જાણો છો કે તેનો અર્થ PROLOG territory છે, તેથી declaration અથવા stray character ને suspect કરો, તમારા data ને નહીં. Anatomy એ rejection slips ને directions માં turn કરે છે. એક prolog citizen ને તેની own lesson ની જરૂર છે: declaration અને તેના picky placement rules આ XML unit ને next close કરે છે.
Summary
Key takeaways
- XML document = prolog section + document element section, root ના start tag પર split.
- Prolog (બધું optional, કોઈ data નહીં): XML declaration first, પછી comments, processing instructions, optional DOCTYPE.
- Document element section: root અને અંદરનું બધું: બધા elements, attributes, text.
- Comments <!-- --> બંને sections માં live થાય છે; અંદર કોઈ -- નહીં, કોઈ nesting નહીં.
- કોઈ prolog વગર bare root element હજુ well-formed છે.
- PIs (જેમ કે xml-stylesheet) એ software-directed instructions છે, declaration નહીં.
- Memory hook: cover page optional, thesis compulsory.