XML syntax (Declaration, Tags, elements)

एक page में XML grammar: एक 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 ने मना कर दिया

आपकी पहली hand-written events.xml load होने से मना कर देती है। Browser एक line number के साथ एक ठंडा, single-line rejection दिखाता है, और कोई page नहीं।

HTML ने आपके साथ कभी ऐसा behave नहीं किया। आप एक closing tag YEARS तक भूलते रहे और browsers चुपचाप माफ़ करते रहे।

XML की grammar छोटी है (आप इसे पूरा एक lesson में hold कर सकते हैं) पर हर rule enforced है। अभी इन्हें rules-with-reasons की तरह सीखना इस unit के हर week की एक घंटे की line-number archaeology बचाता है।

Theory

Declaration

एक XML file XML declaration से खुल सकती है:

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

यह XML version और character encoding announce करता है (UTF-8 events.xml के Gujarati event names cover करता है)। यह optional है, पर present होने पर file की बहुत पहली चीज़ होना ज़रूरी है: इसके ऊपर एक blank line या comment भी नहीं। इसके rules पर एक पूरा lesson इस unit को बंद करता है; आज, इसे पहचानिए और पहले रखिए।

Theory

Elements: Building Blocks

एक element start tag, content, end tag है:

<venue>Main Ground</venue>

Rules जिनके teeth हैं:

  • हर element close होता है: XML में कोई forgiving unclosed <br> नहीं है; empty वाले भी close होते हैं
  • Empty elements self-close होते हैं: <online/> <online></online> का shorthand है
  • Names case-sensitive हैं, कोई spaces नहीं रखते, और किसी digit या punctuation से शुरू नहीं होने चाहिए (<2024event> illegal है; <event2024> fine है)
  • content text, दूसरे 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>                              <!-- closes before its parent -->
</events>                               <!-- root closes last -->

Theory

Attributes और Nesting Law

Attributes start tag पर name="value" pairs की तरह सवार होते हैं: <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 करता है और reject हो जाता है।

Discipline पहचानते हैं? Tags reverse order of opening में close होते हैं: last opened, first closed। यही BCA304 का stack है, LIFO, grammar चला रहा है। इस page के हर rule को obey करने वाला document एक official adjective कमाता है: 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 पर हारता है, वह habit जो HTML ने tolerate किया और XML कभी नहीं करेगा। Option B nesting cross करता है: event के अंदर opened name को event से PEHLE close होना ज़रूरी है (last opened, first closed)। Option D case-sensitivity पर trip करता है: <Event> और </event> अलग names हैं, तो root कभी close नहीं होता। Exams exactly इन 3 mistakes से यह exact question बनाते हैं; quotes, crossing, case, इसी order में check कीजिए।

Think first

Element या Attribute?

Event का id एक attribute में बैठता है (id="1") जबकि इसका venue एक child element में। Tap करने से पहले: क्या कोई rule इस split को force करता है, और working convention क्या है?

Show the answer

कोई hard rule नहीं: XML venue को attribute की तरह या id को element की तरह accept कर लेता। Working convention: छोटे identifying metadata के लिए attributes (ids, types, flags), खुद data के लिए elements (names, venues, times), क्योंकि elements nest, repeat और grow कर सकते हैं (2 venues? एक और element add कीजिए; attributes एक tag पर repeat नहीं हो सकते)। अगर आप defend कर सकें तो exams दोनों designs accept करते हैं; RSS जैसे real feeds exactly इसी metadata-vs-data split को follow करते हैं।

Watch out

4 Rejection Slips

जब events.xml load होने से मना करती है, यह likelihood order में इनमें से एक है:

  • कहीं एक unclosed element (line number crime के पास point करता है, exact नहीं)
  • Unquoted attribute: id=1
  • Case mismatch: <Name>, </name> से closed
  • Crossed nesting: child से पहले parent close करना

और BCA305-02 से: Android Studio ने आपके layouts में यही same 4 flag किए थे। Same grammar, same police।

Theory

Well-Formed एक Checklist है, इसे Use कीजिए

Parser को blame करने से पहले, 5-point inspection run कीजिए: declaration पहले (अगर present)? हर tag closed? attributes quoted? cases matching? nesting uncrossed? वह checklist well-formed की definition IS है, और इसे list की तरह लिखना standard 5-mark "XML syntax के rules state कीजिए" question को verbatim answer देता है। आज सिर्फ़ 2 rules को एक nod मिला: single ROOT element और case-sensitivity की fine print। वे अगले lesson को headline करते हैं।

Summary

Key takeaways

  • Declaration <?xml version="1.0" encoding="UTF-8"?> optional है, पर present होने पर absolutely पहले आता है।
  • Element = start tag + content + matching end tag; सब कुछ close होता है, empties self-close होते हैं (<online/>)।
  • Attributes start tag पर quoted name="value" pairs हैं; unquoted values errors हैं।
  • Names: case-sensitive, कोई spaces नहीं, कभी किसी digit से शुरू नहीं होते।
  • Nesting कभी cross नहीं होती: last opened, first closed (BCA304 का LIFO grammar form में)।
  • इस सब को obey कीजिए और document WELL-FORMED है; parsers इससे कम कुछ भी reject कर देते हैं।
  • Memory hook: quote कीजिए, close कीजिए, uncross कीजिए, इसका case match कीजिए।

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