2011-06-21 61 views
13

您能否幫我理解xml屬性中「本地名稱」和「限定名稱」之間的區別? 從http://developer.android.com/reference/org/xml/sax/Attributes.htmlxml屬性中「本地名稱」和「限定名稱」之間的區別

/** 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) 

在這個例子中,

<anelement attr1="test" attr2="test2"> </anelement> 

會有什麼區別?

回答

13

限定名稱包括名稱空間前綴和本地名稱:att1foo:att2

示例XML

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

Java代碼:

ATT1

屬性沒有命名空間前綴不拿起默認的命名空間。這意味着雖然root元素的名稱空間爲"http://www.example.com/DEFAULT",但att1屬性的名稱空間爲""

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" 
+0

Skeet的&你的答案包括完美的答案。 –

7

本地名稱是不受名稱空間限定的名稱。完全合格的包括命名空間,如果有的話。

這可能是值得閱讀W3C recommendation on XML names獲得完整的細節。

基本上如果你沒有xmlns你的XML文件中的任何地方,你可能不需要擔心命名空間。如果您有命名空間,你可能會想建立一個完全合格的名稱,元素名稱等

注意這些屬性一般不太可能使用的命名空間不是元素,在我的經驗,當檢查。

+0

明確的答案,但尚未沒有簡單的例子。 –

相關問題