Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 测试最后一个嵌套的分区是否为特定元素_Xml_Xpath_Schematron - Fatal编程技术网

Xml 测试最后一个嵌套的分区是否为特定元素

Xml 测试最后一个嵌套的分区是否为特定元素,xml,xpath,schematron,Xml,Xpath,Schematron,我需要一条规则来确保任何div[@class='extend']/div[@class='check']都位于div[@class='chapter']中的最后一个位置。请注意,可能还有其他具有不同嵌套div的“扩展”分区(如div[@class='extend']]/div[@class='video']),其位置不应是最后一个。div[@class='extend']/div[@class='check']的嵌套位使我无法编写此规则 我试过: XML 18.3 这里有一些介绍文字 检查18

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

我试过:

XML


18.3
这里有一些介绍文字

检查18.3

检查18.3 这是第18.3章的检查

此处为章节文本 文本文本

视频18.3

视频18.3 这是第18.3章的视频

此处为章节文本 文本文本

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。

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

下面的情况如何

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

此检查(带有)应该是一章中的最后一个扩展div。

请注意,此解决方案断言带有
class=“extend”
的div是
章节
中的最后一个子项(任何类型)。如果
章节
div
之后可能有其他元素(包括没有
class=“extend”
的其他
div
元素),这个断言将无法正确运行。

好的,根据您对Joshua回答的评论,我认为extend/check应该是该章节真正的最后一个内容(不仅仅是最后一个范围)

这应该起作用:


此检查(带有)应该是一章中的最后一个扩展div。

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

谢谢。不过,这并没有像预期的那样起作用。我需要确保扩展/检查是一章中的最后一块内容。这根本不是检查一章。last是什么意思?last of What?章节中的任何元素或任何
div[@class='extend']
?是的,你正确地理解了我对约书亚答案的评论。你的规则很有魅力。非常巧妙。这些评论帮助很大。谢谢!一次问题:为什么使用(>>)而不是一个>或gt?我以前从未见过。
$a>>$b
根据文档中的位置比较两个节点。它要求
$a
$b
正好是一个节点。
$a>$b
$a gt$b
$a
$b
转换为带编号的值,因此在节点的情况下,它将将比较内容。
$a>>$b
应等于
计数($a/previous::node())>count($b/previous::node())
(除了
$a
$b
在不同的文档中的情况除外),但效率要高得多。
<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>