Jump to content

Understanding how namespaces work


RikP

Recommended Posts

Hello, I am having difficulty understanding how namespaces work.  To my understanding, the namespace URI is added to an xml document with a qualifier so you can contextualize the content (i.e. differentiate two complex elements with the name "table" that may have different elements and attributes).  I understand that the URI is not looked up, but rather to give the namespace a global unique identifier.  This is where my confusion begins.  Take for instance the following xml:

<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
        <tr>
          <td><xsl:value-of select="title"/></td>
          <td><xsl:value-of select="artist"/></td>
        </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

</xs:schema>

to my understanding, the elements or attributes prefixed with xsl (xsl:tempalte, xsl:for-each, etc.), the XML parser recognizes to be part of the "http://www.w3.org/1999/XSL/Transform" namespace.  My question is, if the URI is not used to lookup content, how does it check that the elements provided are all (valid) in the xsl prefixed namespace, or does it?  What I do not understand is the following from the XSL Transformation Page, it states the following 

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Quote

To get access to the XSLT elements, attributes and features we must declare the XSLT namespace at the top of the document.

How do we have "access" to elements, attributes, and features if no lookup is done?

For instance, if I added the following element to the xml file:

<xsl:foo>Made up element</xsl:foo>

what happens? I know that foo is not a defined element inside of the xsl namespace, but how does a XML parser know that? 

 

How does the parser know what elements should/could be inside the prefixed namespace?  

 

Thanks in advance!

Link to comment
Share on other sites

If the software that is reading the XML document finds an element that it recognizes from a namespace that it is built to support,  then it will use the data from the element in the way that was specified in the standard associated with that namespace.

If you give it an element that it does not recognize, then the behaviour depends on who built the software. The programmer may have chosen to ignore it or they might have decided to show an error message.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...