Validating XAML with WXS?

Note: this entry has moved.

Drew Marsh (I recommend his blog on XAML-Avalon related posts), says

I'm really not sure that XAML can ever actually be pure schema validated in its current incarnation.
(here.. it's a shame the blockquote cite attribute doesn't work). And he shows as an example, the "attached" properties that are valid depending on the context, such as:

<DockPanel>
  <Border DockPanel.Dock="Top">

It turns out that validating this is extremely easy for a Schematron aware XML Schema (WXS) processor, such as Schematron.NET. This is achieved by using WXS built-int extensiblity mechanism. The following simple expression validates this case, :

<assert test="//*[boolean(@DockPanel.Dock) and local-name(..)="DockPanel"]">
The 'DockPanel.Dock' attribute can only appear on elements directly below a 'DockPanel'.
</assert>

First part of the XPath expression tests for the existence of the attribute (boolean(@DockPanel.Dock)) and the second part tests that the local name of its parent is a DockPanel (local-name(..)="DockPanel"). If this condition is not met, that is, the assertion fails, such a validator would signal an error. Cool, isn't it?
Maybe we should all start asking for Schematron support in .NET XmlValidatingReader... or extensiblity hooks at least to plug in other schema languages...

2 Comments

Comments have been disabled for this content.