What is the difference between localname and qname?

核能气质少年 提交于 2019-11-27 12:05:25

问题


When using SAX to parse an XML file in Java, what is the difference between the parameters localname and qname in SAX methods such as startElement(String uri, String localName,String qName, Attributes attributes)?


回答1:


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"



回答2:


Generally speaking, localname is the local name, meaning inside the namespace. qname, or qualified name, is the full name (including namespace). For example, <a:b …> will have a localname b, but a qname a:b.

This is however very general, and settings-dependant. Take a look at the example at the end of this page for a more thorough example: example




回答3:


By default, an XML reader will report a Namespace URI and a localName for every element that belongs in a namespace, in both the start and end handler.

Consider the following example:

  <html:hr xmlns:html="http://www.w3.org/1999/xhtml"/>

With the default SAX2 Namespace processing, the XML reader would report a start and end element event with the Namespace URI http://www.w3.org/1999/xhtml and the localName hr. Most XMLReader implementations also report the original qName html:hr, but that parameter might simply be an empty string (except for elements that aren't in a namespace).

http://www.saxproject.org/namespaces.html



来源:https://stackoverflow.com/questions/7157355/what-is-the-difference-between-localname-and-qname

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!