【问题标题】:Test that last nested division is a particular element测试最后一个嵌套除法是一个特定的元素
【发布时间】:2019-04-29 17:57:49
【问题描述】:

我想要一个规则来确保任何div[@class='extend']/div[@class='check'] 位于div[@class='chapter'] 的最后。请注意,可能还有其他具有不同嵌套 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>

Schematron:

<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>

但是,这并没有找到我预期的结果(应该会产生错误)。

【问题讨论】:

  • 最后一个是什么意思?最后的什么?章节内的任何元素或任何div[@class = 'extend']?

标签: xml xpath schematron


【解决方案1】:

好的,根据您对 Joshua 回答的评论,我认为扩展/检查应该是章节的最后内容(不仅仅是最后一个范围)。

这应该可行:

    <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>

希望cmets帮助理解。

【讨论】:

  • 是的,您正确理解了我对 Joshua 回答的评论。你的规则就像一个魅力。相当巧妙。 cmets 的帮助很大。谢谢!
  • 曾经的问题:为什么使用 (>>) 而不是 > 或 gt ?我以前从未见过。
  • $a &gt;&gt; $b 根据文档中的位置比较两个节点。它要求$a$b 恰好是一个节点。 $a &gt; $b$a gt $b 会将 $a$b 转换为编号值,因此如果是节点,它将比较内容。 $a &gt;&gt; $b 应该等于count($a/preceding::node()) &gt; count($b/preceding::node())(除了$a$b 在不同的文档中的情况),但效率更高。
【解决方案2】:

下面的呢?

<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 中的最后一个子项(任何类型)。如果chapter 可以在div 之后有其他元素(包括没有class="extend" 的其他div 元素),则此断言将无法正常工作。

【讨论】:

  • 谢谢。但是,这并没有按预期工作。我需要确保扩展/检查是一章中的最后一部分内容。这根本不是检查章节。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 1970-01-01
  • 2022-01-22
  • 2015-04-20
相关资源
最近更新 更多