如何使用xpath表达式从以下数据中找出productID的最小值和最大值

时间:2011-08-16 09:20:54

标签: xml xpath

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:nor="http://schemas.cordys.com/NorthwindMetadata">
   <soapenv:Header/>
   <soapenv:Body>
      <nor:UpdateOrder_x0020_Details reply="yes" commandUpdate="no" preserveSpace="no" batchUpdate="no">
         <nor:tuple>
            <nor:new>
               <nor:Order_x0020_Details qAccess="0" qConstraint="0" qInit="0" qValues="">
                  <nor:OrderID>11113</nor:OrderID>
                  <nor:ProductID>43</nor:ProductID>
                  <nor:UnitPrice>36.0000</nor:UnitPrice>
                  <nor:Quantity>25</nor:Quantity>
                  <nor:Discount>0</nor:Discount>
               </nor:Order_x0020_Details>
            </nor:new>
         </nor:tuple>
         <nor:tuple>
            <nor:new>
               <nor:Order_x0020_Details qAccess="0" qConstraint="0" qInit="0" qValues="">
                  <nor:OrderID>11113</nor:OrderID>
                  <nor:ProductID>30</nor:ProductID>
                  <nor:UnitPrice>99.000</nor:UnitPrice>
                  <nor:Quantity>10</nor:Quantity>
                  <nor:Discount>0</nor:Discount>
               </nor:Order_x0020_Details>
            </nor:new>
         </nor:tuple>
         <nor:tuple>
            <nor:new>
               <nor:Order_x0020_Details qAccess="0" qConstraint="0" qInit="0" qValues="">
                  <nor:OrderID>11113</nor:OrderID>
                  <nor:ProductID>40</nor:ProductID>
                  <nor:UnitPrice>88.0000</nor:UnitPrice>
                  <nor:Quantity>19</nor:Quantity>
                  <nor:Discount>0</nor:Discount>
               </nor:Order_x0020_Details>
            </nor:new>
         </nor:tuple>
      </nor:UpdateOrder_x0020_Details>
   </soapenv:Body>
</soapenv:Envelope>

2 个答案:

答案 0 :(得分:1)

<强>予。 XPath 2.0

使用此XPath 2.0表达式

   max(/*/*/*/*/*/*/nor:ProductID)

和分别:

   min(/*/*/*/*/*/*/nor:ProductID)

<强> II。 XPath 1.0

使用此XPath 1.0表达式

/*/*/*/*/*/*/nor:ProductID
                   [not(. > following::nor:ProductID)
                  and
                    not(. > preceding::nor:ProductID)
                   ]

和分别:

/*/*/*/*/*/*/nor:ProductID
                   [not(. < following::nor:ProductID)
                  and
                    not(. < preceding::nor:ProductID)
                   ]

以下是基于XSLT的两种解决方案验证

<强>予。 XSLT 1.0 :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:nor="http://schemas.cordys.com/NorthwindMetadata">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
  min:   <xsl:value-of select=
            "/*/*/*/*/*/*/nor:ProductID
                   [not(. > following::nor:ProductID)
                  and
                    not(. > preceding::nor:ProductID)
                   ]
     "/>
     <xsl:text>/ max: </xsl:text>
     <xsl:value-of select=
     "/*/*/*/*/*/*/nor:ProductID
                   [not(. &lt; following::nor:ProductID)
                  and
                    not(. &lt; preceding::nor:ProductID)
                   ]
     "/>
 </xsl:template>
</xsl:stylesheet>

将此转换应用于提供的XML文档

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:nor="http://schemas.cordys.com/NorthwindMetadata">
    <soapenv:Header/>
    <soapenv:Body>
        <nor:UpdateOrder_x0020_Details reply="yes" commandUpdate="no" preserveSpace="no" batchUpdate="no">
            <nor:tuple>
                <nor:new>
                    <nor:Order_x0020_Details qAccess="0" qConstraint="0" qInit="0" qValues="">
                        <nor:OrderID>11113</nor:OrderID>
                        <nor:ProductID>43</nor:ProductID>
                        <nor:UnitPrice>36.0000</nor:UnitPrice>
                        <nor:Quantity>25</nor:Quantity>
                        <nor:Discount>0</nor:Discount>
                    </nor:Order_x0020_Details>
                </nor:new>
            </nor:tuple>
            <nor:tuple>
                <nor:new>
                    <nor:Order_x0020_Details qAccess="0" qConstraint="0" qInit="0" qValues="">
                        <nor:OrderID>11113</nor:OrderID>
                        <nor:ProductID>30</nor:ProductID>
                        <nor:UnitPrice>99.000</nor:UnitPrice>
                        <nor:Quantity>10</nor:Quantity>
                        <nor:Discount>0</nor:Discount>
                    </nor:Order_x0020_Details>
                </nor:new>
            </nor:tuple>
            <nor:tuple>
                <nor:new>
                    <nor:Order_x0020_Details qAccess="0" qConstraint="0" qInit="0" qValues="">
                        <nor:OrderID>11113</nor:OrderID>
                        <nor:ProductID>40</nor:ProductID>
                        <nor:UnitPrice>88.0000</nor:UnitPrice>
                        <nor:Quantity>19</nor:Quantity>
                        <nor:Discount>0</nor:Discount>
                    </nor:Order_x0020_Details>
                </nor:new>
            </nor:tuple>
        </nor:UpdateOrder_x0020_Details>
    </soapenv:Body>
</soapenv:Envelope>

产生了想要的正确结果

  min:   30/ max: 43

<强> II。 XSLT 2.0 :

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:nor="http://schemas.cordys.com/NorthwindMetadata">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
    max: <xsl:sequence select=
     "max(/*/*/*/*/*/*/nor:ProductID)"/>
     <xsl:text>/ min: </xsl:text>
     <xsl:sequence select=
     "min(/*/*/*/*/*/*/nor:ProductID)"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于同一XML文档(上图)时,会再次生成所需的正确答案

max: 43/ min: 30

最后的注释:与XPath表达式中的前缀名称一样,使用过的XPath引擎的API必须用于注册命名空间以及将前缀绑定到它。

答案 1 :(得分:0)

使用此XPath查找nor:tuple最多nor:ProductID

//nor:tuple[
    not(following-sibling::nor:tuple/nor:new//nor:ProductID > nor:new//nor:ProductID 
        or 
        preceding-sibling::nor:tuple/nor:new//nor:ProductID > nor:new//nor:ProductID)
]

要查找最大值(在您的示例中为43):

//nor:tuple[
    not(following-sibling::nor:tuple/nor:new//nor:ProductID > nor:new//nor:ProductID 
        or 
        preceding-sibling::nor:tuple/nor:new//nor:ProductID > nor:new//nor:ProductID)
]/nor:new//nor:ProductID

要查找最小值,请将>替换为<