2011-11-12 156 views
1

特定節點我有一個XML文檔是這樣的:獲取從XML使用XPath

<food> 
<fruit> 
    <apple> 
     <product> 
      <title>Apple 1</title> 
      <price>7</price> 
     </product> 

     <product> 
      <title>Apple 2</title> 
      <price>4</price> 
     </product> 
    </apple> 

    <grapes> 
     <product> 
      <title>Red grapes </title> 
      <price>4</price> 
     </product> 
     <product> 
      <title>Green grapes</title> 
      <price>6</price> 
     </product> 
    </grapes> 
</fruit> 
<drink> 
    <water> 
     <product> 
      <title>Water 1</title> 
      <price>1</price> 
     </product> 
     <product> 
      <title>Water 2</title> 
      <price>6</price> 
     </product> 
    </water> 
    <soda> 
     <product> 
      <title>Coca-Cola</title> 
      <price>10</price> 
     </product> 
     <product> 
      <title>Sprite</title> 
      <price>4</price> 
     </product> 
    </soda> 
</drink> 
</food> 

我有一個這樣的XML結構。

我想購買成本高於5的所有產品的產品名稱和價格。如何編寫這樣做的XPath表達式?

+0

錯失。 當然,它是在XML結構 – IVar

回答

3

我想你的文檔以</food>結尾。那是你要的嗎 ?

//product[price > 5] 

編輯:

如果你不想<product>標籤,你可以這樣做:

//product[price > 5]/* 

不過,我覺得這是最後的解決辦法不太方便,因爲我們不劃定產品更多。

+0

你的例子的作品,它給了我一個結果。但如果我想展示一切,我該怎麼辦?我是否需要循環使用for Each或者可以在沒有循環的情況下完成? – IVar

+0

那麼,我不喜歡XSLT(我使用xquery)。我想你需要循環使用for-each(例如:http://www.w3schools.com/xsl/)。 – fflorent

+0

此答案中的XPath表達式給出* all *結果,而不僅僅是一個。但是,您訪問XPath之外的結果的方式將提取單個字符串值或節點集或其他內容。你原來的問題只是關於XPath的問題,但現在你需要告訴我們你使用XPath的環境(是否在XSLT中?) – LarsH

2

使用

/*/*/*/product[price > 5]/title 
| 
    /*/*/*/product/price[. > 5] 

這將選擇的工會:

  1. 有一個product父時爲數字處理,其price孩子的字符串值大於5的所有title元素這是XML文檔頂層元素的重孫。

  2. 所有price元件,其字符串值當作爲處理過的數大於5,且父是product在XML文檔的頂部元件的曾孫。在按文檔順序的節點集(典型地),這意味着一個title及其相應price在任何節點的集合是由相應的XPath API產生是彼此相鄰設置

選擇的元素。

在XSLT人會用一個很簡單的改造是這樣的:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="product[price > 5]"> 
    Name: <xsl:value-of select="title"/> 
    <xsl:text>, price </xsl:text> 
    <xsl:value-of select="price"/> 
</xsl:template> 
<xsl:template match="text()"/> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<food> 
<fruit> 
    <apple> 
     <product> 
      <title>Apple 1</title> 
      <price>7</price> 
     </product> 

     <product> 
      <title>Apple 2</title> 
      <price>4</price> 
     </product> 
    </apple> 

    <grapes> 
     <product> 
      <title>Red grapes </title> 
      <price>4</price> 
     </product> 
     <product> 
      <title>Green grapes</title> 
      <price>6</price> 
     </product> 
    </grapes> 
</fruit> 
<drink> 
    <water> 
     <product> 
      <title>Water 1</title> 
      <price>1</price> 
     </product> 
     <product> 
      <title>Water 2</title> 
      <price>6</price> 
     </product> 
    </water> 
    <soda> 
     <product> 
      <title>Coca-Cola</title> 
      <price>10</price> 
     </product> 
     <product> 
      <title>Sprite</title> 
      <price>4</price> 
     </product> 
    </soda> 
</drink> 
</food> 

想要的,正確的結果產生

Name: Apple 1, price 7 
    Name: Green grapes, price 6 
    Name: Water 2, price 6 
    Name: Coca-Cola, price 10 

將價格限制指定爲外部傳遞給變換的全局參數甚至更好。

在這種情況下,一個XSLT 2。0變換是簡單一點:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text"/> 
<xsl:strip-space elements="*"/> 

<xsl:param name="pLimit" select="5"/> 

<xsl:template match="product[price > $pLimit]"> 
    Name: <xsl:value-of select="title"/> 
    <xsl:text>, price </xsl:text> 
    <xsl:value-of select="price"/> 
</xsl:template> 
<xsl:template match="text()"/> 
</xsl:stylesheet> 

甲相應XSLT 1.0變換是

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text"/> 
<xsl:strip-space elements="*"/> 

<xsl:param name="pLimit" select="5"/> 

<xsl:template match="product"> 
    <xsl:if test="price > $pLimit"> 
     Name: <xsl:value-of select="title"/> 
     <xsl:text>, price </xsl:text> 
     <xsl:value-of select="price"/> 
    </xsl:if> 
</xsl:template> 
<xsl:template match="text()"/> 
</xsl:stylesheet>