XML Namespace
Namespace avoid conflicts with element. For example: In the example below the XML has 2 section one for book author details and other one is having the details of library customers. Both elements have similar tags that might be bight confusing. So to make it more simpler to understand we use namespaces. <?xml version = "1.0" encoding = "UTF-8" ?> <library> <author> <name>Honey Maxi</name> <age>45</age> <gender>Male</gender> </author> <author> <name>Jack Mauga</name> <age>39</age> <gender>Male</gender> </author> <customer> <name>Zaphod Beeblebrox</name> <age>19</age> <gender>Male</gender> </customer> <customer> <name>June Hox</name> <age>15</age> <gender>Female</gender> </customer> </library> |
Defining a Namespace
Syntax: xmlns:prefix=”URI“ xmlns – Namespace declaration always starts with xmlns which stands for XML namespace. There are multiple places we can define a namespace:
|
Referring Defined Namespace
Once we have defined our namespace we need to refer our elements using the prefix defined. <?xml version = "1.0" encoding = "UTF-8" xmlns:customer="https://www.tutorialsatoz.com/customer" xmlns:author="https://www.tutorialsatoz.com/author" ?> <library> <author> <author:name>Honey Maxi</author:name> <author:age>45</author:age> <author:gender>Male</author:gender> </author> <author> <author:name>Jack Mauga</author:name> <author:age>39</author:age> <author:gender>Male</author:gender> </author> <customer> <customer:name>Zaphod Beeblebrox</customer:name> <customer:age>19</customer:age> <customer:gender>Male</customer:gender> </customer> <customer> <customer:name>June Hox</customer:name> <customer:age>15</customer:age> <customer:gender>Female</customer:gender> </customer> </library> |
5 Replies