Why do i need to prefix an XML element with x:?
I saw various cases where elements in XML file are prefixed wi开发者_开发问答th 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?
"x:" is a namespace alias called x. Somewhere above that element there will be an xmlns:x attribute pointing to some url, the url is usually not important, its just an identifier for the namespace.
"x" is just a name local to the file, it could just as well be xmlns:foo, as long as foo: is used everywhere x: was used.
Namespaces are typically used by xml processors to make sure they are looking for the right elements (for xlst transforms for example), not custom elements the user has defined in the xml.
You can read more about xml namespaces here
The x:
is a namespace identifier in your XML. An XML subtree can have one »default« namespace for elements and attributes (set by xmlns='...'
). If you need elements or attributes from other namespaces you need to define a prefix, such as xmlns:x='...'
. Elements and attributes you use from that namespace then need to be prefixed with x:
. A common one I have is
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
which then requires me for the use
element to do the following:
<use xlink:href='#foo'/>
In this case the use
element comes from SVG, while the href
attribute comes from XLink. Both are different, but in some places compatible specifications.
And that's basically what namespaces are for:
- Group different XML uses into coherent parts
- Allow them to be used in a single XML
- Potentially avoid name clashes (if you have two namespaces and both allow for an element called
foo
, then you need to specify which of the twofoo
s 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:
<Window
x:Class="W"
Height='500'
Width='500'
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
The Class
attribute 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:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
so that you can emit generated XML like so:
<xsl:if test="Content/Chapter">
<xsl:variable name="text"><xsl:value-of select="$trans//item[@name='content'][lang($lang)]" /></xsl:variable>
<h2><xsl:value-of select="$text"/></h2>
<ol>
<xsl:apply-templates select="Content" mode="TableOfContents"/>
</ol>
</xsl:if>
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.
精彩评论