首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用xpath表达式从下面的数据中找出productID的最小和最大值

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

Stack Overflow用户
提问于 2011-08-16 17:20:54
回答 2查看 181关注 0票数 3
代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

发布于 2011-08-16 21:11:33

I. XPath 2.0

使用此XPath 2.0表达式

代码语言:javascript
复制
   max(/*/*/*/*/*/*/nor:ProductID)

代码语言:javascript
复制
   min(/*/*/*/*/*/*/nor:ProductID)

使用此XPath 1.0表达式

代码语言:javascript
复制
/*/*/*/*/*/*/nor:ProductID
                   [not(. > following::nor:ProductID)
                  and
                    not(. > preceding::nor:ProductID)
                   ]

代码语言:javascript
复制
/*/*/*/*/*/*/nor:ProductID
                   [not(. < following::nor:ProductID)
                  and
                    not(. < preceding::nor:ProductID)
                   ]

这里的是两个解决方案的基于XSLT的验证。

XSLTI.XSLT1.0

代码语言:javascript
复制
<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文档上应用此转换时为

代码语言:javascript
复制
<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>

生成所需的正确结果

代码语言:javascript
复制
  min:   30/ max: 43

代码语言:javascript
复制
<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文档(如上)时,同样会生成所需的正确答案

代码语言:javascript
复制
max: 43/ min: 30

最后说明:当在XPath表达式中使用前缀名称时,必须使用所使用的XPath引擎的来注册名称空间和前缀到它的绑定。

票数 1
EN

Stack Overflow用户

发布于 2011-08-16 18:28:11

使用此XPath查找具有最大nor:ProductIDnor:tuple

代码语言:javascript
复制
//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中):

代码语言:javascript
复制
//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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7076041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档