Dưới đây là ba XML-câyXPath 1.0 gần nhất trước và/hoặc nút tổ tiên với một thuộc tính trong một XML Tree
(1)
<?xml version="1.0" encoding="UTF-8"?>
<content>
<section id="1">
<section id="2"/>
<section id="3"/>
<section id="9"/>
</section>
<section id="4">
<section id="5">
<section>
<bookmark/> <!-- here's the bookmark-->
<section id="6">
<section id="7">
<section id="8"/>
</section>
</section>
</section>
</section>
</section>
</content>
(2)
<?xml version="1.0" encoding="UTF-8"?>
<content>
<section id="1"/>
<section id="2">
<section id="9">
<section id="10">
<section id="11"/>
</section>
</section>
<section>
<section id="4">
<section id="5"/>
</section>
</section>
<section/>
<bookmark/> <!-- here's the bookmark-->
<section id="6">
<section id="7">
<section id="8"/>
</section>
</section>
</section>
</content>
Các kết quả mong muốn trong cả hai trường hợp là số .
Với XSLT 1.0 và XPath 1.0 Tôi có thể hoặc là có được tổ tiên từ (1)
<xsl:value-of select="//bookmark/ancestor::*[@id][1]/@id"/>
hoặc nút trước từ (2)
<xsl:value-of select="//bookmark/preceding::*[@id][1]/@id"/>
Làm thế nào để tôi có được gần tổ tiên hoặc trước nút có id từ dấu trang của tôi?
Tôi cần một giá trị xsl: value-of khớp với cả hai trường hợp. Cảm ơn.
EDIT:
Giải pháp này cũng nên bao gồm cấu trúc này. Id mong muốn vẫn là .
(3)
<?xml version="1.0" encoding="UTF-8"?>
<content>
<section id="1">
<section id="2"/>
<section id="3"/>
<section id="9"/>
</section>
<section id="4">
<section>
<section id="10"/>
<section id="5"/>
<section>
<bookmark/> <!-- here's the bookmark-->
<section id="6">
<section id="7">
<section id="8"/>
</section>
</section>
</section>
</section>
</section>
</content>
Hình ảnh hiển thị tất cả các trục xpath: [pic] (http://nwalsh.com/docs/tutorials/xsl/xsl/graphics/axes.gif) – therealmarv
Câu hỏi hay, +1. Xem câu trả lời của tôi cho biểu thức XPath một lớp mà chọn chính xác nút thuộc tính mong muốn trong cả ba trường hợp. :) –