天天看点

javax.xml.namespace.QName

1.来历:qname是qualified name 的简写

2.构成:由名字空间(namespace),前缀(prefix)以及冒号(:),还有一个元素名称构成

3.举例:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

     xmlns="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"

     version="1.0">

<xsl:template match="foo">

    <hr/>

</xsl:template>

</xsl:stylesheet>

xsl是名字空间前缀,template是元素名称,xsl:template 就是一个qname

4.总结:qname无非是有着特定格式的xml元素,其作用主要是增加了名字空间,比如有同样的元素名称,而名字空间不同的情况。

自己测试:

// 指定名称空间 URI 和本地部分的 QName 构造方法。
//		QName qn=new javax.xml.namespace.QName("http://tempuri.org/", "resultXML");
		//指定名称空间 URI、本地部分和前缀的 QName 构造方法。
//		QName qn=new javax.xml.namespace.QName("http://tempuri.org/", "resultXML","/");
		//指定本地部分的 QName 构造方法。
		QName qn=new javax.xml.namespace.QName("resultXML");
		System.out.println("LocalPart:"+qn.getLocalPart());
		System.out.println("Prefix:"+qn.getPrefix());
		System.out.println("NameSapceUri:"+qn.getNamespaceURI());
           

仔细看下下面的东西:   我也是耐心了看了几遍才懂的。。。

4 Qualified Names

In XML documents conforming to this specification, some names (constructs corresponding to the nonterminalName)MUST be given as qualified names, defined as follows:

Qualified Name
[7]   

QName

   ::=   

PrefixedName

| UnprefixedName

[8]   

PrefixedName

   ::=   

Prefix ':' LocalPart

[9]   

UnprefixedName

   ::=   

LocalPart

[10]   

Prefix

   ::=   

NCName

[11]   

LocalPart

   ::=   

NCName

The Prefix provides thenamespace prefix part of the qualified name, andMUST be associated with a namespace URI reference in a namespace declaration. [Definition: TheLocalPart provides thelocal part of the qualified name.]

Note that the prefix functions only as a placeholder for a namespace name. ApplicationsSHOULD use the namespace name, not the prefix, in constructing names whose scope extends beyond the containing document.

5 Using Qualified Names

In XML documents conforming to this specification, element names are given asqualified names, as follows:

Element Names
[12]   

STag

   ::=   

'<' QName (SAttribute)*S? '>'

[NSC: Prefix Declared]
[13]   

ETag

   ::=   

'</' QName S? '>'

[NSC: Prefix Declared]
[14]   

EmptyElemTag

   ::=   

'<' QName (SAttribute)*S? '/>'

[NSC: Prefix Declared]

An example of a qualified name serving as an element name:

<!-- the 'price' element's namespace is http://ecommerce.example.org/schema -->
  <edi:price xmlns:edi='http://ecommerce.example.org/schema' units='Euro'>32.18</edi:price>
      

Attributes are either namespace declarations or their names are given asqualified names:

Attribute
[15]   

Attribute

   ::=   

NSAttNameEqAttValue

| QName Eq AttValue

[NSC: Prefix Declared]
[NSC: No Prefix Undeclaring]
[NSC: Attributes Unique]

An example of a qualified name serving as an attribute name:

<x xmlns:edi='http://ecommerce.example.org/schema'>
  <!-- the 'taxClass' attribute's namespace is http://ecommerce.example.org/schema -->
  <lineItem edi:taxClass="exempt">Baby food</lineItem>
</x>      

JDK   api文档里的跳转页面也有这些上面的资料    可以做去查下。。。。