I need to validate a small fragment of an xml file against a schema. Essentially, I'd like to ask the question "Does element X in XML document Y conform to its type as defined in schema Z?" and if not get a message describing why. This has to account for restrictions placed on those types as well (eg maxLength, minInclusive).
Is this possible?
I don't know about doing this from C#, but it's easily done in XQuery or XSLT 2.0. In XSLT 2.0 it's:
<xsl:copy-of select="doc('doc.xml')//selected/element" validation="strict"/>
and in XQuery it's
validate strict {doc('doc.xml')//selected/element}
All you need is a schema-aware XQuery or XSLT 2.0 processor that runs in your chosen environment.
It turns out this was much easier than I expected. The solution was to create a new schema that contains as its root the one element I want to verify. Once this schema is added to the schemaset, you can simply validate the fragment as you would any complete document.
A microsoft knowledge article that exactly describes validating Xml fragments. This could be useful. http://support.microsoft.com/kb/318504
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.