Как я могу узнать минимальное и максимальное значения productID из приведенных ниже данных, используя выражение xpath - PullRequest
3 голосов
/ 16 августа 2011
<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 ]

1 голос
/ 16 августа 2011

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 используются префиксные имена, API используемого механизма XPath должен использоваться для регистрации пространства имен и привязки префикса к нему.

0 голосов
/ 16 августа 2011

Используйте этот 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

Чтобы найти минимальное значение, замените > на <.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...