开发者

Differences between a 'local name' and 'qualified name' in a xml attribute

Can you please help me understand what is the difference between 'local name' and 'qualified name' in a xml attribute? From http://developer.android.com/reference/org/xml/sax/Attributes.html:

/** Look up an attribute's local name by index. */
abstract String getLocalName(int index)

/** Look up an attribute's XML qualified (prefixed) name by index. */    
abstract String getQName(int index)

In this example,

<anelement attr1="test" attr2="test2"> </anelement>
开发者_Go百科

What will be the difference?


The qualified name includes both the namespace prefix and the local name: att1 and foo:att2.

Sample XML

<root 
    xmlns="http://www.example.com/DEFAULT" 
    att1="Hello" 
    xmlns:foo="http://www.example.com/FOO" 
    foo:att2="World"/>

Java Code:

att1

Attributes without a namespace prefix do not pick up the default namespace. This means while the namespace for the root element is "http://www.example.com/DEFAULT", the namespace for the att1 attribute is "".

int att1Index = attributes.getIndex("", "att1");
attributes.getLocalName(att1Index);  // returns "att1"
attributes.getQName(att1Index);  // returns "att1"
attributes.getURI(att1Index);  // returns ""

att2

int att2Index = attributes.getIndex("http://www.example.com/FOO", "att2");
attributes.getLocalName(att2Index);  // returns "att2"
attributes.getQName(att2Index);  // returns "foo:att2"
attributes.getURI(att2Index);  // returns "http://www.example.com/FOO"


A local name is one which isn't qualified by a namespace. The fully qualified one includes the namespace, if any.

It's probably worth reading the W3C recommendation on XML names to get the full details.

Basically if you don't have xmlns anywhere in your XML file, you probably won't need to worry about namespaces. If you do have namespaces, you'll probably want to create a fully qualified name when checking for element names etc.

Note that attributes are generally less likely to use namespaces than elements, in my experience.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