Bạn có thể sử dụng preceding
và following
XPath trục để kiểm tra xem không có giá trị lớn:
XmlDocument doc = new XmlDocument();
doc.LoadXml("<documents>"
+ " <document><id>3</id></document>"
+ " <document><id>7</id></document>"
+ " <document><id>1</id></document>"
+ "</documents>");
var max = doc.SelectSingleNode(
"/documents/document[not(id < preceding::document/id)
and not(id < following::document/id)]");
Nếu có nhiều giá trị id tối đa trong tài liệu mã trên sẽ trở lại đầu tiên. Nếu bạn muốn yếu tố cuối cùng với một id tối đa mà bạn có thể sử dụng
var max = doc.SelectSingleNode(
"/documents/document[not(id < preceding::document/id)
and not(id <= following::document/id)]");
Nếu bạn muốn có được một danh sách tất cả các yếu tố có một id tối đa mà bạn có thể sử dụng SelectNodes
phương pháp: phiên bản
var maxList = doc.SelectNodes(
"/documents/document[not(id < preceding::document/id)
and not(id < following::document/id)]");
XSLT :
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<root>
<xsl:value-of
select="/documents/document/id[not(. <= preceding::document/id)
and not(. <= following::document/id)]"/>
</root>
</xsl:template>
</xsl:stylesheet>
Nguồn
2010-09-24 11:24:46
+1 Đối với biểu thức tối đa XPath 1.0 ngắn nhất và chính xác, bên cạnh sự phức tạp cuadratic ... –
cảm ơn, giải pháp tuyệt vời, bạn sẽ nhận được điểm thưởng cho cái này! – Hinek
Giải pháp thông minh! –