Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Import stylesheets in xslt conditionally?

Tags:

xslt

xslt-2.0

Is there any way to import stylesheets after checking some conditions?

Like,if the value of variable $a="1" then import 1.xsl or else import 2.xsl.

like image 730
Senthil Avatar asked Dec 17 '10 15:12

Senthil


People also ask

Can we use JavaScript in XSLT?

JavaScript can run XSLT transformations through the XSLTProcessor object. Once instantiated, an XSLTProcessor has an XSLTProcessor. importStylesheet() method that takes as an argument the XSLT stylesheet to be used in the transformation.

Can XSLT transform XML to CSV?

The following XSL Style Sheet (compatible with XSLT 1.0) can be used to transform the XML into CSV. It is quite generic and can easily be configured to handle different xml elements by changing the list of fields defined ar the beginning.

Can you use CSS with XSLT?

An XSLT style sheet can emit HTML <STYLE> elements, including CSS specifications, directly into the HTML that results from the XSLT transformation. This option works best when the number of CSS rules is small and easily managed.

Can XSLT transform XML to JSON?

XSLTJSON: Transforming XML to JSON using XSLTXSLTJSON is an XSLT 2.0 stylesheet to transform arbitrary XML to JavaScript Object Notation (JSON). JSON is a lightweight data-interchange format based on a subset of the JavaScript language, and often offered as an alternative to XML in—for example—web services.


1 Answers

Hi All, Is there any way to import stylesheets after checking some conditions?

Like,if the value of variable $a="1" then import 1.xsl or else import 2.xsl.

No, the <xsl:import> directive is only compile-time.

In XSLT 2.0 one can use the use-when attribute for a limited conditional compilation.

For example:

<xsl:import href="module-A.xsl" 
     use-when="system-property('xsl:vendor')='vendor-A'"/>

The limitations of the use-when attribute are that there is no dynamic context when the attribute is evaluated -- in particular that means that there are no in-scope variables defined.

A non-XSLT solution is to dynamically change the href attribute of the <xsl:import> declaration before the transformation is invoked:

  1. Parse the xsl stylesheet as an XML file

  2. Evaluate the condition that determines which stylesheet should be imported.

  3. Set the value of the href attribute of the <xsl:import> declaration to the URI of the dynamically determined stylesheet-to-be-imported.

  4. Invoke the transformation with the in-memory xsl stylesheet that was just modified.

like image 56
Dimitre Novatchev Avatar answered Oct 09 '22 08:10

Dimitre Novatchev