Core XML Technologies <Namespaces> Namespaces Why Namespaces? <Person> <Name>bob roth</name> <Phone>814.345.6789</Phone> </Person> <Hotel> <Name>Mariott</Name> </Hotel> If we combine these two documents and want to locate all people names, an XML app will have a hard time. Namespaces - based on unique prefixes Use URI references to disambiguate names from different documents/domains Add a prefix and we can identify two kinds of part elements <{http://www.cars.com/xml}part /> <{http://www.hairclubformen.com/xml}part /> BUT this is creates problems for XML as a NAME 1
Namespace Prefix Namespaces don t use a URI directly-- they use a prefix substitute <dbs:name>oracle</dbs:name> NOTE: The prefix is NOT the Namespace <dbs:name>oracle</dbs:name> Need a way to MAP some arbitrary prefix (e.g. dbs) to the URI {http://www.dunbradstreet.com} use a prefix that is mapped to a URI e.g. http://www.dunbradstreet.com Namespaces - declared as attribute note that the dbs prefix has its own prefix = xmlns <dbs:foo xmlns:dbs='http://www.dunbradstreet.com > <dbs:company>ariba</dbs:company> <phone>213.456.7890</phone> p </dbs:foo> when declaring a namespace one MUST use the xmlns prefix as in xmlns:dbs the dbs prefix is mapped to http://www.dunbradstreet.com Namespaces - declared as attribute note that the dbs prefix has its own prefix = xmlns <dbs:foo xmlns:dbs='http://www.dunbradstreet.com > <dbs:company>ariba</dbs:company> <phone>213.456.7890</phone> phone element is NOT part </dbs:foo> of defined namespace the scope of dbs is the element where declared and all subelements 2
Namespaces and attributes <edi:purchase xmlns:edi='http://ecommerce.org/sch ema'> <edi:lineitem edi:taxclass="exempt">baby food </edi:lineitem> </edi:purchase> attribute taxclass is in the http://ecommerce.org/schema namespace Multiple Namespaces <?xml version="1.0"?> <! two namespace prefixes --> <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> Default Namespace -- no Prefix <stock xmlns='http://www.nyse.com > <name> Macromedia </name> <product> Flash </product> </stock> No :prefix after xmlns means no shorthand prefix BUT all elements in scope are considered (by default) part of http://www.nyse.com namespace Default and Defined namespaces <?xml version="1.0"?> <book xmlns= http://gov.books' xmlns:isbn= http://www.books.org > <title rating= PG13 >Cheaper by the Dozen</title> <isbn:number>1568491379</isbn:number> </book> The book and title elements are part of the default http://gov.books namespace Note: attributes are not automatically part of any namespace 3
Example: Two Prefixes One namespace Two prefixes may map to the same namespace <bookz:book xmlns:bookz= http://www.books.org xmlns:isbn= http://www.books.org > <bookz:title>cheaper by the Dozen</bookz:title> <isbn:number>1568491379</isbn:number> </ bookz:book> All the above elements are part of the SAME namespace Default namespace If the URI reference in a default namespace declaration is empty, then unprefixed elements in the scope of the declaration are not considered to be in any namespace. <book xmlns= xmlns:isbn= http://www.books.org > <title rating= PG13 >Cheaper by the Dozen</title> <isbn:number>1568491379</isbn:number> </book> book, title and rating are NOT in ANY namespace Default Namespace -- no Prefix <stock xmlns='http://www.nyse.com > <name> Macromedia </name> <product xmlns= > Flash </product> </stock> The stock and name elements are considered (by default) part of http://www.nyse.com namespace The product element NOT part of ANY namespace Namespace-aware Parsers Check that all prefixes are mapped to URIs except for xml: and xmlns: Rejects element and attribute names with more than one colon 4
Namespaces and DTDs Completely independent of each other Document can have one, both or none Namespaces does not change result of validity check If document uses foo:bar as element name then DTD must have: <!ELEMENT foo:bar (#PCDATA)> Namespace Quiz Valid or NOT?? <!DOCTYPE Greeting [ <!ELEMENT Greeting (#PCDATA)> Valid or NOT?? <!DOCTYPE Greeting [ <!ELEMENT Greeting (#PCDATA)> <!ATTLIST Greeting xmlns:gg CDATA #IMPLIED> <Greeting xmlns:gg = http://www.foo.org > </Greeting> NOT <Greeting xmlns:gg = http://www.foo.org > </Greeting> YES Valid 5
Valid or NOT?? <!DOCTYPE Greeting [ <!ELEMENT Greeting (#PCDATA)> <!ATTLIST Greeting xmlns:gg CDATA #IMPLIED> Valid or NOT?? <!DOCTYPE Greeting [ <!ELEMENT Greeting (#PCDATA)> <!ATTLIST Greeting xmlns:gg CDATA #IMPLIED> <Greeting xmlns:gg = bozozeclown > </Greeting> YES Valid <gg:greeting xmlns:gg = http://www.gg.com > </gg:greeting> NOT Valid Valid or NOT?? <!DOCTYPE gg:greeting [ <!ELEMENT gg:greeting (#PCDATA)> <!ATTLIST gg:greeting g xmlns:gg CDATA #IMPLIED> Namespace Reading Namespaces in XML (W3C Recommendation) http://www.w3.org/tr/rec-xml-names/ Namespace philosophy: http://www.jclark.com/xml/xmlns.htm <gg:greeting xmlns:gg = http://www.gg.com > </gg:greeting> Valid 6
ANT and XML Validation ANT Ant is an XML vocabulary for automating computing tasks (similar to the make utility) Widely used to run and configure Java but has extensible capabilities Ant has a task for validating XML documents called xmlvalidate To use Ant you must download Java and the Ant jar file http://ant.jakarta.com & http://java.sun.com/j2se/ When ANT is installed.. Create a text file called build-valid.xml Insert the following text: <?xml version="1.0"?> <project default="valid"> <target name="valid"> <xmlvalidate file="date.xml"/> </target> </project> name of file to be validated execute ant from the command line C:\Java\Ant>ant -f build-valid.xml -f means get ant file based on next parameter 7
ant produces the following output, if successful: Buildfile: build-valid.xml valid: [xmlvalidate] 1 file(s) have been successfully validated. BUILD SUCCESSFUL Total time: 2 seconds Ant -- fileset Using the fileset type as a child of xmlvalidate, you can validate a series of XML documents, as shown in buildfileset.xml: <?xml version="1.0"?> <project default="valid"> <target name="valid"> <xmlvalidate> <fileset file="date*.xml"/> </xmlvalidate> </target> </project> JAVA & Validation Using JAXP, one can request a validating parser that will tell you if an XML document is valid The source code for JAXPValidator.java is available from the class web site Install the J2SE and compile the java code Execute and pass document name as parameter Output for Valid XML %javac JAXPValidator.java creates a JAXPValidator.class file %java JAXPValidator filename.xml --if successful, output will be: Document 1 has been read 8
Output for Invalid XML (Java) Warning: validation was turned on but an org.xml.sax.errorhandler was not set, which is probably not what is desired. Parser will use a default ErrorHandler to print the first 10 errors. Please call the 'seterrorhandler' method to fix this. Error: URI=file:///C:/fpc/Invaliddoc.xml Line=5: Element type "Greeting" must be declared. Document 1 has been read 9