Jump to content

Clarification on "xmlns" attribute


pavankumard

Recommended Posts

Hi,From W3 Schools, I got the following information.----------------------------------------------------------------------------------------------------------------------------------------------------xmlns:xs="http://www.w3.org/2001/XMLSchema" indicates that the elements and data types used in the schema come from the "http://www.w3.org/2001/XMLSchema" namespace. It also specifies that the elements and data types that come from the "http://www.w3.org/2001/XMLSchema" namespace should be prefixed with xs:----------------------------------------------------------------------------------------------------------------------------------------------------My doubt is, will the parser really refer to the above namespace or is it just a standard?If it really refers to the above namespace how will it do that. If not why should we have this URL as a standard?Pavan.

Link to comment
Share on other sites

The XML namespaces serve as identifiers, not as actual locations the parser looks into. That is, the namespace URI (note: URI includes URL and URN) specifies that the element is not something you've defined, but rather something that was previously defined and may have special meaning. If the XML parser supports it, it could apply it's instructions. For example, the XSLT namespace (http://www.w3.org/1999/XSL/Transform) when read by an XSLT processor will instruct it that the elements with that prefix are the actual XSLT elements and thus, the processor will do the transformation using those elements as instructors. Elements with other namespaces will be left alone in the output.

Link to comment
Share on other sites

But when I try giving some junk URI as namespace and use that namespace to qualify the existing instructions, it works as usual !! This brings me ambiguity in understanding the innerworking. The following example works!!<xsl:stylesheet xmlns:xsl="http://junk.com"> <xsl:if test="...."> <SomeTag> <xsl:value-of select=".."> </SomeTag> </xsl:if><xsl:stylesheet>I didnt understand, why we are supposed to supply "http://www.w3.org/1999/XSL/Transform" and not some junk value.Thanks in advance.Pavan Kumar D.

Link to comment
Share on other sites

Depends on what you mean by "works". It shows XML tree (after adding a slash to the closing stylesheet element), but that doesn't mean it works as a stylesheet.What you see when you open this file is pure XML tree. The XML parser is not instructed to look for the XSLT namespace and apply it's meaning.Try to create some XML, and add this on top of it (right after the XML prolog):

<?xml-stylesheet type="text/xsl" href="test.xsl"?>

Then create the file test.xsl in the same folder. The contents of test.xsl should be the file you gave as an example. Opening the XML file, you should see an error, because the XML parser failed to find the XSLT namespace and apply it. Btw, even if you do use the right namespace, you'll still get an error, as what you have is not valid XSLT stylesheet. Add xsl:template element around everything (except xsl:stylesheet).If you DO see a result instead of an error message when opening the XML, you're dealing with a very buggy XML parser.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...