Web hosting
Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Monday, 18 September 2017

XML Parser's

  • Introduction to XML Parser in Hindi
एक parser बहुत ही basic और important tool होता है। हर XML application parser पर based होती है। Parser एक software component होता है जो XML document और application (Ex. Browser) के बीच में होता है। Parser programmer को XML syntax की complexity से बचाता है।  Parser XML file के syntax को validate करता है।

Parser के द्वारा आप XML document के data को access और modify कर सकते है। Parser को आप एक software library की तरह देख सकते है जो आपको XML document के साथ work करने के लिए methods provide करती है।

XML के कुछ parser's के नाम निचे दिए जा रहे है। इनमें DOM और SAX parser सबसे popular है।
  • DOM 
  • SAX 
  • JDOM 
  • STAX 
  • XPATH 
  • DOM4J 

Validating and Non-validating Parser's 

XML parser's को 2 categories में divide किया गया है। Non-validating parser's वो parser होते है जो Syntax rules को validate कर सकते है लेकिन DTD validate करने की ability उनमें नहीं होती है। 

Validating parser's वो parser होते है जो syntax rules को validate करने के साथ DTD को भी validate करते है। 

जैसा की मैने पिछली एक tutorial में बताया था की XML documents well-formed और valid 2 तरह के होते है। Well formed documents सिर्फ syntax को follow करते है जबकि valid documents syntax को follow करने के साथ Document को DTD के according structure करते है। 

Non-validating parser हर तरह के document को well-formed document की तरह देखता है। क्योंकि इस तरह के parser में DTD को validate करने की ability नहीं होती है। यदि कोई valid document parse भी किया जाये तो non-validating parser सिर्फ उसका syntax ही validate करते है। 

चाहे document well-formed हो या valid दोनों ही cases में validating parser easily काम करता है।    

Working of Parser and Application 
Parser XML file को validate करता है। जैसे की parser check करता है की XML document का syntax ठीक है या नहीं। इसके बाद यदि document valid है तो parser DTD को भी validate करता है। Document के validate होने के बाद parser application को interface provide करता है जिसके द्वारा application XML file के data को extract करती है। Parser के द्वारा provide किये गए interface 2 तरह के हो सकते है। 

  • Object based interface - इस तरह के interface में parser Objects का एक tree बनाता है जिसमे XML file के सभी elements होते है। ये सबसे simple interface होता है क्योंकि ये memory में एक tree होता है जो hard disk पर किसी file को represent करता है।   
  • Event based interface - Event based interface बहुत powerful होता है। ज्यादतर programmers इसे ही यूज़ करते है। Event based interface में parser tree create करने के बजाय file को read करता है और जैसे ही कोई element या attribute आता है तो event generate करता है। इस तरह के interface में element start, element end, attributes और text content के लिए events generate किये जाते है।               


Different XML Parser's  


DOM (Document Object Model) Parser 

DOM का मतलब document object model होता है। ये parser complete XML document का representation memory में build करता है। और जब किसी program को उसकी जरुरत होती है तो उसे पूरी memory सौंप देता है। DOM बहुत ज्यादा memory यूज़ करता है। ये memory nodes में relationship build करने के कारण occupy होती है। DOM को बहुत ही complicated माना जाता है। इसको बहुत से programming languages के साथ implement किया गया है लेकिन ये same ही interface provide करता है।

       

SAX (Simple API for XML) Parser 

SAX का मतलब Simple API for XML होता है। इस parser की विशेषता ये होती है की ये XML को unit by unit read करता है और events create करता है जो application यूज़ कर सकती है। इससे application जो data जरुरी नहीं है उसे ignore कर देती है और जिस data पर events create किये गए है उसी पर focus करती है। ये एक push parser है क्योंकि ये events को application की तरफ push करता है।  

इस parser की disadvantage ये होती है की calling application को जिस data की जरुरत होती है उसे उसको track करके रखना पड़ता है। SAX को ज्यादातर बड़े XML documents के लिए यूज़ किया जाता हैं जँहा पर memory document की size से कम हो। 

SAX parser के कई implementation आ चुके है। जैसे की crimson and xerces आदि। 


Pull Parser 

Pull parser application के लिए wait करता है। जैसे ही application call करती है ये next available event को find करता है। और जब तक की XML file का end नहीं आ जाता है application loop चलाती है। Pull parser streaming applications के लिए useful होता है जँहा पर data या तो बहुत ज्यादा बड़ा है या फिर data को next stage के लिए prepare किया जा रहा है। इसे large data resources के साथ work करने के लिए यूज़ किया जाता है। SAX parser सभी events को return करता है लेकिन pull parser चाहे तो events को skip भी कर सकता है 



