#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,660
    Rep Power
    171

    About xml namespaces


    I understand that "namespaces" are a way of grouping elements and attributes under a common heading in order to differentiate them from a similarily named items.

    I see this on top of documents quite often use the famouse w3.org url as the namespace:
    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    This namespace is a web page. It does not contain any rules or definitions of the namespace So does this mean a namespace could be "anything" as long as it's a unique string? It does not have to include any rules does it? The reason I ask is that I read:
    Namespace identifier is a string of characters such as isbn, which identifies how the namespace specific string should be interpreted
    How can it specify how a string should be interpreted if it is doesn't contain anything?
    Thanks
    Last edited by zxcvbnm; March 11th, 2013 at 12:33 AM.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,056
    Rep Power
    9398
    Yes. Namespaces should be URIs (I've seen things that complain if they're not) but they don't have to be to some computer-readable specification. Human-readable is a good idea though.

    As for your second question, it's too confusing, I can't tell what you're asking.
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,660
    Rep Power
    171
    Originally Posted by requinix
    Yes. Namespaces should be URIs (I've seen things that complain if they're not) but they don't have to be to some computer-readable specification. Human-readable is a good idea though.

    As for your second question, it's too confusing, I can't tell what you're asking.
    Ok let's say it is a human readable string. How can a string contain rules or definitions? In other words, I can open a DTD and see all the rules and specifications but I don't understand how a string can contain rules!

    I hope it's clear what I am asking
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,056
    Rep Power
    9398
    It doesn't have to, that's what I'm saying. Neither the URI nor the document it's pointing to have to include rules or definitions or schema. It's more or less an arbitrary string. It is, however, a Good Thing To Do because then other people reading the XML can easily find out the schema without having to Google stuff or hunt down your documentation.
  8. #5
  9. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,660
    Rep Power
    171
    Originally Posted by requinix
    It doesn't have to, that's what I'm saying. Neither the URI nor the document it's pointing to have to include rules or definitions or schema. It's more or less an arbitrary string. It is, however, a Good Thing To Do because then other people reading the XML can easily find out the schema without having to Google stuff or hunt down your documentation.
    I don't know if I am slow or these things are confusing for everyone!
  10. #6
  11. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,660
    Rep Power
    171
    Requinix;

    I am not gonna open a bew thread but I am going to ask a question here

    1 - What is the difference between these 2 XMLs?

    2 - What happens when I want applicationUsers element to use both namespaces?

    3 - Why isnt there a prefix for the default name space?

    Thank you
    Code:
    <hr:applicationUsers xmlns="http://www.webmoosh.com/namespaces/hr/entities"
    	xmlns:hr="http://www.webmoosh.com/namespaces/hr/config">
    	<user  hr:firstname="Behnam" />
    	<user hr:firstname="John" />
    </hr:applicationUsers>
    And
    Code:
    <applicationUsers xmlns="http://www.webmoosh.com/namespaces/hr/entities"
    	xmlns:hr="http://www.webmoosh.com/namespaces/hr/config">
    	<user  hr:firstname="Behnam" />
    	<user hr:firstname="John" />
    </applicationUsers>
    Last edited by zxcvbnm; March 11th, 2013 at 04:28 AM.
  12. #7
  13. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,056
    Rep Power
    9398
    1. In the first the root node is in a different namespace than the <user>s below it. (In the second they're in the same one.)
    2. You can't. A node or attribute can only belong in one namespace, though a node and its attributes don't all have to be in the same one (like with the <user>s).
    3. Because it's easier that way?
  14. #8
  15. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,660
    Rep Power
    171
    Thanks Requinix;

    Is this valid? The reason I ask is that:

    1 - There is no default name space. Both name spaces have prefix! Is it ok not to have a default name space?

    2 - ApplicationUsers element does not have any namespace. Is it ok?

    3 - Elements and attributes each have a seperate namespace assigned to them. For example the dial attributes under phone element has a different name space than phone element, still valid?

    Code:
    <applicationUsers xmlns:tr="http://www.webmoosh.com/namespaces/hr/entities"
    	xmlns:hr="http://www.webmoosh.com/namespaces/hr/config">
    	<user  hr:firstname="Behnam" />
    	<address unit="5" />
            <tr:phone hr:dial="5544098" />
    </applicationUsers>
  16. #9
  17. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,056
    Rep Power
    9398
    1. The default namespace always exists; if you don't define it yourself you get the default xmlns="". In your example <applicationUsers>, <user>, and <address> are in the default namespace.
    2. Yes (see above) but I don't think that's what you want.
    3. Yes.
  18. #10
  19. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,660
    Rep Power
    171
    Requinx:
    This is an over simplified xml:
    Code:
    <?xml version="1.0"?>
    <note
    xmlns="http://www.w3schools.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.w3schools.com note.xsd">
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>Don't forget me this weekend!</body>
    </note>
    And this is the Schema:
    Code:
    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.w3schools.com"
    xmlns="http://www.w3schools.com"
    elementFormDefault="qualified">
    
    <xs:element name="note">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="to" type="xs:string"/>
          <xs:element name="from" type="xs:string"/>
          <xs:element name="heading" type="xs:string"/>
          <xs:element name="body" type="xs:string"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    
    </xs:schema>
    1 - Why does the schema need name spaces? It makes sense that the xml simply looks at the schema without need to the namespaces in the schema. Would you please clarify why there is a need for name spaces in the schema itself?

    2 - The <note> element could be anything right? For example <root>?

    3 - Why defining xsi name space when it is not used in the xml at all?

    4 - This schema is NOT a URI, right? Cause URI is a unique string which could not even exist. This schem (which is a namespace) is more than just a string. Clarify please.

    Thanks
    Last edited by zxcvbnm; March 13th, 2013 at 05:24 PM.
  20. #11
  21. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,056
    Rep Power
    9398
    1. The schema needs the namespace corresponding to XML Schema itself, otherwise it's just plain XML.

    2. Depends. In this case no because the associated schema only defines a "note" element. It also depends on what's reading the XML back and the purposes of the XML. Logically speaking the root could have been named anything but technically speaking the writers chose that name so that's what you have to use.

    3. It does use it: the schemaLocation.

    4. You mean the namespace? It certainly is a URI. The schemaLocation? That maps a namespace URI to an actual document (XML Schema) that can be used to validate the XML. So if a parser wanted to validate the XML it would load the "note.xsd" file and use it to validate stuff under the "http://www.w3schools.com" namespace.

IMN logo majestic logo threadwatch logo seochat tools logo