Using XSLT to validate source XML's DTD

D

Duane Morin

I fear this might be a FAQ but I can't figure out how to google for
it.

Recently we had a big bug where the source that an XSL file acted on
changed out from under it. Result, the XSL matched nothing and
important content did not make it to the final destination.

I was under the impression that an XSL could be told "You will be
acting on an XML that is valid according to the following DTD." Thus,
if the XML that it was applied against was NOT what was expected, it
would crash rather than failing silently to match anything.

I do NOT want the DTD for the XSL file. Nor do I want to
independently validate the XML before sending it (because that
wouldn't solve my problem if the XSL does not know that the source has
changed). I need the XSL to be able to detect that the incoming XML
is no longer what it thinks it is.

Am I right?

Duane
 
P

Patrick TJ McPhee

% I was under the impression that an XSL could be told "You will be
% acting on an XML that is valid according to the following DTD."

Some XSL processors will validate documents which include a DOCTYPE,
but it depends on the processor. Note that DTD validation is always
against a DTD specified in the XML document, so it might not be
helpful (for the reasons you note in the part I'm about to elide).

% changed). I need the XSL to be able to detect that the incoming XML
% is no longer what it thinks it is.

If the XSL is supposed to handle every element, you could create a
catch-all template and have it terminate

<xsl:template match="*">
<xsl:message terminate='yes'>Oops. Didn't expect
<xsl:value-of select='name'/>.
</xsl:message>
</xsl:template>

this will be a problem if there are elements you currently want to
ignore.

Failing that, you could write a schema using xml schema or relax ng and
apply that as part of the xslt processing. You'd have to keep it in
synch with the style sheet, of course.

XSLT 2.0 can use schemas, but I believe the objective is to allow XPath
queries to involve data types, rather than to provide transform-time
validation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top