I saw various cases where elements in XML file are prefixed with x: or something else.
What is the purpose for this?
Also, what is the meaning of the “namespace” for the xml? (usually some URL)? how is it being used in any way?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
x:is a namespace identifier in your XML. An XML subtree can have one »default« namespace for elements and attributes (set byxmlns='...'). If you need elements or attributes from other namespaces you need to define a prefix, such asxmlns:x='...'. Elements and attributes you use from that namespace then need to be prefixed withx:. A common one I have iswhich then requires me for the
useelement to do the following:In this case the
useelement comes from SVG, while thehrefattribute comes from XLink. Both are different, but in some places compatible specifications.And that’s basically what namespaces are for:
foo, then you need to specify which of the twofoos you want.In that way namespaces are not much different from how they work in many programming languages. You could view the things you import by namespaces a little like libraries in above SVG case. The people writing SVG noticed that there is already a specification allowing linking to arbitrary XML elements (XLink) and they simply re-used it. In other cases, such as WPF, tehre exist namespaces to separate the declarational UI stuff of the presentation framework and the glue attributes needed to interface with code. E.g. a window there might look like this:
The
Classattribute is only needed for the compiler to know the appropriate codebehind class, it doesn’t have any visible difference on the window.Another example is XSLT, where you define transformations on XML documents. Both the XSLT instructions and the templates for the output are XML. You usually use an explicit namespace for XSLT’s elements:
so that you can emit generated XML like so:
If I were to make the
xsl:namespace the default namespace, then the XSLT processor couldn’t tell my to-generate HTML from the actual instructions. That’s a problem if both your code and data use the same format and live in the same place. I believe Lisp solved that by marking data with a single-quote.As for namespaces being an URI, that’s probably just a W3C thing. There is no requirement for anything to live at those URIs. They are just identifiers. They are not meant to be retrieved. A program consuming XML with namespaces must know the namespaces it can handle and act appropriately.