Read more ...

Transforming with XSLT

XML and XSLT (Extensible StyleSheet Language)

  • Introduction to XML and  XSLT in Hindi 
  • Various XSLT processors in Hindi 
  • Different XSLT elements in Hindi 
  • Example of XML with XSLT in Hindi 

Introduction to XML and XSLT 

CSS के अलावा भी XML को human readable form में represent किया जा सकता है। इसके लिए आप XSLT (Extensible StyleSheet Language) यूज़ करते है। XSLT एक programming language है जिसे XML documents को transform और represent करने के लिए यूज़ किया जाता है।

सबसे पहले आप एक XML document create करते है। इसके बाद एक XSLT file create करते है। फिर दोनों को एक computer program के द्वारा combine करके एक नयी file generate की जाती है। नयी file एक XML file हो सकती है या fir plain text की भी file हो सकती है।


CSS के comparison में देखें तो XSLT एक programming language होती है। इसमें input parameters, conditions और function calls होते है। XSLT एक procedural programming language ना होकर declarative होती है। XSLT top to bottom fashion में processing करती है। XSLT में एक बार variable की value define करने के बाद आप उसे change नहीं कर सकते है।



Various XSLT (Extensible StyleSheet Language) Processors         

अलग अलग operating system और programming languages जैसे की java, perl आदि पर based बहुत से XSLT processors available होते है। आप अपनी need के according suitable processor यूज़ कर सकते है। कुछ processors के बारे में निचे दिया जा रहा है। 

Xerces 

Xerces java based XML processor होता है। ये apache का software libraries के collection होते है। इनसे आप XML को parse, validate, serialize और manipulate कर सकते है। ये libraries DOM, SAX और SAX2 जैसे API's को implement करती है। Xerces Java, Perl और C++ में implement किया गया है। 

Xalan 

Xalan एक XSLT processor है जो XML document को HTML, text और दूसरे document types में convert करने के लिए यूज़ किया जाता है। ये processor XSLT 1.0 XML transformation language को implement करता है। ये processor java और C++ दोनों languages के लिए available है।

xsltproc

ये एक binary application होती है जो different c libraries को यूज़ करके बनायीं जाती है। और इस application में एक xmllint नाम का program भी होता है जो XML documents को validate करने के लिए यूज़ किया जाता है। 

Sablotron

ये भी एक binary application होती है जो C++ libraries को यूज़ करते हुए बनायीं गई है। और इसमें perl और python API होती है। ये एक बहुत fast, portable और compact toolkit होती है। ये application XSLT 1.0, DOM Level 2 और XPATH 1.0  को implement करती है। 

Saxon  

Saxon XML processor को Michael kay ने develop किया था। इस processor को java में implement किया गया है। 


XSLT Elements/Commands  

XSLT program language की form में एक XML file होती है। इसलिए XSLT की हर command एक element है। हर command को attributes से qualify किया जाता है। निचे आपको कुछ basic commands की list दी जा रही है।     
        
Elements  
Description  
stylesheet  
ये सभी XSLT files का root element होता है। इस element के 2 attribute होते है जिनमे आप XSLT namespace और version number define करते है।

 Ex.  <xsl:stylesheet xmlns:xsl="URI" version="1.0">   
output  
Output के रूप में किस तरह की file generate होगी ये आप output element के द्वारा define करते है। Output की गई file में indentationऔर DTD required हैं या नहीं ये भी इसी element के द्वारा define किया जाता है। 

Ex. <xsl:output indent="yes">    
template  
इस command के द्वारा आप XML file का एक particular part match करते है। इस element का एक attribute होता है match जो define करता है की XML tree की किस branch को process करना है। उदाहरण के लिए यदि आप पुरे XML tree को process करना चाहते है तो / यूज़ कर सकते है। 

Ex. <xsl:template match="/">     
value-of 
यदि आप किसी एक particular attribute का result चाहते है तो इसके लिए आप value of command यूज़ कर सकते है। Value को select करने के लिए select attribute यूज़ किया जाता है।  

Ex. <xsl value-of select="/person/age">   
apply-templates  
ये command current XSLT file को select attribute में define की गई template के लिए search करता है। यदि कोई भी template नहीं मिलती है तो current template को output कर दिया जाता है। 

Ex. <xsl:apply-templates select="paragraph|list">     
Text  
Commands के आलावा XSL file में text भी होता है जिसे processor simple values की तरह treat करता है।   
       

Example 

XML File

<name> Ram Sharma </name> 

XSLT File
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="URI">
<xsl:output method='text'/>
<xsl:template match="/content">
<xsl:value-of select='.'/>
<xsl:text>&#xa;</xsl:text> 
</xsl:template>
</xsl:stylesheet>   

