2010-09-24 128 views
0

我想写一些Schematron规则,其中一个应该检查,如果元素在父元素的范围内是唯一的。所以我有一个xml结构的例子:Schematron验证和唯一性

<abc> 
    <elem id="qw0"> 
    <a>1</a> 
    <a>2</a> 
    <a>3</a> 
    </elem> 
    <elem id="qw1"> 
    <a>1</a> 
    <a>2</a> 
    <a>3</a> 
    <a>3</a> 
    </elem> 
</abc> 

我的规则应该检查每个元素的“a”元素是否是唯一的。在此具体示例中,对于elemid =“qw1”有两个元素“a”,其值为“3”。这不应该被允许。

到目前为止,我来到这样的规则:

<iso:pattern id="doc.abc"> 
    <iso:title>checking ABC</iso:title> 
    <iso:rule context="elem"> 
    <iso:assert test="count(a[. = current()]) = 1">TACs should be unique.</iso:assert> 
    </iso:rule> 
</iso:pattern> 

但是,这并不工作,因为它看起来在整个文件,而不仅仅是ELEM的直接孩子。

回答

0

我发现,这可能与以下规则来解决:

<iso:pattern id="doc.abc"> 
    <iso:title>checking ABC</iso:title> 
    <iso:rule context="a"> 
    <iso:assert test="count(parent::node()/a[. = current()) = 1">TACs should be unique.</iso:assert> 
    </iso:rule> 
</iso:pattern> 

但是,这激发了对每一个元素规则。

它会更优雅地发射它每elem,没有a

1

如果您使用的是Schematron的处理器与底层XSLT/XPath的2.0发动机,并希望使该规则的背景下,< ELEM>元素,你可以使用:

 
    <sch:pattern> 
    <sch:rule context="elem"> 
     <sch:report test="count(a) != count(distinct-values(a))"> 
     Values not distinct</sch:report> 
    </sch:rule> 
    </sch:pattern>