Reputation: 773
I know the functionality of Namespaces which is used in C++ and C. But I do not understand this line of code in XSLT:
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
What is it there for? What would happen if I miss this? (Nothing happens if I delete this line)... And what is this pointing to?`
I've read some Posts about this in W3Cschools... But I still didn't understood ...
Thanks in advance.
Upvotes: 1
Views: 765
Reputation: 243459
This question deserves a better explanation.
Here is an element named table
:
<table material="wood"
legs="4" shape="rectangle" type="dining"/>
From the values of its 4 attributes it is clear that this describes a wooden, dining table with four legs, and rectangular shape.
Here is another table
element:
<table name="Product">
<columns>
<column name="id"/>
<column name="name" type="string"/>
<column name="price" type="decimal"/>
<column name="date-produced" type="date"/>
</columns>
<rows>
<row id="1" name="bike" price="300" quantity="2"
date-produced="2013-08-12"/>
</rows>
</table>
This clearly isn’t a piece of furniture, but is probably a relational database table.
We see that the two “table” elements have different meaning and nothing in common – they happen to have the same name, but they belong to different vocabularies.
Here are some more examples of element names that can belong to different vocabularies:
<furniture:table/>
<sql:table/>
<resataurant:table/>
<html:table/>
<sql:column/>
<architecture:column/>
<table:column>
<css:style/>
<architecture:style/>
<fashion:style/>
Here we have three different element names: “table”, “column” and “style”, each belonging to > 1 vocabularies.
There are 8 different vocabularies represented by these elements:
furniture
, sql
, restaurant
and html
for the name table
sql
, architecture
and table
for the name column
css
, architecture
and fashion
for the name style
.The purpose of a namespace is to identify a vocabulary to which a given name belongs. In other words, to disambiguate two identical names that belong to different vocabularies.
Here is a typical namespace declaration:
<xsl:stylesheet version="1.0"
xmlns:xsl
="http://www.w3.org/1999/XSL/Transform"
>
It associates a namespace prefix to a namespace URI (Uniform Resource Identifier).
The effect of this namespace declaration is that the name: xsl:stylesheet
belongs to the namespace with namespace-uri the string “http://www.w3.org/1999/XSL/Transform”
Different namespace prefixes can be associated to the same namespace-uri -- even within the same XML document.
However, a namespace URI must be globally unique: the XSLT namespace is always the same string, regardless of what prefix is associated with it.
The prefix :
"xmlns:"
":"
xmlns
then the prefix is "" (the empty string)The NS URI:
"^%$23rt"
, "http://myComp.com"
, "my:my"
Do note:
Now, to answer the specific questions:
I do not understand this line of code in XSLT:
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
What is it there for?
This is the namespace declaration for the XSLT namespace that defines the XSLT vocabulary
Its purpose is to disambiguate any element and attribute names that are in this namespace from any other occurences of the same names, that belong to other vocabularies (or do not belong to any vocabulary (belong to the "empty" vocabulary).
What would happen if I miss this?
In case there is no XSLT namespace declaration specified in an XML document, then:
<xsl:stylesheet>
declarations, instructions, elements or attributes that it understands. It will not do any work and will likely report errors.And what is this pointing to?
As explained earlier, the namespace-uri can be any string and may not necessarily be a syntactically valid URL. In case when it is a syntactically valid URL, there is no obligation that there would be an HTTP server that would provide HTTP response for any HTTP request with this URL.
And the XSLT processor definitely doesn't issue any such HTTP request -- anyone can prove this by disconnecting from all networks and performing any XSLT transformation -- the transformation still produces its results.
But in this specific case, if you try this URL in your browser, you will see that the W3C has been friendly to its numerous users and a page is actually served:
I've read some Posts about this in W3Cschools... But I still didn't understood ...
W3Schools is not considered to be a good resource about XML/XSLT/XPath -- as explained at: W3Fools
The only authoritative source about XML, XML Namespaces and XSLT is their corresponding specification: http://www.w3.org/TR/2008/REC-xml-20081126/, http://www.w3.org/TR/2006/REC-xml-names-20060816/, and https://www.w3.org/TR/1999/REC-xslt-19991116 (the latter is the XSLT 1.0 spec -- separate specifications exist for XSLT 2.0 and for XSLT 3.0)
However, the W3C specifications were not written with the purpose to be user-friendly. If anyone wants to learn more about XSLT and XML, I would recommend my video course at Pluralsight: XSLT 2.0 and 1.0 Foundations -- the second module of this course explains in details the concept of XML namespaces.
Upvotes: 4
Reputation: 116959
Nothing happens if I delete this line
Well, that's just not true. If you remove the namespace declaration, your stylesheet will stop working. In fact, it will not be an XSLT stylesheet anymore.
The XSLT processor uses the namespace to distinguish between XSLT instructions (which the processor must execute) and other elements, such as literal result elements (which will be copied to the output tree).
Upvotes: 2