views:

72

answers:

1

I have to pass back information from a different source and can have a transformation in between, But depending on if the source document has the xml processing instruction, I have to pass it on and if it doesnt, I shouldnt pass it on.

Input
<?xml version="1.0" encoding="UTF-8"?>
<Source/>

Output    
<?xml version="1.0" encoding="UTF-8"?>
<Source/>

If it doesnt have the processing instruction, I shouldnt include it in the output, mainly to support some other legacy programs which do not understand them (duh!)

I can dynamically switch stylesheets and use the omit-xml-declaration based on if the input had the instruction, but is there a way to do it in a single stylesheet?

EDIT: I cant remove the transformation that is present prior to returning the data.

EDIT: I just read that even though it looks like a processing instruction, the <?xml version.. ?>, infact it is not, so not sure if it is possible to match on it, any suggestions?

+2  A: 

The XML declaration looks like a processing instruction, but it isn't.

It exists to inform the XML parser about the document that it is going to read (XML version, character encoding).

It does not exist in the resulting DOM/infoset and therefore it cannot be matched in XSLT.

Additionally, you cannot dynamically alter the <xsl:output> directive, something like different output elements in an <xsl:if> or <xsl:choose> construct won't work.

My suggestion is: If part of the downstream applications do not understand XML declarations (which is a WTFfact that really makes me scratch my head), then leave them off all the time using:

<xsl:output omit-xml-declaration="yes" />
Tomalak
What I meant was, I can dynamically switch the stylesheet itself, one which has the omit-xml-declaration and one that doesnt. Thanks for your answer.
Thiyagaraj
You could look for the string `"<?xml"` in the source documents and decide what stylesheet you want to run. I don't think that you can make the decision from *within* the XSLT, though.
Tomalak