测试最后的嵌套除法是特定元素

时间:2019-04-29 17:57:49

标签: xml xpath schematron

我想要一个规则来确保任何qdiv[@class='extend']/div[@class='check']中位于最后。注意,可能还有其他具有不同嵌套div(例如div[@class='chapter'])的“ extend”分区,它们的位置不应位于最后。 div[@class='extend']/div[@class='video']的嵌套位使我无法编写此规则。

我尝试过:

XML

div[@class='extend']/div[@class='check']

Schematron:

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

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

2 个答案:

答案 0 :(得分:1)

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

这应该有效:

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

希望这些评论有助于理解。

答案 1 :(得分:0)

以下内容如何?

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