XML syntax (Declaration, Tags, elements)

XML grammar એક page માં: optional first-line declaration, દરેક start tag ને તેના matching end tag દ્વારા closed કરવું, attributes હંમેશા quoted, empty elements self-closed, અને nesting જે ક્યારેય cross નથી થતું.

10 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

parser એ no કહ્યું

તમારું first hand-written events.xml load થવા નથી માંગતું. browser એક cold, single-line rejection ને show કરે છે line number સાથે, અને કોઈ page નહીં.

HTML એ તમને ક્યારેય આ રીતે treat નથી કર્યું. તમે YEARS સુધી closing tag ને forget કર્યું અને browsers quietly forgave કર્યું.

XML નું grammar નાનું છે (તમે તેનું બધું એક lesson માં hold કરી શકો છો) પણ દરેક rule enforced છે. તેમને rules-with-reasons તરીકે learn કરવું now saves કરે છે hour of line-number archaeology ને આ unit ના દરેક week માં.

Theory

declaration

XML file એ XML declaration સાથે open થઈ શકે છે:

<?xml version="1.0" encoding="UTF-8"?>

તે XML version અને character encoding ને announce કરે છે (UTF-8 events.xml માં Gujarati event names ને cover કરે છે). તે optional છે, પણ જો present હોય તો તે file ની very first thing હોવી જોઈએ: તેની ઉપર ન તો blank line નહીં ન તો comment. તેના rules પર full lesson આ unit ને close કરે છે; today, તેને recognise કરો અને first place કરો.

Theory

Elements: building blocks

element એ start tag, content, end tag છે:

<venue>Main Ground</venue>

Rules with teeth:

  • દરેક element closes થાય છે: XML પાસે કોઈ forgiving unclosed <br> નથી; even empty ones close થાય છે
  • empty elements self-close થાય છે: <online/> એ shorthand છે <online></online> માટે
  • names case-sensitive છે, spaces contain નથી કરતા, અને digit અથવા punctuation થી start નથી થવા જોઈએ (<2024event> illegal છે; <event2024> fine છે)
  • content text હોઈ શકે છે, other elements, અથવા બંને

Practical

events.xml, rules સામે annotated

<?xml version="1.0" encoding="UTF-8"?>  <!-- declaration: first thing -->
<events>                                <!-- root element opens -->
  <event id="1" type="cultural">        <!-- attributes: quoted values -->
    <name>Garba Night</name>            <!-- start tag + content + end tag -->
    <venue>Main Ground</venue>
    <online/>                           <!-- empty element, self-closed -->
  </event>                              <!-- તેના parent પહેલાં closes -->
</events>                               <!-- root closes last -->

Theory

Attributes અને nesting law

Attributes start tag પર name="value" pairs તરીકે ride કરે છે: <event id="1" type="cultural">. quotes mandatory છે: id=1 એ XML માં syntax error છે (HTML એ તેને shrug કર્યું).

Nesting properly contained હોવું જોઈએ: element તેના parent કરતા પહેલાં close થાય છે. <b><i>text</b></i> streams ને cross કરે છે અને rejected થાય છે.

discipline ને recognise કરો છો? Tags reverse order of opening માં close થાય છે: last opened, first closed. એ BCA304 નો stack છે, LIFO, grammar ને run કરતો. document જે આ page ના દરેક rule ને obey કરે છે તે official adjective ને earn કરે છે: well-formed.

Quiz

આમાંથી કઈ line well-formed XML છે?

  1. <event id=1><name>Garba</name></event>
  2. <event><name>Garba</event></name>
  3. <event id="1"><name>Garba</name></event>
  4. <Event><name>Garba</name></event>
Show the answer

<event id="1"><name>Garba</name></event>

Option C તેના attribute ને quote કરે છે, બંને elements ને close કરે છે, અને તેમને cross વગર nest કરે છે: દરેક rule satisfied. Option A unquoted id=1 પર loses છે, habit જે HTML tolerate કર્યું અને XML ક્યારેય નહીં. Option B nesting ને cross કરે છે: name જે event ની અંદર open થયું તે event કરતા પહેલાં close થવું જોઈએ (last opened, first closed). Option D case-sensitivity પર trips થાય છે: <Event> અને </event> એ different names છે, તેથી root ક્યારેય close નથી થતું. Exams આ exact question ને આ exact 3 mistakes થી build કરે છે; quotes, crossing, case ને check કરો, તે order માં.

Think first

Element અથવા attribute?

event નો id attribute માં sits કરે છે (id="1") જ્યારે તેનું venue child element માં છે. tap કરતા પહેલાં: શું કોઈ rule છે જે તે split ને force કરે છે, અને working convention શું છે?

Show the answer

કોઈ hard rule નથી: XML venue ને attribute તરીકે અથવા id ને element તરીકે accept કરે. working convention: attributes નાના identifying metadata માટે (ids, types, flags), elements data itself માટે (names, venues, times), કારણ કે elements nest, repeat અને grow કરી શકે છે (2 venues? બીજું element add કરો; attributes એક tag પર repeat નથી થઈ શકતા). Exams either design ને accept કરે છે જો તમે તેને defend કરી શકો; real feeds જેમ કે RSS exactly આ metadata-vs-data split ને follow કરે છે.

Watch out

4 rejection slips

જ્યારે events.xml load થવા નથી માંગતું, તે આમાંનું એક છે, likelihood order માં:

  • somewhere unclosed element (line number crime ની near points કરે છે, at નહીં)
  • unquoted attribute: id=1
  • case mismatch: <Name> closed by </name>
  • crossed nesting: parent ને child પહેલાં closing કરવું

અને BCA305-02 થી: Android Studio એ તમારા layouts માં આ same 4 ને flag કર્યું. Same grammar, same police.

Theory

Well-formed એ checklist છે, તેને use કરો

parser ને blame કરતા પહેલાં, 5-point inspection ને run કરો: declaration first (જો present હોય)? દરેક tag closed? attributes quoted? cases matching? nesting uncrossed? તે checklist IS well-formed ની definition છે, અને તેને list તરીકે writing કરવું એ standard 5-mark "state the rules of XML syntax" question ને verbatim answer કરે છે. બે rules ને today ફક્ત nod મળ્યો: single ROOT element અને case-sensitivity નું fine print. તેઓ next lesson ને headline કરે છે.

Summary

Key takeaways

  • declaration <?xml version="1.0" encoding="UTF-8"?> optional છે, પણ જો present હોય તો absolutely first આવે છે.
  • Element = start tag + content + matching end tag; બધું closes થાય છે, empties self-close થાય છે (<online/>).
  • Attributes start tag પર quoted name="value" pairs છે; unquoted values errors છે.
  • Names: case-sensitive, કોઈ spaces નહીં, ક્યારેય digit થી start નથી થવાનું.
  • Nesting ક્યારેય cross નથી થતું: last opened, first closed (grammar form માં BCA304 નો LIFO).
  • બધું obey કરો અને document WELL-FORMED છે; parsers કંઈ પણ less ને reject કરે છે.
  • Memory hook: quote it, close it, uncross it, match its case.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction of XML

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

XML syntax (Declaration, Tags, elements) · Web Designing-2 (option A) · Gri-Learn