2011-10-27 33 views
5

Tôi đã sử dụng XPath để chọn nút với giá trị số nguyên id lớn nhất trước khi sử dụng truy vấn này:Làm cách nào để chọn một nút XML có giá trị nút con #text dài nhất với XPath?

//somenode[not(@id <= preceding::somenode/@id) and not(@id <= following::somenode/@id)] 

Tôi đã hy vọng rằng tôi có thể làm điều gì đó tương tự như:

//entry[not(string-length(child::text()) <= string-length(preceding::entry/child::text())) and not(string-length(child::text()) <= string-length(following::entry/child::text()))] 

Nhưng nó sẽ trả về một loạt các nút thay vì chỉ một.

mẫu XML:

<xml> 
    <entry>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</entry> 
    <entry>Nam dignissim mi a massa mattis rutrum eu eget mauris.</entry> 
    <entry>Ut at diam a sem scelerisque pretium nec pulvinar purus.</entry> 
    <entry>Nunc in nisi nec dolor accumsan suscipit vel a quam.</entry> 
    <entry>Nunc suscipit lobortis arcu, nec adipiscing libero bibendum nec.</entry> 
    <entry>Aenean eget ipsum et nunc eleifend scelerisque.</entry> 
    <entry>In eu magna et diam volutpat molestie.</entry> 
    <entry>In volutpat luctus mi, eu laoreet orci dictum vel.</entry> 
    <entry>In mattis mi nec magna sodales eu bibendum felis aliquet.</entry> 
<!-- etc for 800 more lines or so --> 
    <entry>Duis auctor felis id neque gravida ut auctor ipsum ullamcorper.</entry> 
    <entry>Sed vel tortor mauris, et aliquet tellus.</entry> 
</xml> 

kiểm tra XPath: http://chris.photobooks.com/xml/default.htm?state=1o

Trả lời

2

Yếu tố truy nã (s) không thể được lựa chọn với một XPath đơn 1.0 biểu, bởi vì trong XPath 1.0 nó không thể áp dụng một hàm cho tất cả các nút được chọn (string-length(someNodeSet) chỉ được áp dụng trên nút đầu tiên của tập hợp nút này). Một lý do khác là trong XPath 1.0, không thể đặt tên và tham chiếu các biến phạm vi.

Trong XPath 2.0 này là không quan trọng:

/*/entry[not(string-length(.) &lt; /*/entry/string-length(.))] 

sẽ chọn trên hết entry yếu tố chiều dài của chuỗi có giá trị là một tối đa.

/*/entry[not(string-length(.) &lt; /*/entry/string-length(.))] [1] 

Ở trên, chọn phần thứ nhất (theo thứ tự tài liệu) như vậy entry phần tử.

XSLT 2.0 - có trụ sở xác minh:

chuyển đổi này:

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="/"> 
    <xsl:sequence select= 
    "/*/entry[not(string-length(.) &lt; /*/entry/string-length(.))]"/> 
</xsl:template> 
</xsl:stylesheet> 

khi áp dụng trên tài liệu XML cung cấp:

<xml> 
    <entry>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</entry> 
    <entry>Nam dignissim mi a massa mattis rutrum eu eget mauris.</entry> 
    <entry>Ut at diam a sem scelerisque pretium nec pulvinar purus.</entry> 
    <entry>Nunc in nisi nec dolor accumsan suscipit vel a quam.</entry> 
    <entry>Nunc suscipit lobortis arcu, nec adipiscing libero bibendum nec.</entry> 
    <entry>Aenean eget ipsum et nunc eleifend scelerisque.</entry> 
    <entry>In eu magna et diam volutpat molestie.</entry> 
    <entry>In volutpat luctus mi, eu laoreet orci dictum vel.</entry> 
    <entry>In mattis mi nec magna sodales eu bibendum felis aliquet.</entry> 
<!-- etc for 800 more lines or so --> 
    <entry>Duis auctor felis id neque gravida ut auctor ipsum ullamcorper.</entry> 
    <entry>Sed vel tortor mauris, et aliquet tellus.</entry> 
</xml> 

chọn entry yếu tố (trong trường hợp này chỉ có một) với t chiều dài chuỗi tối đa và xuất ra các phần tử đã chọn:

<entry>Nunc suscipit lobortis arcu, nec adipiscing libero bibendum nec.</entry> 
+0

Tôi nghĩ rằng bạn đang thiếu thứ gì đó trong truy vấn thứ hai đó, một '[1]' có thể kết thúc? – travis

+0

@travis: Tốt bắt - Tôi hơi tập trung vào tối nay. Đã sửa. –

+2

+1 - * "(chiều dài chuỗi (someNodeSet) chỉ được áp dụng trên nút đầu tiên của bộ nút này)" * - Tôi đã mắc sai lầm ngớ ngẩn và sau đó bị lừa bởi các trường hợp thử nghiệm quá ít. Đây là câu trả lời hay nhất. –

Các vấn đề liên quan