XPath XSLT 和命名空間

2021-06-17 16:13:53 字數 4061 閱讀 8997

w3c xml 路徑語言(也稱為 xpath)用於對 xml 文件的某些部分進行定址,它可用在許多 w3c xml 技術(包括 xslt、xpointer、xml 架構和 dom level 3)中。xpath 使用類似於檔案系統和 url 中使用的分層定址機制來檢索 xml 文件的某些部分。xpath 支援對字串、數字和布林值進行基本操作。

xpath 和命名空間

xpath 資料模型將 xml 文件視為節點(如元素、屬性和文字節點)樹。在節點樹中,每個節點的名稱都由其本地名稱和命名空間名稱(即,它的通用名稱或擴充套件名稱)組合而成。

對於沒有命名空間的元素和屬性節點,執行 xpath 查詢是相當簡單的。下面的程式可用於從命令列查詢 xml 文件,並將用來闡釋命名空間對 xpath 查詢的影響。

假設下面的 xml 文件不宣告任何命名空間,則查詢相當簡單,如以下**後面的示例所示。

<?xml version="1.0" encoding="utf-8" ?> 

the autobiography of benjamin franklin

benjamin

franklin

8.99

the confidence man

herman

melville

11.99

示例1

示例2

示例3

有關使用 xpath 的詳細資訊,請閱讀 aaron skonnard 的文章 addressing infosets with xpath 並檢視 zvon.org xpath tutorial 上的示例。

xslt 和命名空間

w3c xsl 轉換 (xslt) 建議 描 述一種基於 xml 的語言,用於將 xml 文件轉換為其他 xml 文件。xslt 轉換又稱為 xml 樣式表,它使用模式 (xpath) 來與目標文件的各個方面進行匹配。在與目標文件中的節點進行匹配時,可以對那些指定成功匹配輸出結果的模板進行例項化,並使用這些模板來轉換文件。

對命名空間的支援已緊密整合到 xslt 中,特別是由於 xpath 用於與源文件中的節點進行匹配。在 xslt 內部的 xpath 表示式中使用命名空間比使用 dom 方便得多。

隨後的示例包含:

程式

imports system.xml.xsl

imports system.xml

imports system

imports system.io

class transformer

public shared function printerror(e as exception, errstr as string) as string

if e is nothing then

return errstr

else

return printerror(e.innerexception, errstr + e.message)

end if

end function 'printerror

'entry point which delegates to c-style main private function

public overloads shared sub main()

run(system.environment.getcommandlineargs())

end sub 'main

overloads public shared sub run(args() as string)

if args.length <> 2 then

console.writeline("usage: xslt source stylesheet")

return

end if

try'create the xsltransform object.

dim xslt as new xsltransform()

'load the stylesheet.

xslt.load(args(1))

'transform the file.

dim doc as new xmldocument()

doc.load(args(0))

xslt.transform(doc, nothing, console.out)

catch xmle as xmlexception

console.writeline(("error: xml parse error occured because " +

printerror(xmle, nothing)))

catch fnfe as filenotfoundexception

console.writeline(("error: " + printerror(fnfe, nothing)))

catch xslte as xsltexception

console.writeline(("error: the following error occured while

transforming the document: " + printerror(xslte, nothing)))

catch e as exception

console.writeline(("unexpected error" + printerror(e, nothing)))

end try

end sub

end class 'transformer

xslt樣式表

輸出

<?xml version="1.0" ?>

the autobiography of

benjamin franklin

the confidence

man

請注意,該樣式表中的命名空間宣告結束於輸出 xml 文件的根節點上。另請注意,xslt 命名空間未包括在輸出 xml 文件中。

從 xslt 轉換的輸出結果生成 xslt 樣式表有些麻煩,這是由於處理器必須能夠從實際的樣式表指令確定輸出元素。我發現可以通過兩種方法來解決此問題,我將通過顯示一些樣式表來闡釋這兩種方法,這些樣式表生成下面的 xmlt 樣式表作為輸出結果。

hello world

第一種方法涉及到建立乙個包含要建立的樣式表的變數,然後結合使用

value-of

disable-output-escaping
屬性來建立該樣式表。

hello

world

如果所建立的樣式表可以方便地進行分割槽,以便放在變數中,則第一種方法非常有效。儘管此方法快速方便,但是它還會歸入雜項 類別,這在面臨需要靈活性的任何情況時往往會變得難以處理。例如,如果在建立新樣式表時涉及到建立許多動態文字並且與樣式表指令交叉在一起,則相對於上面 提到的雜項,以下方法會更適合。

hello

world

上面的文件使用

namespace-alias
指令,將

alias
字首及其所繫結到的命名空間名稱替換為

xslt
字首及其所繫結到的命名空間名稱。

命名空間還用於指定擴充套件 xslt 的機制。可以建立執行方式與 xslt 函式相同的、帶有命名空間字首的函式。同樣,某些命名空間中的元素可被視為對 xslt 的擴充套件,並且可以像執行轉換指令(如

template
copy
value-of
等)那樣執行。下面是 hello world 程式的示例,該程式使用基於命名空間的擴充套件函式來列印簽名問候語。

function sayhello()

XAML 命名空間和命名空間對映

本主題將介紹大部分 xaml 檔案的根元素中存在的 xml xaml 命名空間 xmlns 對映。它還將介紹如何為自定義型別和程式集生成類似的對映。無論是其一般用途還是 windows 執行時應用程式設計的應用上,xaml 都用於宣告物件 這些物件的屬性以及表示為層次結構的物件 屬性關係。你在 xa...

Spring c命名空間和p命名空間注入

c命名空間和p命名空間不能直接使用,需要在bean的xml檔案中匯入xml約束。實體類 user package com.harris.pojo public class user public user public string getname public void setname strin...

C 命名和命名空間(Namespace)

常用的命名方法有兩種 一種是pascal命名法 帕斯卡命名法 另一種camel命名法 駝峰命名法 pascal命名法是指個單詞的首字母大寫 camel 命名法是指第乙個單詞小寫,從第二個單詞開始每個單詞的首字母大寫。變數的命名規則 變數的命名規則遵循 camel 命名法,並盡量使用能描述變數作用的英...