Click Here

NAMESPACES XML is a method to resolve conflicts that may exist with the same names of tags for different topics.

Because the XML tags are defined by the user, in our application we take data from 2 external XML as "mails.xml" and "notifications.xml" it may be that some of their tags with the same name (element, to, from , ...).

To solve the problem use NAMESPACES and prefixes.

Given the following file "mails.xml"

<?xml encoding="UTF-8"?> <notifications> <message>     <message>          <to>John</to>          <from>Peter</from>          <title>Meeting</title>          <text>Remember out meeting</text>     </message> </message> </notifications>

Given the following file "notifications.xml"

<?xml encoding="UTF-8"?> <notifications> <message>     <message>          <to>John</to>          <from>Peter</from>          <title>Meeting</title>          <text>Remember out meeting</text>     </message> </message> </notifications>

In this example, we observed that some tag names (message, to, from, title, ..) are the same in both files.

If we use both files in a single website, then we must modify these files and add XML namespaces and prefixes to tags that can cause problems.

Syntax for using namespaces:


Example of using namespaces:

<?xml xmlns:m="http://www.webpage.com/mail" encoding="UTF-8"?>

So would the XML file "mails.xml" and "notifications.xml" applying NAMESPACES

<?xml xmlns:m="http://www.webpage.com/mail" encoding="UTF-8"?> <mails> <message>     <message>          <to>John</to>          <from>Peter</from>          <title>Meeting</title>          <text>Remember out meeting</text>     </message> </message> </mails>

<?xml xmlns:m="http://www.webpage.com/notif" encoding="UTF-8"?> <notifications> <message>     <message>          <to>John</to>          <from>Peter</from>          <title>Meeting</title>          <text>Remember out meeting</text>     </message> </message> </notifications>

Another workaround is to add a prefix to tags that can be replicated.

For example, add the prefix "m" for tags of "messages.xml" and add "t" for tags of "notifications.xml" that can be replicated in other XML

<?xml encoding="UTF-8"?> <mails> <m:message>     <m:message>          <m:to>John</m:to>          <m:from>Peter</m:from>          <m:title>Meeting</m:title>          <m:text>Remember out meeting</m:text>     </m:message> </m:message> </mails>

<?xml encoding="UTF-8"?> <notifications> <t:message>     <t:message>          <t:to>John</t:to>          <t:from>Peter</t:from>          <t:title>Meeting</t:title>          <t:text>Remember out meeting</t:text>     </t:message> </t:message> </notifications>

Click Here