Vivian River
Vivian River

Reputation: 32400

How to make XSLT conditionally output a blank file with no DOCTYPE?

I'm working with some XSLT transformations. These transformations must be applied to thousands of XML documents each day in an automated way. The resulting files should be output to a target directory.

Note that some transformations produce XML and some produce plain text.

I have a new requirement that when the input XML contains certain values, certain XSLT transformations should produce no output at all and no file should be written.

I'm processing the output from XSLT in .net, so for the XSLT that yields text, I simply check that the resulting output is either blank or only whitespace; in which case I write no file, and everything is perfect.

The problem comes when I try to recognize blank outputs from XSLT that yields XML, because even if I specify that nothing should be output in my xsl:template block, the output will contain an XML doctype <?xml version="1.0" encoding="UTF-8"?> even though the document is otherwise blank.

One solution to the problem is to have my .net code filter out documents that contain only an XML doctype, but I'm not sure what the best way to do that is. So far, I have tried and xml.Trim().StartsWith("<?xml") && xml.Trim().EndsWith("?>"), but I don't know how bullet-proof that is.

It would probably be better if I could somehow make my XSLT output a doctype only if there is data to be output. I tried putting the <xsl:output> node inside an <xsl:if> node, but that doesn't work.

What's a good way to get the result I need?

Upvotes: 0

Views: 1117

Answers (4)

Caroline S
Caroline S

Reputation: 11

I think that mzjn's answer is probably best (always omit the declaration). But if you needed the declaration, You could use omit-xml-declaration="yes" in the <xsl:output> tag, but then just conditionally wrote the XML declaration at the top with <xsl:text>.

Upvotes: 0

Michael Kay
Michael Kay

Reputation: 163549

If you use XSLT 2.0 then you can write your result documents using xsl:result-document, and you can decide dynamically whether to write it or not. (There will always be a primary output as well, but you can direct that to /dev/null).

Upvotes: 1

mzjn
mzjn

Reputation: 51002

<?xml version="1.0" encoding="UTF-8"?> is not a "DOCTYPE". It is an XML declaration: http://www.w3.org/TR/xml/#sec-prolog-dtd.

If you want to omit the XML declaration, use

<xsl:output omit-xml-declaration = "yes"/> 

in your stylesheet. It works in both XSLT 1.0 and 2.0. References:

Note that <xsl:output> is a top-level element in a stylesheet module. It cannot be put in an <xsl:if>, as you already noticed. See Condition <xsl:output> in XSLT 1.0? for a related discussion.

It is good practice to have an XML declaration in XML documents, but it is not compulsory. So a pure XSLT option is to always omit the XML declaration.

Upvotes: 0

Daniel Haley
Daniel Haley

Reputation: 52888

I have a new requirement that when the input XML contains certain values, certain XSLT transformations should produce no output at all and no file should be written.

What I would do is conditionally perform the transform itself. Check the XML file before the transform is run. If the input dictates that no file should be created, don't run the transform.

Upvotes: 1

Related Questions