XPath 1.0node
XPath Containment程序員
Distributed Query Evaluation正則表達式
RE and DFA算法
-- 在XML中的使用shell
XPath 語法: http://www.w3school.com.cn/xpath/xpath_syntax.aspexpress
XPath (紅色字體) 示例:編程
/bib/book/year Result: <year> 1995 </year>
<year> 1998 </year>
/bib/paper/year Result: empty
XPath: Restricted Kleene Closure 暴力查找node的感受網絡
//author Result: <author> Serge Abiteboul </author>
<author> <first-name> Rick </first-name>
<last-name> Hull </last-name>
</author>
<author> Victor Vianu </author>
<author> Jeffrey D. Ullman </author>
/bib//first-name Result: <first-name> Rick </first-name>
XPath: Text Nodes 提取 (無子結構的) elemapp
/bib/book/author/text()
Result: Serge Abiteboul
Victor Vianu
Jeffrey D. Ullman
XPath: Text Nodes 提取 (有子結構的) elemless
//author/* Result: <first-name> Rick </first-name>
<last-name> Hull </last-name>
XPath: Attribute Nodes 提取屬性的內容
/bib/book/@price
Result: 「55」
XPath: Qualifiers 只要有firstname子節點的元素
/bib/book/author[firstname] Result: <author> <first-name> Rick </first-name>
<last-name> Hull </last-name>
</author>
XPath: Qualifiers 只要條件爲:有firstname子節點和address子節點,且address中下的某一level中有zip節點,以及下一level就得有city。知足如此條件的author下的lastname的內容。
/bib/book/author[firstname][address[//zip][city]]/lastname Result: <lastname> ... </lastname>
<lastname> ... </lastname>
XPath: 子節點的屬性知足指定條件
/bib/book[@price < 「60」] /bib/book[author/@age < 「25」]
XPath: author下的elem只容許有txt。
/bib/book[author/text()]
XPath 軸: 軸可定義相對於當前節點的節點集
軸可定義相對於當前節點的節點集。
軸名稱 | 結果 |
---|---|
ancestor | 選取當前節點的全部先輩(父、祖父等)。 |
ancestor-or-self | 選取當前節點的全部先輩(父、祖父等)以及當前節點自己。 |
attribute | 選取當前節點的全部屬性。 |
child | 選取當前節點的全部子元素。 |
descendant | 選取當前節點的全部後代元素(子、孫等)。 |
descendant-or-self | 選取當前節點的全部後代元素(子、孫等)以及當前節點自己。 |
following | 選取文檔中當前節點的結束標籤以後的全部節點。 |
namespace | 選取當前節點的全部命名空間節點。 |
parent | 選取當前節點的父節點。 |
preceding | 選取文檔中當前節點的開始標籤以前的全部節點。 |
preceding-sibling | 選取當前節點以前的全部同級節點。 |
self | 選取當前節點。 |
Return Node: [[name]] 使用double circles表示。
任意深度子目錄存在:雙斜線。
表達式解釋:
root下的任意未知結點person,知足孩子中有name,任意子孫中需有屬性zip = "12345"的結點。
知足以上條件,則返回這個person的phone。
Equivalence, Containment
返回的結果老是一致的,咱們認爲這是一個query。
這個比較複雜,因此咱們引入一種方法來解決:
Practical Algorithm for Linear XPath *,//
關鍵在於找到mapping。
第一個例子:
第二個例子:
第三個例子:
爲什麼不等價?
應該是前者包含後者!
注意1:/*// 轉化爲雙線。
注意2://name 這裏沒有*,因此是>=0,這致使了二者不等價!
Branching XPath *,//
-- 難度加大,計算量大
視狀況而定,此處略。
a.b*.c // any num of b, here we regard it as a Query.
a.b+.c // at least one num of b
Review 正則表達式 與 DFA 的關係
在分佈式環境下的Query,以下:(如用在下圖中查詢這個Query)
A naïve approach takes too many communication steps
=> we have to do more work locally
A better approach needs to
1. identify all external references
2. identify targets of external references
Algorithm:
1. Given a query, we compute its automaton 計算出某一個query的對應的一個東西
2. Send it to each site 將這個東西分發給各個site
3. Start an identical process at each site 各個site分別處理這個東西
4. Compute two sets Stop(n, s) and Result(n, s) 計算出這兩個結果
5. Transmits the relations to a central location and get their union 合併全部結果
Detail:
每一個site都會有本身的一個《進出表》,以下:
Site 1:
In | x1, x4 |
Out | y1, y3 |
Site 2:
In | y1, y3 |
Out | z2 |
Site 3:
In | z2 |
Out | x4 |
相對於Query: a.b+.c
以 Site 2 爲例,該site將處理query並獲得以下兩個table:
《搞不定表》- Stop Table
Start | End |
(y1, s2) | (z2, s2) |
(y3, s2) | (z2, s2) |
這裏的z2怎麼得來?Site 2雖然不曉得其餘site的信息,但起碼知道
與本身有直接鏈接的node信息。好比:
與y3直接相鏈接的z2有可能做爲s2。
《能搞定表》- Result Table
Start | End |
(y1, s2) | (y3, s3) |
(y1, s3) | (y1, s3) |
(y3, s3) | (y3, s3) |
而後,每一個site都應該返回了針對該query的倆表。
接下來就是如何合併這麼些個表的問題。
讓咱們將整個表都列出來,作一次傻瓜式的demo。
Union:
Start | Stop | Start | Result | ||
(x1, s1) | (y1, s2) | stop1 | (x1, s3) | x1 | result1 |
(x4, s2) | (y3, s3) | stop2 | (x4, s2) | x3 | result2 |
(y1, s2) | (z2, s2) | stop3 | (x4, s3) | x4 | result3 |
(y3, s2) | (z2, s2) | stop4 | (y1, s2) | y3 | result4 |
(z2, s2) | (x4, s2) | stop5 | (y1, s3) | y1 | result5 |
(y3, s3) | y3 | result6 | |||
(z2, s1) | z3 | result7 | |||
(z2, s2) | z2 | result8 | |||
(z2, s3) | z2 | result9 |
解說:
root | ||
stop1 | 在其餘site能直接終止麼? | |
result4 | 還真有,能夠直接結束! | 匹配到了(y1,s2) |
stop3 | 不結束,繞一繞,其餘site或許能給出新的sol | 匹配到了(y1,s2) |
result8 | 確實有,能夠直接結束! | 匹配到了(z2,s2) |
stop5 | 不結束,繞一繞,其餘site或許能給出新的sol | 匹配到了(z2,s2) |
result2 | 還真有,能夠直接結束! | 匹配到了(x4,s2) |
stop2 | 不結束,繞一繞,其餘site或許能給出新的sol | 匹配到了(x4,s2) |
result6 | 有的啦,能夠直接結束! | |
n/a | 不結束,繞一繞,其餘site或許能給出新的sol | 匹配不到了! |
Answer:
{y3, z2, x3}
定義:
This definition may seem circular, but 1-3 form the basis
Precedence: Parentheses have the highest precedence,
followed by *, concatenation, and then union.
RE Examples (表達形式 = 實際的集合形式)
-- 當search時但願return results in this kind of pattern.
• L(001) = {001}
• L(0+10*) = { 0, 1, 10, 100, 1000, 10000, … } // union: or; * any number of 0
• L(0*10*) = {1, 01, 10, 010, 0010, …} i.e. {w | w has exactly a single 1}
• L( |- |- )* = {w | w is a string of even length}
• L((0(0+1))*) = { ε, 00, 01, 0000, 0001, 0100, 0101, …}
• L((0+ε)(1+ ε)) = {ε, 0, 1, 01}
• L(1Ø) = Ø ; concatenating the empty set to any set yields the empty set.
• Rε = R // ε就像1
• R+Ø = R // 空集就像0
Exercise 1
∑ = {10, 11}, ∑* = {є, 10, 11, 1010, 1011, 1110, 1111, …}
表示集合裏元素的數量各自都是任意的,而後組合在一塊兒。
Exercise 2
L1 = {10, 1}, L2 = {011, 11}, L1L2 = {10011, 1011, 111}
哄小孩的題。
Exercise 3
Write RE for
– All strings of 0’s and 1’s (其實就是任意0與1的組合)
• (0|1)*
– All strings of 0’s and 1’s with at least 2 consecutive 0’s (須要至少有連續的0)
• (0|1)*00(0|1)*
– All strings of 0’s and 1’s beginning with 1 and not having two consecutive 0’s (不能有連續的0)
• (1+10)* // {1, 10} 其中的元素任意組合確實不會有連續的0出現,技巧哉!
More exercises
1) (0|1)*011
• Answer: all strings of 0’s and 1’s ending in 011
2) 0*1*2*
• Answer: any number of 0’s followed by any number of 1’s followed by any number of 2’s
3) 00*11*22*
• Answer: strings in 0*1*2 with at least one of each symbo
Link: 百度百科
起源:
引擎
這裏只簡單瞧瞧 DFA (Deterministic Finite Automata),"肯定有限狀態自動機"。
b*a*b*a*b* 有循環,可見是個環。
Duality: RE與DFA的等價性
侷限性:
Which languages CANNOT be described by any RE?
• Set of all bit strings with equal number of 0s and 1s. // 沒有計數相關功能
• Set of all decimal strings that represent prime numbers. // 又不是人工智能,:p
ab*a
可見,b有環。
a(a|b)*a
可見,(a|b)有環。思考環的構造,以下:
DFA to RE: State Elimination
• Eliminates states of the automaton and replaces the edges with regular expressions that includes the behavior of the eliminated states.
• Eventually we get down to the situation with just a start and final node, and this is easy to express as a RE
We can describe this automaton as: (R | SU*T)*SU*
分析:
對於第一個node來講,有兩條路徑:
而後考慮end node:也是兩條路徑:
因而便構成了: (R | SU*T)*SU*
We can describe this automaton as simply R*
標準的elimination過程
Example One:
1) First convert the edges to RE’s.
注意:簡化的過程當中不要丟失信息。
2) 有個環,精簡它。
3) 3-->1-->2的過程不能丟失,因此保留"11"。
Answer: (0+10)*11(0+1)*
Example Two:
1) First convert the edges to RE’s.
注意:簡化的過程當中不要丟失信息。
2) 有個環,精簡它。
3) 1-->2-->3的過程不能丟失,因此保留"10*1"。
注意:這裏有兩個end node。
策略:關掉node 1,計算;再關掉node 2,計算。
關掉node 3:
0*
關掉node 1:
0*10*1(0|10*1)*
合併結果(或操做):
0* | 0*10*1(0|10*1)*