2012-05-11 55 views
1

我需要以下SQL查詢的當量:Mysql的像子句等效使用XPath

選擇具有標題的所有電影以「V」,然後用3個字符,然後,隨後的字符串(例如範海辛的空間)

SELECT * FROM films WHERE title LIKE ="V__ %"; 

我怎樣才能做到這一點像電影/電影/標題,XPath 1.0中樹XPath 1.0表達式,因爲我將使用PHP和XSLT。

謝謝

+0

Zatla00:待辦事項,即目前公認的答案不正確地選擇不匹配選擇標準元素 - 請參閱我的答案的結尾。 –

回答

2

那是正確的,如果空格後的字符串無法呈現的?然後你可以使用:

//title[starts-with(text(), 'V') and string-length(substring-before(text(), ' '))=3] 

如果空格後的字符串的存在是很重要的,那麼你可以使用:

//title[starts-with(text(), 'V') and string-length(substring-before(text(), ' '))=3 and string-length(substring-after(text(), ' '))>0] 

http://www.w3.org/TR/xpath/

+0

感謝第二個表達式正是我正在尋找的。 – Zatla00

1

待辦事項,即目前公認的答案不正確地選擇非匹配選擇標準元素 - 請參閱此答案的結尾。

這是一個正確的解決方案

使用

/films/film/title 
    [starts-with(., 'V') 
    and 
    not(translate(substring(.,1,3), $vAlpha, ''))  
    and 
    substring(.,4,1) = ' '  
    and 
    not(substring(.,5,1) = ' ') 
    and 
    not(translate(., concat($vAlpha, ' '), ' '))  
    ] 

其中$vAlpha正是所有字母組成的字符串。

這將選擇的所有元素/films/film/title其字符串值

  1. 開始與字符V

  2. 它的起始三個字符都是字母。

  3. 它的第四個字符是空格。

  4. 它的第五個字符不是空格。

  5. 它的所有字符都是字母或空格。

XSLT - 基於驗證

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 
<xsl:variable name="vAlpha" select= 
    "concat('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
      'abcdefghijklmnopqrstuvwxyz')"/> 

<xsl:template match="/"> 
    <xsl:copy-of select= 
     "/films/film/title 
      [starts-with(., 'V') 
     and 
      not(translate(substring(.,1,3), $vAlpha, ''))  
     and 
      substring(.,4,1) = ' '  
      and 
      not(substring(.,5,1) = ' ') 
      and 
      not(translate(., concat($vAlpha, ' '), ' '))   
      ] 
     "/> 
</xsl:template> 
</xsl:stylesheet> 

當這種轉變是在下面的XML文檔應用:

<films> 
<film> 
    <title>Van Helsing</title> 
</film> 
<film> 
    <title>Van Helsing</title> 
</film> 
<film> 
    <title>Ban Helsing</title> 
</film> 
<film> 
    <title>van Helsing</title> 
</film> 
<film> 
    <title>Vann Helsing</title> 
</film> 
<film> 
    <title>Van Helsing2</title> 
</film> 
</films> 

以上的XPath表達式,並且所述選定的元素(在這種情況下只有一個)被複制到o本安輸出

<title>Van Helsing</title> 

待辦事項,當前所接受的答案不正確地選擇以下兩個不匹配的元素:

<title>Van Helsing</title> 
<title>Van Helsing2</title> 
+0

一如既往!但請注意,SELECT * FROM films WHERE標題LIKE =「V__%」;'**的xpath表達式**完全**我的第一個表達式:) –

+1

@AlehDouhi:是的,兩個答案都基於對實際內容的具體解釋通緝。兩者可能更接近或不能更接近OP的未表達要求。我希望這個答案提供了一些有用的技巧,即使我對OP的意圖的猜測並不完全是他未表達的情況。 –

+0

謝謝@DimitreNovatchev的回答,它給了我相同的結果,因爲參賽作品寫得很好,我的意思是電影名稱。 – Zatla00