Priyanka Shaju
Priyanka Shaju

Reputation: 799

What is mean by <beans:beans> and <beans>

What is the differences between

<beans xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns:p="http://www.springframework.org/schema/p"  
xmlns:context="http://www.springframework.org/schema/context"  
xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">  

and

<beans:beans xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns:p="http://www.springframework.org/schema/p"  
xmlns:context="http://www.springframework.org/schema/context"  
xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

Could you please explain what is the difference between these two.

Upvotes: 0

Views: 825

Answers (5)

madDerDog
madDerDog

Reputation: 23

Simply, it allows you NOT to add the namespace in front of the element you choose that belongs the default namespace. xmlns="namespace" From the above example if: i)xmlns="http://www.springframework.org/schema/beans"

Then you can use element <beans> as well as <beans:beans> - explicit syntax.

ii) xmlns="http://www.springframework.org/schema/p"

Then you can use <p> as well as <xmlns:p> - explicit syntax.

NOTE: You can only set the default namespace once using xmlns!

Upvotes: 1

zutnop
zutnop

Reputation: 611

The first one is correct and the second one isn't. The difference comes from your "default namespace". Your default namespace is beans. If your default namespace would be something else, like context for example, then you would need to explicitly define a beans namespace (which you haven't done in your example) and use the beans:beans style.

There is a different use case for nested <beans> element where you use a new <beans> tag inside your root <beans> tag:

for the purpose of defining a subset of beans with certain default values or to be registered only when certain profile(s) are active. Any such nested element must be declared as the last element in the document.

but your examples aren't related to this.

Upvotes: 1

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

The key to understand this is the xmlns value. In both of the cases it's:

xmlns="http://www.springframework.org/schema/beans"

This specifies the default schema for the document. This means that every tag in the XML will should be part of this schema, unless explicitly specify which other schema is it part of.

Specifying that a tag comes from another schema is usually done by <schemaName:tagName> pattern. So, for example, if the tag name is part of the schema foo, you have to use it with <foo:name>.

When picking a default schema for the document, you're allowed to skip the schema name, like in your example with <beans>. In this case, the <beans> tag must be specified in the schema, pointed with xmlns.

In the other case, you're explicitly specifying that the tag beans is part of the schema with alias beans, which result to <beans:beans>. However, if you don't have a xmlns:beans=http://www.springframework.org/schema/beans definition, you'd result with an invalid XML.

Personally, I'd prefer always explicitly provide the schema alias, though.

Upvotes: 1

Pelit Mamani
Pelit Mamani

Reputation: 2381

They are equivalent. You can say the full name of the element is ns : elementName e.g.:

http://www.springframework.org/schema/beans : beans

if you say xmlns="http://www.springframework.org/schema/beans" than it means "if you see an element without a namespace prefix, assume a default namespace of "http://www.springframework.org/schema/beans"

if you say xmlns:beans="http://www.springframework.org/schema/beans" than it means "if you see an element with a beans: prefix, assume it's shorthand for "http://www.springframework.org/schema/beans"

Upvotes: 1

Kayaman
Kayaman

Reputation: 73558

It's an XML namespace used to separate potentially conflicting element names, so there's no real difference.

XML Namespaces

Upvotes: 1

Related Questions