测试最后一个嵌套部分是一个特定元素
Test that last nested division is a particular element
我想要一个规则来确保 div[@class='extend']/div[@class='check']
位于 div[@class='chapter']
的最后。请注意,可能有其他 "extend" 个具有不同嵌套 div 的分区(如 div[@class='extend']/div[@class='video']
),其位置预计不会是最后一个。 div[@class='extend']/div[@class='check']
的嵌套位使我无法编写此规则。
我试过:
XML
<test>
<div class="chapter" id="s1">
<h1 class="title">18.3</h1>
<div class="intro" id="s2">
<p>Some intro text here</p>
</div>
<!--Incorrect position of div class="extend"/div class="check"-->
<div class="extend" id="s3">
<div class="check" id="s4">
<div class="metadata" id="s5">
<div class="title" id="s6">
<p>Check 18.3</p>
</div>
</div>
<h1 class="title">Check 18.3</h1>
<p>This is the Check for Chapter 18.3</p>
</div>
</div>
<!---\-\-\-->
<div class="sect1" id="s7">
<h1 class="title">Section text here</h1>
<p>text text text.</p>
<div class="extend" id="s8">
<div class="video">
<div class="metadata" id="s9">
<div class="title" id="s10">
<p>Video 18.3</p>
</div>
</div>
<h1 class="title">Video 18.3</h1>
<p>This is the Video for Chapter 18.3</p>
</div>
</div>
<div class="sect2" id="s11">
<h1 class="title">Section text here</h1>
<p>text text text.</p>
</div>
</div>
<!--div class="extend"/div class="check" should be here as last div of chapter-->
</div>
</test>
示意图:
<pattern id="lastdiv">
<rule context="xhtml:div[@class='check']">
<assert test="(ancestor::xhtml:div[@class='chapter']/xhtml:div[@class='extend'])[last()]" role="error">This check (with <value-of select="@id"/>) should be the last extend div within a chapter.</assert>
</rule>
</pattern>
但是,这没有找到我预期的结果(应该会产生错误)。
下面的怎么样?
<pattern id="lastdiv">
<!-- This rule fires on divs that have a class of "extend" and that have a child div with a class of "check" -->
<rule context="xhtml:div[@class='extend' and xhtml:div/@class='check']">
<!-- The assert tests that the div is the last div within its parent -->
<assert test="position() = last()" role="error">This check (with <value-of select="@id"/>) should be the last extend div within a chapter.</assert>
</rule>
</pattern>
请注意,此解决方案断言带有 class="extend"
的 div 是 chapter
中的最后一个 child(任何类型)。如果 chapter
可以在 div
之后有其他元素(包括没有 class="extend"
的其他 div
元素),则此断言将无法正常工作。
好的,根据你的评论作为对约书亚回答的回应,我认为 extend/check 应该是章节的真正最后内容(不仅仅是最后一个范围)。
这应该有效:
<pattern id="lastdiv">
<rule context="xhtml:div[@class = 'extend'][xhtml:div[@class = 'check']]">
<!-- the current extend div -->
<let name="extend" value="."/>
<!-- all elements of the current chapter, except of $extend and its descendantes -->
<let name="chapterElements" value="ancestor::xhtml:div[@class = 'chapter']//* except ($extend, $extend//*)"/>
<!-- all $chapterElements which have a larger document position (>>) than the $extend-->
<let name="followingChapterElements" value="$chapterElements[. >> $extend]"/>
<!-- If there is a $followingChapterElements, the check fails-->
<report test="$followingChapterElements" role="error">This check (with <value-of select="@id"/>) should be the last extend div within a chapter.</report>
</rule>
</pattern>
希望评论能帮助理解。
我想要一个规则来确保 div[@class='extend']/div[@class='check']
位于 div[@class='chapter']
的最后。请注意,可能有其他 "extend" 个具有不同嵌套 div 的分区(如 div[@class='extend']/div[@class='video']
),其位置预计不会是最后一个。 div[@class='extend']/div[@class='check']
的嵌套位使我无法编写此规则。
我试过:
XML
<test>
<div class="chapter" id="s1">
<h1 class="title">18.3</h1>
<div class="intro" id="s2">
<p>Some intro text here</p>
</div>
<!--Incorrect position of div class="extend"/div class="check"-->
<div class="extend" id="s3">
<div class="check" id="s4">
<div class="metadata" id="s5">
<div class="title" id="s6">
<p>Check 18.3</p>
</div>
</div>
<h1 class="title">Check 18.3</h1>
<p>This is the Check for Chapter 18.3</p>
</div>
</div>
<!---\-\-\-->
<div class="sect1" id="s7">
<h1 class="title">Section text here</h1>
<p>text text text.</p>
<div class="extend" id="s8">
<div class="video">
<div class="metadata" id="s9">
<div class="title" id="s10">
<p>Video 18.3</p>
</div>
</div>
<h1 class="title">Video 18.3</h1>
<p>This is the Video for Chapter 18.3</p>
</div>
</div>
<div class="sect2" id="s11">
<h1 class="title">Section text here</h1>
<p>text text text.</p>
</div>
</div>
<!--div class="extend"/div class="check" should be here as last div of chapter-->
</div>
</test>
示意图:
<pattern id="lastdiv">
<rule context="xhtml:div[@class='check']">
<assert test="(ancestor::xhtml:div[@class='chapter']/xhtml:div[@class='extend'])[last()]" role="error">This check (with <value-of select="@id"/>) should be the last extend div within a chapter.</assert>
</rule>
</pattern>
但是,这没有找到我预期的结果(应该会产生错误)。
下面的怎么样?
<pattern id="lastdiv">
<!-- This rule fires on divs that have a class of "extend" and that have a child div with a class of "check" -->
<rule context="xhtml:div[@class='extend' and xhtml:div/@class='check']">
<!-- The assert tests that the div is the last div within its parent -->
<assert test="position() = last()" role="error">This check (with <value-of select="@id"/>) should be the last extend div within a chapter.</assert>
</rule>
</pattern>
请注意,此解决方案断言带有 class="extend"
的 div 是 chapter
中的最后一个 child(任何类型)。如果 chapter
可以在 div
之后有其他元素(包括没有 class="extend"
的其他 div
元素),则此断言将无法正常工作。
好的,根据你的评论作为对约书亚回答的回应,我认为 extend/check 应该是章节的真正最后内容(不仅仅是最后一个范围)。
这应该有效:
<pattern id="lastdiv">
<rule context="xhtml:div[@class = 'extend'][xhtml:div[@class = 'check']]">
<!-- the current extend div -->
<let name="extend" value="."/>
<!-- all elements of the current chapter, except of $extend and its descendantes -->
<let name="chapterElements" value="ancestor::xhtml:div[@class = 'chapter']//* except ($extend, $extend//*)"/>
<!-- all $chapterElements which have a larger document position (>>) than the $extend-->
<let name="followingChapterElements" value="$chapterElements[. >> $extend]"/>
<!-- If there is a $followingChapterElements, the check fails-->
<report test="$followingChapterElements" role="error">This check (with <value-of select="@id"/>) should be the last extend div within a chapter.</report>
</rule>
</pattern>
希望评论能帮助理解。