ऊपर दी गई xslt file को ध्यान से देखिये। सबसे पहले हमने इसे XML file की तरह define किया है। उसके बाद इसे XSLT stylesheet के रूप में define किया गया है। Third line में output file का format define किया गया है। इसके बाद template command के द्वारा root element के content को search किया है। इसके बाद root element की value को output किया गया है। इसके बाद line feed character input किया गया है। इसके बाद open किये गए सभी elements को close किया गया है 
Read more ...

Formatting XML with CSS (Cascading Style Sheets)

Formatting XML with CSS (Cascading Style Sheets) 
  • Introduction to formatting XML with CSS in Hindi 
  • Formatting XML font with CSS in Hindi  
  • Formatting alignment and display of XML with CSS in Hindi 

    Introduction to Formatting XML with CSS 

    CSS (Cascading Style Sheets) के द्वारा आप XML को और भी ज्यादा human readable form में represent कर सकते है। CSS content को touch किये बिना उसके presentation को चेंज कर देती है। 

    CSS file को आप XML document के साथ attach कर सकते है। इसके बाद आप दोनों को browser में process कर सकते है। CSS के साथ आप XML document के content को बहुत ही आकर्षक तरीके से present कर सकते है। 

    कोई भी CSS file selectors और declarations से मिलकर बनी होती है। CSS file का हर selector XML document के element से match करता है। हर selector में फिर curly braces के अंदर declarations दिए जाते है। ये property/value pair में होते है। XML का content कैसे format होगा ये declarations define करते है। 

    किसी भी CSS file को XML document में include करने के लिए आप निचे दिया गया statement यूज़ करते है। 


    <?xml-stylesheet type="text/css" href="filename.css"?>

    ये statement processor को बताता है की इस XML document को define की गई CSS file के साथ represent करना है। 

    आइये XML के साथ CSS के इस्तेमाल को एक उदाहरण द्वारा समझने का प्रयास करते है। 

    XML document

    <?xml version="1.0">
    <?xml-stylesheet type="text/css" href="person.css">
    <person>
    <name>Ram</name>
    <age>24</age>
    </person>

      
    यँहा पर एक XML document show किया गया है। इसमें आप देखेंगे की ऊपर define किये गए statement के द्वारा person.css file को access किया गया है। इस file को निचे दिया जा रहा है।     

    CSS file 

    person {font-size:12pt;}
    name {color:blue;}
    age {color:red;}

                
    जैसा की मैने आपको पहले बताया selectors XML document के element से match होते है। यँहा पर सभी selectors XML document के elements से मैच कर रहे है। जिस selector में जो declarations define की गई है वो उसी element पर apply होती है। यदि एक ही property parent और child दोनों में define की गई है तो child property parent property को override कर लेती है।     

    जैसा की आप CSS file में देख सकते है property और value को colon से separate किया गया है। और इस complete property/value pair को semicolon से separate किया जाता है। Semicolons से separate करके आप और pairs define कर सकते है।     



    Different CSS Formatting Properties 

    CSS आपको बहुत सी properties provide करती है जिनकी मदद से आप अपने XML document को अपने according represent कर सकते है। इनमे से कुछ properties के बारे में निचे दिया जा रहा है। 

    Display 

    किसी element को show करना है या नहीं और show करना है तो कैसे करना है ये सब display property के द्वारा define किया जाता है। Display attribute की सबसे important values निचे दी जा रही है।

     Property : Values  
    Description  
     display : inline 
    Inline default value होती है। इसका मतलब होता है की content के बाद line break नहीं आएगा।   
    display : none  
    None value का मतलब होता है की content show नहीं होगा।   
    display : block  
    Block value का मतलब होता है की content से पहले और बाद में line breaks include होंगे।   
    display : list-item 
    list-item value block की तरह ही होती है बस इसमें content में भी line breaks include होते है।   
      

    Margin 

    Text के आसपास खाली space को define करने के लिए margin property यूज़ की जाती है। इस attribute की values को percentage में भी दिया जा सकता है या फिर pixels में भी आप define कर सकते है। आप चाहे तो पुरे text का margin एक बार में define कर सकते है या फिर हर side का independently भी define किया जा सकता है। इनके बारे में निचे दिया जा रहा है।     

    Property : Values  
     Description 
    margin : 5px;  
    ये simple margin property होती है। इसे define करने से सभी sides का margin एक बार में define हो जाता है।    
    margin-left : 5px; 
    इस property से आप left side का margin define कर सकते है।   
    margin-right : 5px; 
    ये property right side का margin define करने के लिए यूज़ की जाती है।  
    margin-top:5px; 
    Top side का margin define करने के लिए आप इस property को यूज़ कर सकते है।   
    margin-bottom:5px; 
    इस property से आप bottom side का margin set कर सकते है।   


    Text align

    Text को different तरह से position करने के लिए text align property यूज़ की जाती है। इस property की common values निचे दी जा रही है। 

    Property : Values  
     Description 
    text-algin : right; 
    Right value से आप text की position right में set करते है।   
    text-algin : left; 
    इस value से text की position left में set की जाती है।   
    text-align : center; 
    Center value से text की position को center में set किया जाता है।   
    text-align : justify; 
    ये value set करने से text automatically content और space के according position हो जाता है।   
       

    Font family 

    इस property के द्वारा किसी भी element के content की font family define की जाती है। इस property की value कोई भी font family का नाम होता हैं। यदि font family का नाम एक word से ज्यादा है तो आप उसे single quotes में लिखते है।

    font-family : verdana;
    font-family:'Times New Roman';


    Font size 

    Font family की तरह ही आप font की size भी define कर सकते है। इसके लिए आप font -size property यूज़ करते है। इस property की common values निचे दी जा रही है।   

    Property : Values  
    Description 
    font-size:small;
    ये font size छोटी और normal होती है। 
    font-size:x-small
    ये font size small size से भी छोटी होती है।  
    font-size:large;
    ये font size बड़ी होती है।  
    font-size:xx-large;
    ये font size सबसे बड़ी होती है।  
    font-size:5pt;
    आप चाहे तो खुद की font size भी define कर सकते है।  


    Font weight 

    Font को bold करने के लिए आप font-weight property यूज़ करते है। इस property की value bold define करने से element का content bold हो जाता है। इसका उदाहरण निचे दिया जा रहा है। 

    font-weight : bold; 


    Font style 

    Element के content को italic show करने के लिए font-style property यूज़ की जाती है। इस property की value italic pass की जाती है। इसका उदाहरण निचे दिया जा रहा है। 

    font-style : italic; 
        

    Displaying XML with CSS

    1. सबसे पहले XML और CSS की 2 separate file create कर लीजिए। 
    2. इसके बाद <?xml-stylesheet> tag यूज़ करते हुए XML document में CSS file करवाइये। 
    3. इसके XML file को browser के साथ open कर दीजिये। CSS file automatically load हो जाएगी

    Read more ...

    XML Namespaces

    XML Namespaces 

    • Introduction to XML namespaces in Hindi
    • Creating XML namespaces in Hindi 
    • Steps to create XML namespaces in Hindi 
    • Example 

    Introduction to XML Namespaces 

    XML एक extensible language है। इसका मतलब ये है की आपके document को कोई और programmer भी extend कर सकता है और उसमे required changes करते हुए यूज़ कर सकता है। ये एक बहुत ही अच्छा feature है और सभी programmers इसे पसंद करते है। इस feature के साथ एक समस्या ये है की extend किये गए document के tags आपके document के tags के साथ conflicts कर सकते है। तो क्या आपको दूसरे documents को extend नहीं करना चाहिए।

    इस problem का एक solution ये हो सकता है की सभी tags को predefined बनाकर उनको एक global directory में save कर लिए जाये और सभी programmers उन tags को यूज़ करते हुए documents create करे।लेकिन ऐसा करने से XML भी HTML की तरह ही limited हो जाएगी। और W3C  नहीं चाहती थी की XML एक limited language बने। इसलिए उन्होंने इस problem का solution निकाला जिसे की XML namespaces कहा जाता है। 


    Creating XML Namespaces 

    Namespaces इस problem को solve भी करते है और XML को limited भी नहीं बनाते है। Namespaces में आप पहले namespaces create करते है। Namespace create करते समय आप उसमे prefix और URI जैसी information pass करते है। Namespace को root tag में xmlns attribute define किया जाता है। XMLNS attribute के आगे colon लगाकर prefix define किया जाता है। इसके बाद assignment operator लगाकर namespace का URI pass किया जाता है। इस attribute से आप एक से ज्यादा namespaces भी define कर सकते है। आइये इसे एक उदाहरण से समझने का प्रयास करते है।

    <?xml version="1.0">

    <person xmlns:pmale="www.besthinditutorials.com/personmale" xmlns:pfmale="www.besthinditutorials.com/personfemale>

    <pmale:name>Ram</pmale:name>
    <pmale:age>24</pmale:age>

    <pfmale:name>Sita</pfmale:name>
    <pfmale:age>23</pfmale:age>


    </person>

    ऊपर दिए गए उदाहरण में हमने male person को और female person को namespaces के द्वारा separate करने की कोशिश की है। जैसा की आप ऊपर देख सकते है example में 2 namespaces create किये गए है और दोनों के structure same है। इन दोनों को URI से differentiate किया जाता है।



    Steps to Create XML Namespaces 

    1. सबसे पहले xmlns attribute define करिये। 
    2. इसके बाद xmlns के आगे colon लगाकर एक prefix define कीजिये। ये वो prefix होता है जिससे आप create किये गए namespace में elements add करते है। 
    3. इसके बाद assignment operator लगाकर एक unique domain URI define कीजिये। ये URI namespace को दूसरे namespace से differentiate करता है। 


    Syntax For Creating XML Namespaces 

    <?xml version="1.0">

    <root-element xmlns:prefix="URI">

    <prefix:sub-element>text</prefix:subelement>

    </root-element>  

    आप चाहे तो एक से अधिक namespaces भी define कर सकते है इसका तरीका निचे दिया जा रहा हैं।


    <?xml version="1.0">

    <root-elment xmlns:prefix1="URI1" 
    xmlns:prefix2="URI2">

    <prefix1:sub-element>Content </prefix1:sub-element>

    <prefix2:sub-element>Content</prefix2:sub-element>

    </root-element>



    Prefix 

    Prefixes को अलग अलग namespaces में elements को add करवाने के लिए यूज़ किया जाता है। Prefixes namespaces को uniquely identify नहीं कर सकते है क्योंकि दो developers एक ही नाम के prefixes create कर सकते है। Prefixes का काम सिर्फ इतना होता है की आप इनसे namespaces में elements add कर सकते है।

    URI 

    URI's namespaces को uniquely identify करने के लिए यूज़ किये जाते है। सबसे बेहतर तरीका होता है की आप अपनी domain name से एक URL create कर ले                         
    Read more ...

    XML Entities and Notations

    XML Entities and Notations 

    • Introduction to XML entities and notations in Hindi 
    • Types of XML entities in Hindi 

    Introduction to XML Entities 

    जैसा की मैने पहले बताया XML files के साथ नहीं entities के साथ काम करती है। Entities XML documents का physical representation होती है। हालाँकि entities को files की तरह store किया जाता है लेकिन ऐसा करने के कोई जरुरत नहीं होती है। 

    किसी XML document में DTD और दूसरी files जिनको DTD refer करती है वो सभी entities कहलाती है। यँहा तक की XML document खुद एक entity होता है क्योंकि यह XML processor के लिए starting point होता है। Document की entity document entity के नाम से जानी जाती है।

    XML entities को store और access करना नहीं बताती है। ये काम XML processor का होता है। XML processor entities को download भी कर सकता है या local catalog यूज़ करके भी entities को access कर सकता है।


    Types of Entities 

    XML में कई तरह की categories होती है mainly इनको 3 categories में divide किया गया है।

    1. General and parameter entities 
    2. Internal and external entities 
    3. Parsed and Un-parsed entities    
     इन सभी entities के बारे में detail से निचे दिया जा रहा है।


    General and Parameter Entities 

    General entity references text और markup में कँही भी आ सकते है। ज्यादातर general entities macros की तरह यूज़ की जाती है। जैसे ही यदि किसी बड़ी information को आपको document में कई जगह लिखना है तो इसके लिए आप general entity से short form generate कर सकते है। जँहा भी आप उस entity को लिखेंगे processor उस जगह उस पूरी information को process कर सकता है। इसका structure निचे दिया जा रहा है।इसे यूज़ करके आप आसानी से general entities create कर सकते है। General entities create करने के लिए <!ENTITY> tag यूज़ किया जाता है। ENTITY के बाद entity का नाम लिखा जाता है और उसके बाद single quotes में वो information लिखी जाती है जिसे आप process करना चाहते है।       

    <!ENTITY entity-name
    '  your 
                 big-text 
                                   here in single quotes
    '
    >  


    एक बार entity को define करने के बाद जँहा भी आप उसे यूज़ करना चाहते है वँहा पर & लिखकर entity का नाम लिख सकते है। जैसे की निचे दिया गया है। 


    &entity-name


    Parameter entity references केवल DTD में ही यूज़ की जा सकती है। Parameter entities को declare करने के लिए % का यूज़ किया जाता है। इसका structure निचे दिया जा रहा है।

     <!ENTITY % entity-name "(parameter1||parameter2) 'default-value' ">
      
    Parameter entity को यूज़ करने के लिए आप निचे दिया गया syntax यूज़ कर सकते है।

    %entity-name  


    Internal and External Entities       

    यदि entity DTD में ही declare की गई है तो वह internal entity कहलाती है। Internal entities general भी हो सकती है तो parameter entities भी हो सकती है। 

    यदि entity को DTD के बाहर किसी दूसरे document में declare किया गया है तो ऐसी entity external entities कहलाती है। इन entities को access करने के लिए entity के नाम के बाद SYSTEM attribute में उस file का URL दिया जाता है जिसमें entities declare की गई है। इसका structure निचे दिया जा रहा है।     

    <!ENTITY entity-name SYSTEM "URL">  


    Notations 

    हर entity को XML processor process नहीं कर सकता है इसके लिए एक specific application की जरुरत पड़ती है। जैसे की images के लिए एक image viewer की आवश्यकता होती है। 

    Notation basically एक mechanism होता है जिसमे वो सभी entities जो process नहीं की जा सकती उनको एक specific entity के साथ जोड़ दिया जाता है। एक notation declare करना बहुत ही आसान होता है। इसके लिए <!NOTATION> tag यूज़ किया जाता है। इसका structure निचे दिया जा रहा है। 

    <!NOTATION entity-identifier SYSTEM "URL of application"> 
          
    यँहा पर entity-identifier entity का नाम होता है।   
    Read more ...

    XML Schema's

    XML Schema's 

    • Introduction to XML schema's in Hindi 
    • XML DTD (Document Type Definitions) in Hindi 
    • XML well formed documents in Hindi 
    • XML valid documents in Hindi 

    Introduction to XML Schema's 

    HTML में सभी tags predefined होते है। इसलिए HTML documents का structure almost fix होता है। सभी HTML editors और दूसरे software's इस common structure से परिचित होते है। यही कारण है की HTML editors को पहले से ही HTML programs के लिए optimize किया जा सकता है। क्योंकि HTML में सब कुछ fix और पहले से defined है इसलिए कुछ भी नया unexpected नहीं होता है। जैसे की HTML में सभी tags predefined है इसलिए editors इनको different color में show कर सकते है। और यदि आप कोई दूसरी language भी साथ में यूज़ कर रहे है तो editors HTML code को उससे separate कर सकते है। 

    XML में tags predefined नहीं होते है। इसलिए XML documents का structure भी fix नहीं होता है। XML editors और दूसरे software's को document के structure का कोई idea नहीं होता है। XML editors को पहले से ही किसी XML document के लिए optimize नहीं किया जा सकता है। इसका reason ये है की हमे पहले से पता नहीं होता है की developer क्या और कैसे बनाने वाला है। 

    ये situation editors और language के बीच में एक gap create करती है। इस gap को DTD (Document Type Definition) के द्वारा fill किया जा सकता है। DTD (Document Type Definition) XML document का description होता है। DTD एक XML schema है। एक schema XML document के बारे में कुछ characteristics define करता है जिससे XML editors उस document के बारे में जान सकते है। हालांकि XML में बहुत से schema's available है लेकिन DTD उनमे सबसे popular और ज्यादा use होता है। इसलिए यँहा पर में आपको सिर्फ DTD के बारे में ही बताऊंगा। आइये इसके बारे में जानने का प्रयास करते है। 

    DTD (Document Type Definition) 

    जैसा की मैने आपको ऊपर बताया DTD एक XML document का description होता है। XML editors या दूसरे software's इस read कर सकते है। DTD को read करके editors खुद को document को process करने के लिए prepare कर सकते है। जैसे की XML processor document को process करने के लिए appropriate changes कर सकता है। और programmer को better experience देने के लिए भी XML document को DTD के द्वारा optimize किया जा सकता है। 

    उदाहरण के लिए DTD को read करने के बाद कुछ editors XML document के सभी elements की list publish कर सकते है।  

    DTD का role ये specify करना होता है की कोनसा element document में किस जगह पर आएगा। जैसे की यदि DTD में <name> element <address> से पहले आ रहा है तो document में भी <name> element पहले ही आएगा। ऐसा नहीं होने पर XML processor error show करता है। 

    DTD processor को एक particular structure follow करने के लिए विवश कर देता है। जैसे की जिस order में elements DTD में है उसी order में processor उनको देखता है। यदि ऐसा नहीं है तो document reject कर दिया जाता है। 

    DTD के द्वारा content को elements से separate किया जाता है। जब सभी elements DTD में define कर दिए जाते है तो parser में ये ability आ जाती है की वो content को elements से separate कर सकता है।

    DTD के माध्यम से elements की default और fixed values declare की जा सकती है। 



    इससे पहले की आप DTD declare करना सीखें में आपको बता दूँ की DTD 2 तरह से define की जा सकती है।
    • Internal DTD - इस तरह की DTD को XML document में define किया जाता है। जब document की size small होती है तो उसी में DTD define करना ठीक रहता है। 
    • External DTD - इस तरह की DTD को document से separate create किया जाता है। जब document की size large हो तो ये तरीका better होता है। 

    निचे DTD का common structure दिया गया है। इसे यूज़ करके आप आसानी से DTD define कर सकते है।   

    <?xml version="1.0">

    <!DOCTYPE root-element [

    <!ELEMENT element1 (subelement1,subelement2,subelement3) >
    <!ELEMENT element2 (subelement-list) >
    <!ELEMENT element3 (#PCDATA) >

    ]> 
          
    जैसा की structure में show किया गया है root element को define करने के लिए <!DOCTYPE tag को यूज़ किया जाता है। ये root element को दर्शाता है। बाकि सभी elements इसी tag के angular brackets में define किये जाते है। लेकिन ऐसा नहीं है की आप इसके बाद root element को define नहीं करेंगे। इसके बाद सबसे पहले root element और उसके sub elements को ही declare किया जाता है।

    जैसा की आप देख सकते है की एक single element को define करने के लिए <!ELEMENT tag यूज़ किया जाता है। Element का नाम लिखने के बाद आप brackets में sub elements को भी comma से separate करके लिख सकते है।

    जब भी किसी element के बाद brackets में #PCDATA लिखा जाता है तो इसका मतलब parser को ये बताना होता है की इस element को parse किया जाना चाहिए। जब #CDATA लिखा जाता है तो उसका मतलब होता है की ये element parse नहीं किया जाना चाहिए।

    आइये अब इसे एक उदाहरण से देखते है।

     <?xml version="1.0">

    <!DOCTYPE person[

    <!ELEMENT person (name,age) >
    <!ELEMENT name (#PCDATA) >
    <!ELEMENT age (#PCDATA) >

    ]>

    <person>

    <name> Ram </name>
    <age> 24 </age>

    </person>
                              
    ऊपर दिया गया उदाहरण internal DTD का है। जैसा की आप देख सकते है same ही document में DTD को भी define किया गया है। आइये अब external DTD को एक उदाहरण से समझने का प्रयास करते है।

    DTD Document
    <?xml version="1.0">

    <!DOCTYPE person [
    <!ELEMENT person (name,age) >
    <!ELEMENT name (#PCDATA) >
    <!ELEMENT age (#PCDATA) >

    ]> 

    XML Document 

    <?xml version="1.0">

    <!DOCTYPE person SYSTEM "person.dtd">

    <person>
    <name> Ram </name>
    <age> 24 </age>
    </person>


    जैसा की आप देख रहे की external DTD यूज़ करने के लिए XML document में एक extra line add की गई है। इस line के द्वारा external parser को ये बताया गया है की person.dtd नाम की DTD को यूज़ करना है। External DTD भी internal की तरह ही होती है बस सिर्फ एक line का difference होता है।         


    Types of XML Documents 

    XML documents को उनके configuration के according 2 categories में divide किया गया है। आइये इनके बारे में जानने का प्रयास करते है। 

    Well Formed XML Documents 

    • इस तरह के documents XML syntax follow करते है। 
    • इस तरह के documents में start और end tags का proper यूज़ होता है। 
    • इस तरह के documents में attributes भी properly यूज़ किया जाता है। 
    • Well formed documents के साथ DTD यूज़ नहीं जाती है।  
    • इस तरह के documents का structure editor को पता नहीं होता है।  

    Valid XML Documents 

    • Valid XML documents XML syntax को follow करते है। 
    • इस तरह के documents DTD में describe किये हुए structure को follow करते है। 
    • इस तरह के document का structure processor और editor को पता होता है। 
    Read more ...

    XML Syntax & Commenting

    XML Syntax & Commenting 

    • Introduction to XML syntax in Hindi 
    • XML Declaration in Hindi 
    • XML tags in Hindi 
    • XML attributes in Hindi 
    • Commenting in XML in Hindi 

    Introduction to XML Syntax 

    XML syntax 2 तरह की information को show करता है। एक तो markup information होती है। जो tags के द्वारा show की जाती है। दूसरी text information में होती है जो की tags के बीच में real data होता है। Markup information text information को define करती है। Markup information किसी meta data की तरह होती है।

    XML का syntax बहुत ही easy और readable है। बस आपको एक बार इसे समझने की आवश्यकता होगी और फिर बाद में आप शायद ही कभी इसे भूलें। 

    आइये XML से related सभी elements के syntax और functions के बारे में जानने का प्रयास करते है। 

    XML Declaration 

    XML declaration से XML processor को XML document को process करने से पहले उसके बारे में information मिल जाती है। XML declaration optional होता है। जब भी आप XML declaration यूज़ करे तो ध्यान रहे की ये आपके document की पहली line होनी चाहिए। 

    XML declaration <?xml  से शुरू होता है और ?> से end किया जाता है। XML declaration के 3 attributes होते है। इनके बारे में निचे दिया जा रहा है। 
    • Version - ये attribute document में यूज़ किया गया XML का version define करता है। इसकी regular value 1.0 होती है।    
    • Encoding - ये attribute document में यूज़ की गई character encoding define करता है। इसकी regular values UTF-8, UTF-16 और EUC-JP आदि हो सकती है। 
    • Standalone - ये attribute parser को बताता है document standalone है या DTD पर depend करता है। इस attribute की regular values ये और no होती है।  

    XML declaration के लिए कुछ rules follow किये जाते है। इनके बारे में आपको पता होना चाहिए। ये निचे दिए गए है।
    • यदि XML declaration include किया गया है तो उसमे version number जरूर होना चाहिए। बाकी attributes को आप चाहे तो ignore कर सकते है। 
    • Attributes के name और values case sensitive होती है। 
    • Attributes के names हमेशा lower case में होने चाहिए। 
    • आप double quotes की जगह single quotes भी यूज़ कर सकते है। 

    XML declaration के 2 उदाहरण निचे दिए जा रहे है। 

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

    XML Tags 

    कोई भी XML document XML tags से मिलकर बना होता है। XML tags XML का base है। XML tags अलग अलग तरह की information को carry करते है। XML tags के द्वारा आप comments insert कर सकते है या फिर कोई setting जो की parsing के लिए required है उसे भी configure कर सकते है। 

    XML tags को आप 3 तरह से जान सकते है। इनके बारे में निचे दिया गया है। 
    • Start tag - ये starting tag होता है। इसके बाद text information आती है।   
    • End tag -  ये end tag होता है। Normally end tags में backslash होता है।   
    • Empty tag - जब start और end tags के बीच कोई content नहीं होता तो वह empty tag कहलाता है। 


    XML tags को define करने के लिए कुछ rules follow किये जाते है। इनके बारे में निचे दिया जा रहा हैं।

    1. XML tags case sensitive होते है। जो case आपने start tag में यूज़ किया है वही आपको end tag में भी यूज़ करना चाहिए। 
    2. आपको XML tags को properly close करना चाहिए।       

      जब start tag और end tag content और attribute के साथ लिखे जाते है वो elements कहलाते है। इसका उदाहरण निचे दिया गया है।

      <person gender="male">
      <name>Ram</name>
      <age>24</age>
      </person>


      XML Root Tag 
      Root tag किसी भी XML file में सबसे पहला और आखिरी tag होता है। बाकी सभी tags root tag के बीच में लिखे जाते है। जैसे की ऊपर दिए गए example में <person> root tag है। 

      XML Attributes 

      किसी भी XML tag के एक से ज्यादा attributes हो सकते है। लेकिन ये सभी unique होने चाहिए। एक attribute किसी tag के बारे में additional information provide करता है। 

      <tag-name att1="value" att2="value">
      content
      </tage-name> 

      Attributes name और value के pair में लिखे जाते है। Attributes की values double quotes में लिखी जाती है।

      <person>
      <name gender="male">Ram</name>
      <age>24</age>
      </person>

      यदि 2 tag same नाम के है तो आप उन्हें attributes के द्वारा differentiate कर सकते है। इसका उदाहरण निचे दिया जा रहा है।         

      <person>
      <name category="firstName">Ram</name>
      <name category="lastName">Sharma</name>
      <age>24</age>
      </person>

      XML document में attributes को properly यूज़ करने के लिए आपको कुछ rules follow करने चाहिए। इनके बारे में निचे दिया जा रहा है। 

      1. आप किसी भी attribute को एक tag में एक से ज्यादा बार define नहीं कर सकते है। 
      2. एक attribute Document Type Definition में declared होना चाहिए। 
      3. Attribute की values कोई भी direct या indirect entity references नहीं हो सकते है।       


      Commenting in XML 

      XML में comments करना बहुत ही आसान है। जैसे आप HTML में comment करते है वैसे ही XML में भी comment किया जाता है। इसका syntax और example निचे दिए जा रहे है। 

      <!---------- Comment Here----------->

      Example 
        
       <?xml version="1.0">
      <!----- This files stores information about a person---->
      <person>
      <name>Hanuman</name>
      <age>infinite</age>
      </person> 
      Read more ...