Root element, case sensitivity

हर XML document में exactly एक root element होना ज़रूरी है जो बाक़ी सबको wrap करे, और XML case-sensitive है, तो <Event> और <event> अलग tags हैं और एक start tag को identical case वाले एक end tag से close होना ज़रूरी है।

8 min read · 9 cards · 2 checks

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


Theory

दो errors जो कुछ नहीं जैसे दिखते हैं

एक XML file तोड़ने के दो सबसे आसान तरीक़े इसे लगभग सही दिखाते छोड़ जाते हैं। पहला, top पर बिना एक single wrapper के दो elements बैठाना। दूसरा, एक <name> tag को </Name> से close करना।

दोनों एक quick glance में pass हो जाते हैं और दोनों document को well-formed नहीं रहने देते, तो Android layout build करने से मना करता है। ये दो rules तक आते हैं: exactly एक root element, और XML case-sensitive है। छोटे rules, बार-बार marks।

Theory

एक trunk, exact names

एक family tree में top पर एक trunk होता है जिससे सब कुछ branch होता है; आप एक ही tree में दो अलग trunks नहीं रख सकते। XML वैसा ही है: एक root element बाक़ी सब को holds करता है। और labels passwords जैसे हैं: case matters करता है। Aditi और aditi अलग logins हैं; <Event> और <event> अलग tags हैं। एक tag को ग़लत case से close कीजिए और XML कहता है 'यह same name नहीं है'।

Theory

दो rules

  • एक root element: एक XML document में exactly एक outermost element होना ज़रूरी है जो बाक़ी सब को wrap करे। Top level पर दो elements invalid है।
  • Case sensitivity: tag और attribute names case-sensitive हैं। <Note>, <note>, और <NOTE> तीन अलग tags हैं। एक start tag को identical case वाले एक end tag से close होना ज़रूरी है: <name>...</name>, कभी </Name> नहीं।

Android में यह attributes पर भी काटता है: यह android:layout_width है, android:Layout_Width नहीं।

Practical

सही और ग़लत

<!-- WRONG: two elements at the top, no single root -->
<name>Aditi</name>
<year>2</year>

<!-- RIGHT: one root <registration> wraps both -->
<registration>
  <name>Aditi</name>
  <year>2</year>
</registration>

<!-- WRONG: case mismatch on open/close -->
<Event>Robotics</event>    <!-- <Event> vs </event> -->

<!-- RIGHT: identical case -->
<event>Robotics</event>

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

एक Android layout हमेशा एक tag से क्यों शुरू होता है?

कोई भी FestConnect layout खोलिए और पूरी चीज़ एक अकेले <LinearLayout>...</LinearLayout> (या एक ConstraintLayout) के अंदर है। इसे root-element rule से connect कीजिए। क्या एक layout में एक TextView और एक Button दोनों बिल्कुल top level पर हो सकते हैं?

Show the answer

नहीं। क्योंकि XML को exactly एक root element चाहिए, एक layout file में एक अकेला outermost container होना ज़रूरी है, तो LinearLayout (या ConstraintLayout) सब कुछ wrap करता है। एक TextView और एक Button दोनों top level पर नहीं बैठ सकते; उन्हें उस एक root container के अंदर रहना ज़रूरी है। यही वजह है हर Android layout एक अकेले layout tag से खुलता और बंद होता है: यह XML root है, one-root rule मानते हुए।

Quiz

कौन सा XML fragment well-formed है?

  1. <data><a>1</a><b>2</b></data>
  2. <a>1</a><b>2</b>
  3. <Name>Aditi</name>
  4. <data><a>1</a><b>2</data></b>
Show the answer

<data><a>1</a><b>2</b></data>

Option A में एक अकेला root <data> है जो <a> और <b> को wrap करता है, सही nested और closed, तो यह well-formed है। B में दो top-level elements हैं और कोई root नहीं (invalid)। C case mismatch करता है: <Name> को </name> से closed किया (invalid)। D nesting cross करता है: </b> से पहले </data> (invalid)। सिर्फ़ A one-root, matched case, और साफ़ nesting एक साथ मानता है।

Watch out

Root और case traps

1. दो roots: single wrapper element भूल जाना। हमेशा siblings को एक root में enclose कीजिए (जैसे <registration> या <LinearLayout>)।

2. Close पर case mismatch: <name>...</Name> fail होता है। Exact case match कीजिए।

3. ग़लत-case Android attributes: android:Text recognize नहीं होगा; यह android:text है। XML case sensitivity attribute names पर भी लागू होती है, सिर्फ़ tags पर नहीं।

Theory

अगला: एक document के दो sections

अब आप जानते हैं एक document में एक root होता है। पर root से पहले भी content होता है: declaration और optional comments। अगला lesson एक XML document के दो parts को नाम देता है, prolog section (declaration और उसके दोस्त) और document element section (root और इसका content), एक पूरी XML file कैसे organized है यह साफ़ करते हुए।

Summary

Key takeaways

  • एक XML document में exactly एक root element होना ज़रूरी है जो बाक़ी सबको wrap करे; top-level पर दो elements invalid है।
  • XML case-sensitive है: <Event> और <event> अलग tags हैं।
  • एक start tag को identical case वाले एक end tag से close होना ज़रूरी है (<name>...</name>, </Name> नहीं)।
  • Android layouts में एक अकेला root container होता है (LinearLayout/ConstraintLayout) one-root rule की वजह से।
  • Case sensitivity attribute names पर भी लागू होती है: android:text, android:Text नहीं।
  • Memory hook: एक trunk, exact names।

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

Root element, case sensitivity · Mobile Application Development - 1 (option B) · Gri-Learn