2011-08-16 21 views
3
<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> 
+0

好问题,+1。查看我对两种不同(XPath 1.0和XPath 2.0)单行表达式的答案。还提供了完整的基于XSLT的验证。 –

回答

0

使用这个XPath从下面的数据找出产品ID的最小值和最大值找到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 

为了找到最小值,与<替换>

1

I.的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的验证:

I. 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来注册名称空间以及该前缀的绑定。