天天看點

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文檔裡的跳轉頁面也有這些上面的資料    可以做去查下。。。。