370 likes | 979 Views
Source: XML Bible 2 nd edition Elliotte Rusty Harold ISBN:0-7645-4760-7. The Complete Reference XML,Heather Williamson ISBN:0-07-212734-1 http://www.w3.org/TR/1999/REC-xml-names-19990114/#ns-breakdown. XML Namespaces. by Navin Kumar Vedagiri. XML Namespaces. What is Namespace?
E N D
Source: XML Bible 2nd edition Elliotte Rusty Harold ISBN:0-7645-4760-7. The Complete Reference XML,Heather Williamson ISBN:0-07-212734-1 http://www.w3.org/TR/1999/REC-xml-names-19990114/#ns-breakdown XML Namespaces by Navin Kumar Vedagiri
XML Namespaces • What is Namespace? • A namespace defines a new scope. • Provide a way to avoid name collisions. • What is XML Namespace? • To give multiple elements of same name within the same document
XML Namespace • XML allows users to create own markup language for their own projects • One such example is MathML (Mathematics Markup Language) • MathML is derivative of XML • MathML is used for displaying complex equations • XML documents containing MathML tags in one single document. • This is where XML Namespaces comes in
Example <?xml version =“1.0”?> <MATH_ASSIGNMENT> <Question id =1> <MATH> <MI>a</MI> <MI>a</MI> <MI>a</MI> <MI>a</MI> <MI>a</MI> <Instructions> A,B,C are sides of triangle……</Instruction> </MATH> </Question> <Question id =2> <MATH> <Instructions> If a train…………………</Instruction> </MATH> </Question> </MATH_ASSIGNMENT> <?xml version =“1.0”?> <MATH> <MI>a</MI> <MI>a</MI> <MI>a</MI> <MI>a</MI> <MI>a</MI> </MATH>
Conflicts In Name • Base element name in MathML is <MATH> • If any of element name in XML code uses <MATH> • Compiler should know which of the <MATH> object should be treated to MathML instructions and which should be interpreted as XML markup • This clearly shows Why we wanted Namespace and how scope of the name can be defined.
Using Namespaces In C++ • Declaring Namespace in C++ namespace direct { class Arrow { public: Arrow(int dir); void setDirection(int dir); private: int direction; } // ...... other stuff } • Using Namespace using namespace direct; Direct.Arrow a = new Arrow();
Declaring Namespace In XML • Syntax for declaring a namespace <elementname xmlns:prefix =“namespaceurl”> • Namespace applied for MathML <MATH xmlns:nm=http://www.w3.org/TR/REC-MathML”>
Explanation Of Using Namespace • Prefix is used to identify that is associated with the element or attribute • In XSL, either element name or attribute name after namespace prefix can be used to identify • Namespace prefix is used with “xmlns” prefix to identify source of DTD used to identify universal attribute family
Example <?xml version=“1.0”?> <MATH_ASSIGNMENT xmlns:mdoc=“http://www.catsback.com/MATH/”> <mdoc:INSTRUCTIONS> Solve the following…..</mdoc:INSTRUCTIONS> <mdoc:Question id=“1”> <MATH xmlns:math=“http://www.w3.org/TR/REC-MathML”> <math:MI>a</math:MI> <math:MO>+</math:MO> <math:MI>b</math:MI> <math:MO>=</math:MO> <math:MI>c</math:MI> <mdoc:INSTRUCTIONS> Where A,B,C…… </mdoc:INSTRUCTIONS> </MATH> </mdoc:Question> </MATH_ASSIGNMENT>
Explanation Of Using Namespace • <MATH_ASSIGNMENT> & <MATH> elements are using namespace declarations • <MATH_ASSIGNMENT> refers to ww.catsback.com/Math • Namespace for child elements need not be declared separately • Namespace will be automatically applied to child elements and its attributes
URI – Uniform Resource Identifier • Abstraction of URL • URL locates a resource, URI identifies a resource • URI doesn’t have to point at any particular file • URI defines a namespace • Groups and disambiguate element & attribute name • Document need not exist at the URI specified
Qualified Name • Combination of prefix and local part name • eg., <mdoc:Question> or <math:MI> • Prefix is used as a placeholder for namespace name • If the document scope extends past the current document then full URL should be used • Example serv:Address Address
Qualified Attribute • Applies prefix directly to name of the element or attribute within the markup • Opening and closing tags must have prefix applied • If qualifying within an element, apply prefix to name of the attribute
Default Namespaces • Applied to an element where it is declared • URL is left blank in “xmlns:” declaration • Children goes to default namespace without explicit declaration • Do not apply to attributes • Processed faster than namespace prefixes
Example of Default Namespace <?xml version=“1.0”?> <MATH_ASSIGNMENT xmlns = “http://www.catsback.com/MATH/”> <INSTRUCTIONS> Solve the following….. </INSTRUCTIONS> <Question id=“1”> </Question> </MATH_ASSIGNMENT>
Adding Namespace To DTDs • Incorporating Namespace to DTD’s is beneficial and problematic • Since xmlns:prefix declaration is treated as an attribute, must be defined within the DTD • All the element name must refer to their namespace
Adding Namespace To DTDs Book DTD <!ELEMENT BOOK (DIV+)> <!ELEMENT DIV (HEAD, DIV*, PGPH*)> <!ELEMENT HEAD (#PCDATA)> Sonnet DTD <!ELEMENT SONNET (STANZA,STANZA,STANZA,REFRAIN)> <!ELEMENT STANZA (LINE,LINE,LINE LINE)> Play DTD <!ELEMENT PLAY (ACTOR, WRITER)> <!ELEMENT ACTOR(Fname, Lname, Age)> <!ELEMENT WRITER(Name,Type)>
<b:BOOK xmlns:b=“book.dtd” xmlns:s =“sonnet.dtd” xmls:p=“play.dtd”> <b:DIV> <b:HEAD>…….</b:HEAD> </b:DIV> <b:DIV> <b:HEAD>…..</b:HEAD> <s:SONNET> <s:STANZA id =“st1”> <s:LINE>…….</s:LINE> </s:STANZA> <s:STANZA id =“st2”> <s:LINE>…….</s:LINE> </s:STANZA> </s:SONNET> <p:PLAY> <p:Actor> <p:Fname>…..</p:Fname> </p:Actor> </p:Play> </b:DIV>
Namespace Scoping • To the element where it is specified and to all elements within the content of that element • If overridden by another namespace declaration with the same NSAttName part • Multiple Namespace scope <?xml version="1.0"?><!-- both namespace prefixes are available throughout --><bk:book xmlns:bk='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'> <bk:title>Cheaper by the Dozen</bk:title> <isbn:number>1568491379</isbn:number></bk:book>
Stylesheet - XML Namespace example <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html> <body> <xsl:for-each select="prompt"> <xsl:value-of select="greeting" /> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>
Constraints • Entity names, Processing Instruction Targets • Overriding default namespace <A xmlns="http://www.foo.org/"> <B> <C xmlns=""> <D>abcd</D> </C> </B> </A>
Namespace scope for an element • Example 1: <A xmlns="http://www.foo.org/"> <B xmlns="http://www.bar.org/"> <C>abcd</C> </B> </A> • Example 2: <foo:A xmlns:foo="http://www.foo.org/"> <foo:B xmlns:foo="http://www.bar.org/"> <foo:C>abcd</foo:C> </foo:B> </foo:A>
Mapping Qualified Names • Example 1: <?xml version="1.0" ?> <A xmlns="http://www.foo.org/" C="bar"> <B>abcd</B> <A> • Example 2: <?xml version="1.0" ?> <A C="bar"> <B>abcd</B> <A>
Declaring at Root <Department xmlns:addr="http://www.tu-darmstadt.de/ito/addresses" xmlns:serv="http://www.tu-darmstadt.de/ito/servers"> <Name>DVS1</Name> <addr:Address> <addr:Street>Wilhelminenstr. 7</addr:Street> <addr:City>Darmstadt</addr:City> </addr:Address> <serv:Server> <serv:Name>OurWebServer</serv:Name> <serv:Address>123.45.67.8</serv:Address> </serv:Server> </Department>
Validation of XML-Namespace • Validity is a concept defined in XML • XML namespaces are layered on top of XML • XML namespaces recommendation does not redefine validity • xmlns attributes are treated as attributes, not XML namespace declarations. • Qualified names are treated like other names.
Summary • Namespaces distinguis between elements and attributes • Identifies, which belong to XML and to other markup • Declared by xmlns attribute • Prefix is attached to element and attributes • No prefix – Default namespace • DTD’s using namespaces should be designed carefully