3

I am using ISO Schematron to validate an XML document. The rule I have set works as required, but I want to report the name of the parent element from the context of where my assertion is fired. A sample document might contain the XML fragment:

<Name>Alan Smith<br/></Name>

My Schematron rule tests that the <br/> element is only allowed to occur as a child of a <title> element. The rule is as follows:

<rule context="br">
    <assert test="parent::title"
     >The "br" element is not allowed as a child of the '<iso:value-of select=".."/>' element.'</assert>
</rule>

When I run this rule against the fragment, the assertion is correctly fired, however I want the message to tell me that "The "br" element is not allowed as a child of the 'Name' element". Instead it actually shows "The "br" element is not allowed as a child of the 'Alan Smith' element". So the <iso:value-of select=".."/> expression is actually selecting the text node and not the parent element as I expected? Can someone help explain what XPpath expression I should be using in the select statement to display the name of the parent element in my error message?. Thanks.

UPDATE: I managed to achieve the desired result by removing the select statement, and replacing it with a name element as in:

<rule context="br">
    <assert test="parent::title"
     >The "br" element is not allowed as a child of the '<name path=".."/>' element.'</assert>
</rule>

So my problem is solved, but if anyone can explain why my first attempt doesn't work, that would be very helpful.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
Wee Shetland
  • 955
  • 8
  • 18

2 Answers2

1

I believe that tony_h's original solution doesn't work because of the 'iso:' prefix. Since the rest of the code does not share the namespace. Omitting that part would solve the problem.

Pydan
  • 57
  • 10
  • The description provided by the OP in the question shows that `` is interpreted as the `value-of` element defined by the Schematron specification. It does provide exactly the output it should, which is not what the OP wanted. Seeing as `iso:value-of` does exactly what schematron specifies, it is likely that the `iso` prefix is mapped to the same namespace as the default (unprefixed) one, which is completely valid in XML. The accepted answer is the solution. This answer here is not supported by the information provided in the question. – Louis Oct 14 '16 at 18:36
1

That is how value-of element works. From http://www.schematron.com/iso/P8.html#GEN16

value-of element

Finds or calculates values from the instance document to allow clearer assertions and diagnostics. The required select attribute is an expression evaluated in the current context that returns a string.

Variable references in the select attribute are resolved in the scope of the current schema, phase, pattern and rule.

An implementation which does not report natural-language assertions is not required to make use of this element.

You can get the name of context node's parent with this XPath expression.

name(..)
  • Thanks Alejandro, it makes sense now, the clue is in the name of the function 'value-of', it provides the value of the xpath node ".." ! – Wee Shetland Apr 13 '11 at 10:20
  • @tony_h: You are welcome. Like in XSLT, "value" means string value. –  Apr 13 '11 at 12:51