Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 5th, 2008, 06:46 PM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rushman
Default Dynamic xsl:include?

Helloooooo there!

I wonder if the following is possible: can I dynamically set the href attribute of an xsl:include?

Let's say we have our original XML

<nothing/>

then we have two xsl that have the same matching template:

XSL-1 is

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template name="root">
    <root1/>
  </xsl:template>
</xsl:stylesheet>

XSL-2 is

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template name="root">
    <root2/>
  </xsl:template>
</xsl:stylesheet>

then we have the final XSL that will be applied to our original XML

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="stylesheet"/>
  <xsl:include href="{$stylesheet}"/>
  <xsl:template match="/">
    <xsl:call-template name="root"/>
  </xsl:template>
</xsl:stylesheet>

If I set the transformer's parameter "stylesheet" to "XSL-1.xsl", my output should look like:

<root1/>

If I set the transformer's parameter "stylesheet" to "XSL-2.xsl", my output should look like:

<root2/>

Altova's XMLSpy only tells me that:
 XSL transformation failed
 Could not include external entity

This message gives me hope that there's a way to do it, but I'm just wrong somewhere.

Thank you for your help,

Rushman
__________________
Dijkstra's law on Programming and Inertia:

If you don't know what your program is supposed to do, don't try to write it.
 
Old March 6th, 2008, 03:33 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Attributes of top level templates cannot use attribute value templates (i.e. href="{...}")

Surely however if you have the ability to pass in a different parameter you can just call a different stylesheet?

If you have common elements to both stylsheets then put them in another stylesheet and then inlude that in stylesheet 1 and 2.

An alternative if you are using Saxon might be to use saxon:call-template, where the name of the template can be an attribute value template and hence determined at runtime.

/- Sam Judson : Wrox Technical Editor -/
 
Old March 6th, 2008, 04:15 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's surprising how often people ask for this. But it doesn't make sense. Like all other programming languages invented in the last 40 years or so, an XSLT program cannot modify itself at run-time. Changing the href of an xsl:include would be like changing the name of a variable or function. Includes happen at compile-time, long before the source document or the values of variables are available.

The answer to your use-case is that xsl-1 and xsl-2 should both include common.xsl, and then instead of setting a parameter to decide which version of the code to use, you invoke xsl-1 or xsl-2 as the entry point.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 27th, 2009, 06:23 AM
moo moo is offline
Registered User
 
Join Date: Jan 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

10K years ago humans used to live in caves, does that make perfect sense?

Lack of dynamic includes is just another entry in list "XSL is not as cool as intended and not suitable for problem X"

Sorry for trolling, just got stuck with my uber-dynamic data processing with xslt idea, grrrrr!!
 
Old January 27th, 2009, 06:35 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Actually, in the very early days of computer programming languages, programs were allowed to modify themselves at run-time. But people soon realized this was a really bad idea, and no modern languages allow it.

Now tell us your requirement, and we'll tell you why you don't need this "feature".
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old October 16th, 2012, 04:33 AM
Registered User
 
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default requirement

I am using a style-sheet that produces xsl-fo.
I managed to put all paper layout - frontpage, right-/left-page, header, footer, table of contents - in a file that I draw by <xsl:include href="include/paper.xsl"/>.

Now - you may imagine - I want to produce the same information on different paper layouts.

The idea is to have a style-sheet parameter that determines the name of the include file.
Default would be 'include/paper.xsl' but I may pass a parameter 'include/paper1.xsl' to the style-sheet in order to change the paper layout.

What is the better solution than asking for a dynamic include?
 
Old October 16th, 2012, 05:11 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Depending on how you are calling your main XSLT file you could use a URL resolver to resolve the include href dynamically.

Depending on the complexity of the include stylesheet and how much control you have you could have different modes, and then call the correct mode depending on the parameter.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old October 16th, 2012, 05:26 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Stylesheet parameters are only usable at run-time - you want something that controls how the stylesheet is built at compile time. If you're using XSLT 2.0 you could use xsl:use-when (perhaps with system-property(), though the way of setting system properties is processor-dependent). Alternatively, do a transformation to modify the stylesheet source before compiling it.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl import/include mapping sammyj727 XSLT 3 July 19th, 2007 04:15 PM
xsl:include / import and tomcat amitda XSLT 2 April 17th, 2007 10:37 AM
how to include jsp page in xsl vijayanmsc XSLT 2 August 23rd, 2006 05:04 AM
xsl:include within include chuck123ie XSLT 1 January 5th, 2006 11:07 AM
xsl:import or xsl:include ShaileshShinde XSLT 2 August 17th, 2005 04:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.