Validate one node based on criteria from another node by referencing the criteria's @ID
I am trying to validate the presence of a child element based on criteria in a different element in the document identified by its ID. Using ISO Schematron and XPath 2.0.
Take the following example:
<value id="red" bits="16" />
<foo value_id="red">
<increased_sensitivity/>
</foo>
The element "increased_sensitivity" should only be allowed if a "value" element with the @id "red" co开发者_如何学运维ntains the attribute bits="16" somewhere in the document.
To be completely honest, im not even certain this is possible with Schematron, my experience with it is quite limited.
Assuming you want to find the value element whose ID matches the foo id value ("red" in this case), and then inspect the bits value there, then this Schematron schema shows one way it could be done:
<?xml version="1.0"?>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<sch:let name="root" value="/"/>
<sch:pattern>
<sch:rule context="increased_sensitivity">
<sch:let name="id" value="parent::*/@value_id"/>
<sch:let name="assoc-bits-value" value="$root//value[@id=$id]/@bits"/>
<sch:assert test="$assoc-bits-value = 16">When the increased_sensitivity element is used, a
corresponding value element must exist in the doucment with a bits value of
"16".</sch:assert>
</sch:rule>
</sch:pattern>
</sch:schema>
精彩评论