Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to perform a conditional include in XSLT, but xsl:include is a top level element. You can only use xsl:if or xsl:choose inside of a template. Is there any kind of hack or work around which allows a conditional include of an external file? I tried to use the document() function, but it fails to load my external file ( probably because it doesn't conform to some set of rules which would make it "valid" ).

My external xml file is a bunch of xslt code fragments. depending on the value of a variable in the main XSLT file, the corisponding code from the external file should be "copy/pasted" in place ( like conditional include in C or PHP ).

The flow of my main XSLT file should proceed in the following manner:

$configurationMode
if ( $configurationMode = Standard ) { xsl:include="standard.xml" } else { xsl:include="alt.xml" }

Obviously I cannot do it as simply as the above, hence why I am asking if there is a hack or workaround.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

This cannot be done with XSLT 1.0 and can be done (to a very limited extent) in XSLT 2.0 using the use-when attribute.

There exist non-xslt ways of achieving the wanted dynamic altering of an xsl:include or an xsl:import directive.

One such method is to load the XSLT stylesheet as an XmlDocument, and using the available DOM methods for access to and modification of attributes, to set the href attribute to the desired value. Then initiate the transformation from this in-memory-modified XMLDocument-contained XSLT stylesheet.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...