<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>
最佳答案
我。 XPath 2.0
使用这个 XPath 2.0 表达式:
max(/*/*/*/*/*/*/nor:ProductID)
和,分别:
min(/*/*/*/*/*/*/nor:ProductID)
二。 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(. < following::nor:ProductID)
and
not(. < 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
二。 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 必须已用于注册命名空间和前缀绑定(bind)到它。
关于xml - 我如何使用 xpath 表达式从以下数据中找出 productID 的最小值和最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7076041/