XML declaration and rules of declaration

Declaration के rules कम और rigid हैं: lowercase <?xml, absolutely first (कोई space नहीं, कोई comment नहीं, ऊपर कोई blank line नहीं), version compulsory, encoding और standalone optional और उसी order में, values quoted।

9 min read · 9 cards · 2 checks

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


Theory

ગરબા નાઈટ Garbage बन जाता है

Committee चाहती है event names bilingual हों: <name>ગરબા નાઈટ</name> events.xml में join होता है। आपकी machine पर, ठीक है। Vendor के system पर, name question marks और boxes में render होता है।

File ने कभी नहीं बताया यह किस character encoding का इस्तेमाल करती है, तो vendor के parser ने guess किया, ग़लत।

Cure वह एक line है जो आप lesson 1 से autopilot पर type कर रहे हैं। आज उस line को closely पढ़ा जाता है, क्योंकि इसके rules rigid हैं और exams हर एक को check करते हैं।

Theory

Declaration, Member by Member

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

  • version: COMPULSORY, हमेशा पहला; practice में 1.0
  • encoding: optional; character set को नाम देता है। UTF-8 (default) हर script cover करता है जो events.xml को मिलेगी, ગરબા सहित। इसे declare करना vendor की guesswork हटा देता है
  • standalone: optional; yes का मतलब है document को समझने के लिए कोई external DTD file नहीं चाहिए; no का मतलब है शायद चाहिए

Delimiters: <?xml से open होता है, ?> से close होता है। यह एक instruction है, element नहीं: कोई closing tag exist नहीं करता, और यह कभी root count नहीं होता।

Theory

Declaration के Rules

पाँच rules, सब enforced:

  • Position: अगर present है, absolutely पहला: कोई blank line नहीं, कोई space नहीं, कोई comment नहीं, इसके ऊपर या पहले एक भी character नहीं
  • Case: lowercase <?xml; <?XML और <?Xml illegal हैं
  • Version mandatory है और attributes में सबसे पहले खड़ा होता है
  • Order fixed है: version, फिर encoding, फिर standalone; rearrange करना एक error है
  • Values quoted हैं, single या double, हर XML attribute की तरह

और पिछले lessons का meta-rule: declaration खुद OPTIONAL है। कोई declaration न होना legal है; एक misplaced या misspelt वाला नहीं है।

Practical

एक Valid, चार Invalid: क्यों Spot कीजिए

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

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

<!-- INVALID: version missing -->
<?xml encoding="UTF-8"?>

<!-- INVALID: order swapped -->
<?xml encoding="UTF-8" version="1.0"?>

<!-- INVALID: unquoted value -->
<?xml version=1.0?>

Quiz

एक file एक blank line से शुरू होती है, फिर line 2 पर <?xml version="1.0"?>, फिर एक perfect root element। Parser क्या कहता है?

  1. ठीक है: declaration से पहले वाला whitespace ignore हो जाता है
  2. Error: declaration को absolutely पहली चीज़ होना ज़रूरी है, और blank line इसे तोड़ती है
  3. ठीक है, पर declaration को एक comment की तरह treat किया जाता है
  4. Error: version 1.0 outdated है
Show the answer

Error: declaration को absolutely पहली चीज़ होना ज़रूरी है, और blank line इसे तोड़ती है

Position rule absolute है: declaration से पहले invisible whitespace भी नहीं आ सकता, तो एक innocent blank line document को invalid कर देती है: इस unit का सबसे common hand-editing bug, क्योंकि editors चुपचाप leading lines add कर देते हैं। Option A HTML की tolerance को ऐसी language में import करता है जिसमें कोई नहीं है। Option C एक downgrade mercy invent करता है जो parsers perform नहीं करते। Option D noise है: 1.0 practice में standard version बना हुआ है। Fix: blank line delete कीजिए; declaration file के पहले byte को touch करता है।

Think first

किस Line ने ગરબા નાઈટ को Save किया?

Tap करने से पहले precisely explain कीजिए encoding="UTF-8" vendor का question-mark garbage कैसे fix करता है। इसके बिना vendor का parser क्या कर रहा था?

Show the answer

एक file में text सिर्फ़ bytes है; एक encoding वह codebook है जो bytes को वापस characters में map करता है। UTF-8 में Gujarati letters हर एक multiple bytes span करते हैं, और vendor का parser, कुछ न बताए जाने पर, एक single-byte codebook guess कर लेता है: हर Gujarati byte एक ग़लत या unprintable Latin-era character में decode हो जाता है: इसलिए boxes और question marks। encoding="UTF-8" declare करना हर consumer को शुरू से सही codebook देता है। Habit: कोई भी XML जो कभी non-English text carry कर सकती है UTF-8 explicitly declare करती है, भले यह default हो, क्योंकि जिस दिन यह travel करती है वह दिन है जब defaults अलग होते हैं।

Watch out

तीन Declaration Slips

Invisible Blank Line: quiz वाला bug; जब एक valid-looking declaration error दे तो file का बहुत पहला character check कीजिए।

इसे एक Element की तरह Treat करना: इसे "close" करने के लिए </xml> लिखना, या इसे दूसरे root की तरह गिनना: यह एक instruction है; ?> इसका पूरा ending है।

Casual Attribute Order: version से पहले encoding किसी भी parser में compile नहीं होता; version, encoding, standalone का order grammar है, style नहीं।

Theory

Unit 1 बंद होती है: events.xml Complete है

Stock लीजिए: FestConnect का data एक well-formed events.xml में रहता है: UTF-8 declared, एक root, quoted attributes, matched cases, clean nesting, sections जिन्हें आप नाम दे सकते हैं। आप XML लिख सकते हैं और, उतना ही valuable, किसी अजनबी की DEBUG कर सकते हैं। Unit 2 seats पूरी तरह बदल देती है: data files से live page तक, जहाँ jQuery FestConnect की schedule को fold, fade और react कराती है। XML Unit 4 में वापस आती है, जब AJAX इसे बिना reload fetch करता है।

Summary

Key takeaways

  • Form: <?xml version="1.0" encoding="UTF-8" standalone="yes"?>, एक instruction, element नहीं।
  • अगर present है, यह file की absolutely पहली चीज़ है: ऊपर कोई space, line या comment नहीं।
  • सिर्फ़ Lowercase <?xml; version compulsory और पहला है; order fixed: version, encoding, standalone; values quoted।
  • encoding byte-to-character codebook को नाम देता है; UTF-8 Gujarati और Hindi safely cover करता है।
  • standalone="yes" का मतलब है कोई external DTD नहीं चाहिए।
  • Declaration optional है; एक ग़लत fatal है, एक missing ठीक है।
  • Memory hook: पहला byte, lowercase, version-encoding-standalone।

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 declaration and rules of declaration · Web Designing-2 (option A) · Gri-Learn