2

I am currently playing with Schematron for XML Document [Data] Validation. Schematron appears to work suitably, at least in initial testing, and I like the general approach of user-centric validation coupled with XPath contexts/rules1.

However, I would like to have this same sort of "declarative validation" setup in POCC (Plain Old C# Code) as some of the validation that needs to be done is outside the scope of what Schematron can handle - at least without additional transformations or pipelines, and I don't like XML that much.

I've found a few libraries like Fluent Validation, similar posts and books, but nothing seems to be tailored to XML/XPath - but perhaps I am too enamored by the terse XPath syntax to understand how Linq-to-XML can really fit in ..

So the question is;

Is there a C# (.NET) fluent/declarative validation library that is tailored to leverage XPath and work with XML documents?

Before voting to close this as a "shopping question", consider the apparent scarcity/obscurity of such a library. It appears that some of the declarative validation libraries could be hacked up with such support, but after playing around a bit, it just doesn't "feel natural". Any practical experience extending fluent validation libraries to support XPath in this aspect would also make a good answer, especially if it has proven to be a suitable approach.


1 This project also uses XSD Schema validation for the structure/type validation (i.e. "well typed" validation). This phase of validation is about "correct semantics" validation; however, XSD does not generate "friendly" error messages, it cannot handle [co-]dependent values, and is .. more verbose. RELAX NG would solve at least one of these problems, but not all.

Community
  • 1
  • 1

1 Answers1

0

XSLT 2.0 can be used for declarative XPath based validation. Here are a few benefits:

With the release of version 2.0, XSLT now allows you to design your stylesheets to be schema-aware. A schema-aware XSLT system offers many benefits, including the ability to validate input trees prior to the XSLT transformation to ensure that the XSLT stylesheet only processes valid input, as well as the ability to validate output trees to ensure that the XSLT transformation is producing the valid XML output. You are also able to specify data types for:

  • variables
  • input parameters
  • user-defined functions
  • templates
  • return values

XSLT 2.0 integration with .Net is detailed in the following questions and answers:

References

Community
  • 1
  • 1
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265