2011-11-12 54 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>