XML declaration and rules of declaration

XML declaration वह line है <?xml version="1.0" encoding="UTF-8"?> जो file खोलती है, और इसके rules strict हैं: यह बिल्कुल पहली चीज़ होनी चाहिए (इससे पहले कोई space या comment नहीं), lowercase xml, version पहले बताया हुआ, फिर encoding, फिर optional standalone, सभी values quoted।

8 min read · 9 cards · 2 checks

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


Theory

वह line जो आपने बिना पढ़े copy की

FestConnect की हर XML file <?xml version="1.0" encoding="UTF-8"?> से खुलती है। आपने इसे कई बार type किया है। अब इसे पढ़िए: यह announce करता है यह XML है, कौन सा version, और text को कौन सा character set encode करता है।

यह trivial दिखता है, पर इसमें XML के कुछ सबसे strict rules हैं: यह पहली होनी चाहिए, यह lowercase होनी चाहिए, और इसके attributes का एक required order है। Examiners इसे exactly इसलिए पसंद करते हैं क्योंकि यह इतना rule-bound है।

Theory

Shipping crate पर label

एक shipping crate पर एक label बाहर, आगे और बीच में चिपका होता है: 'Contents: glass. This side up. Version 1.'। यह goods का हिस्सा नहीं है; यह handler को बताता है crate को कैसे treat करें। XML declaration वह label है: data नहीं, बल्कि file के बारे में instructions (कौन सा version, कौन सी encoding)। और एक label की तरह, यह बिल्कुल आगे होना चाहिए, कुछ भी unpack होने से पहले पढ़ा जाना चाहिए।

Theory

Declaration और इसके parts

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

  • version (required, आमतौर पर 1.0): पहले आना ज़रूरी है।
  • encoding (optional): character set, आमतौर पर UTF-8।
  • standalone (optional, yes/no): क्या document external declarations पर depend करता है।

यह एक processing instruction है (<? ... ?>), कोई element नहीं, तो इसका कोई closing tag नहीं है। सभी values quoted हैं। Declaration ख़ुद optional है, पर recommended है।

Practical

सही और ग़लत declarations

<!-- CORRECT -->
<?xml version="1.0" encoding="UTF-8"?>
<registration> ... </registration>

<!-- WRONG: not the first thing (comment before it) -->
<!-- a fest file -->
<?xml version="1.0"?>

<!-- WRONG: uppercase XML -->
<?XML version="1.0"?>

<!-- WRONG: encoding before version -->
<?xml encoding="UTF-8" version="1.0"?>

Think first

Declaration fix कीजिए

एक file एक blank line से शुरू होती है, फिर <?Xml encoding="UTF-8" version="1.0">। हर rule list कीजिए जो यह तोड़ती है, फिर corrected line लिखिए।

Show the answer

टूटे rules: (1) इसके पहले एक blank line है, declaration बिल्कुल पहली चीज़ होनी चाहिए; (2) `Xml` ग़लत case है, यह lowercase `xml` होना चाहिए; (3) encoding version से पहले है, पर version पहले आना चाहिए; (4) यह > से ख़त्म होती है `?>` के बजाय। Corrected: <?xml version="1.0" encoding="UTF-8"?> line एक पर, इससे पहले कुछ नहीं। चार छोटे rules, चार आसान marks।

Quiz

कौन सी XML declaration सही तरह लिखी गई है?

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?xml encoding="UTF-8" version="1.0"?>
  3. <?XML version="1.0"?>
  4. <?xml version=1.0 encoding=UTF-8?>
Show the answer

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

Option A सही है: lowercase xml, version पहले, encoding दूसरा, सभी values quoted, ?> से closed। B encoding को required-first version से पहले रखता है। C uppercase XML इस्तेमाल करता है। D values के चारों तरफ़ quotes छोड़ देता है, जो XML को चाहिए। Declaration के fussy rules, पहले, lowercase, version-first, quoted, exactly वही हैं जो यह question test करता है।

Watch out

Declaration traps

1. इससे पहले कुछ भी: <?xml ...?> से पहले whitespace या comment भी नहीं होना चाहिए। यह line एक है।

2. ग़लत case या order: यह lowercase xml है, और version encoding से पहले आना चाहिए।

3. Unquoted values या `>` के बजाय `?>`: values quoted हैं, और यह ?> से close होती है क्योंकि यह एक processing instruction है, element नहीं।

Theory

XML master हुआ: वापस बनाने पर

अब आप अपने project में कोई भी XML पढ़ और लिख सकते हैं: layouts, manifest, string resources, और data records। Unit 4 इसे काम में लगाता है: आप FestConnect की screen पर वापस जाते हैं और असली Android widgets जोड़ते हैं, title bar hide करते हुए, orientation lock करते हुए, text fields और एक onClick वाला Button रखते हुए, और एक Toast pop करते हुए, हर एक उस XML में defined जो आप अब समझते हैं और Java में wired।

Summary

Key takeaways

  • XML declaration है <?xml version="1.0" encoding="UTF-8"?>: यह version और character set बताती है।
  • version required है और पहले आता है; encoding और standalone optional हैं और उस order में इसके बाद आते हैं।
  • अगर मौजूद है, declaration बिल्कुल पहली line होनी चाहिए, इससे पहले कुछ भी नहीं (whitespace/comments भी नहीं)।
  • Keyword lowercase xml है; सभी values quoted हैं; यह ?> से close होती है (एक processing instruction, element नहीं)।
  • Declaration optional है पर recommended है।
  • Memory hook: shipping crate पर label, आगे और बीच में, सबसे पहले पढ़ा गया।

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 XML (Extensible Markup Language)

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

XML declaration and rules of declaration · Mobile Application Development - 1 (option B) · Gri-Learn