XSLT calling template based on the condition - xslt

I have a problem in my real-time XSLT files. Based on that, i am putting my question here. I have 3 xslt files such as 1.xsl and master.xsl. This master.xsl is imported into 1.xsl
On the master.xsl, i am using this below code
<xsl:call-template name="content">
<xsl:with-param name="request" select="$request"/>
<xsl:call-template>
Like wise, on the 1.xsl,
<xsl:template name="content">
<xsl:param name="request" as="node()"/>
....
</xsl:template>
In this case, on the file 1.xsl, some time, for the template 'content', the parameter request, there wont be passed. it will be passed in some time.
so, the above template will be(without parameter 'request') in some cases
<xsl:template name="content">
....
</xsl:template>
when there is no parameter, this is showing error as of now
XTSE0680: Parameter request is not declared in the called template
so, in this case, kindly give me some ideas to modify the coding on master.xsl

The reasons for the error message have been pointed out in the answer to XSLT calling template with xsl:with-param on different template. You have to modify the template to declare the parameter. Or you would need to change the code in master.xsl to only pass the parameter with e.g.
<xsl:choose>
<xsl:when test="$request">
<xsl:call-template name="content">
<xsl:with-param name="request" select="$request"/>
<xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="content"/>
</xsl:otherwise>
</xsl:choose>
That will only pass in $request if it is a non-empty sequence. Of course if your code is included in a stylesheet where the template does declare the parameter and the variable $request is not empty you will continue to experience the error. There is no way to check at run-time whether the template expects a param or not.

Related

Issue: SVG <use> Element is Converted to String in XSL Template

I'm fairly new to XSL, XPATH etc. Some of this code is mine, some are someone else's.
Problem: When the template below gets called with the templates I've outlined further below, all the xsl:text nodes after the if test is output as a string instead of an HTML node, and thus the icon is not rendered.
This question has to do with understanding the why? of what's going on. My exact question is at the bottom of this post.
So, I have a template that I call that generates SVG elements with a <use> element for use with an SVG sprite.
<xsl:template name="svg-link">
<xsl:param name="svg-id"/>
<xsl:param name="svg-class"/>
<xsl:param name="svg-title"/>
<span class="{$svg-class} svgstore svgstore--{$svg-loc}">
<svg>
<xsl:if test="$svg-title != ''">
<title><xsl:value-of select="$svg-title"/></title>
</xsl:if>
<xsl:text disable-output-escaping="yes"><use xlink:href="</xsl:text>
<xsl:value-of select="concat('#', $svg-loc)" />
<xsl:text disable-output-escaping="yes">"></use></xsl:text>
</svg>
</span>
</xsl:template>
All sorts of templates call/apply this template. There is one in particular that I'm having an issue with. We have two snippets implemented by the CMS that output the same markup, but the configurations for the snippets are implemented differently, i.e. Page Template A vs Page Template B. The snippet in question is made of multiple XSL templates. The templates are organized like so:
Snippet Template: entry point for snippet for all callers. Accepts a couple of params related to CSS classes. Creates a few wrapper elements for the snippet. Calls the following template.
"Model" Template: is a template that needs to be defined by each page template. As mentioned above, each page template uses a different approach to implementing configuration options for the snippet. The idea is to make the following template agnostic about how the snippet was configured in the first place because this template is responsible for knowing those details and passing it on to the following template.
Snippet Item Template: renders most of the markup for the snippet based on the info passed to it by the "Model" Template.
Here's some simplified pseudo-code demonstrating above:
<xsl:template name="snippet">
<xsl:param name="outer-classes"/>
<xsl:param name="inner-classes"/>
<xsl:variable name="items">
<xsl:call-template name="snippet-model"/>
</xsl:variable>
<!-- Render Snippet if it has content. -->
<xsl:if test="count( $items )">
<div class="{ $outer-classes }">
<div class="{ $inner-classes }">
<xsl:copy-of select="$items">
</div>
</div>
</xsl:if>
</xsl:template>
<!-- Placeholder. Defined by each page template. -->
<xsl:template name="snippet-model"/>
<xsl:template name="snippet-item">
<xsl:param name="a"/>
<xsl:param name="b"/>
<xsl:param name="b"/>
<div class="snippet-item {$a}">
<xsl:apply-templates select="$b"/>
<xsl:call-template name="svg-link">
<xsl:with-param name="svg-id">alpha</xsl:with-param>
<xsl:with-param name="svg-class">alpha</xsl:with-param>
<xsl:with-param name="svg-title">The Title</xsl:with-param>
</xsl:call-template>
</div>
</xsl:template>
And an example of how a page template uses the above:
<xsl:template match="table[#class = 'snippet-alpha']">
<xsl:call-template="snippet">
<xsl:with-param name="outer-classes">page-template-a other</xsl:with-param>
<xsl:with-param name="inner-classes">some-template-modifier</xsl:with-param>
</xsl:call-template>
</xsl:template>
<!-- Template definition of `snippet-model` template. -->
<xsl:template name="snippet-model">
<!-- Another page template might not use `tbody/tr` to loop over. -->
<xsl:for-each select="tbody/tr">
<xsl:call-template="snippet-item">
<xsl:with-param name="a" select="td[1]"/>
<xsl:with-param name="b" select="td[2]"/>
<xsl:with-param name="c" select="td[3]"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
I've narrowed down my issue to likely be the xsl:variable capturing the results of xsl:call-template in the snippet template. And/Or the referencing of that variable later with xsl:copy-of.
What Have I Tried?
Below I have working and non-working solutions, all of which I do not fully grokk why they may or may not work.
Works: Adding xmlns:xlink="http://www.w3.org/1999/xlink" to xsl:stylesheet for the file that contains the svg-link template and then modding svg-link template, see code below this list.
Works: Instead of outputting the value of the xsl:variable that captures the results of xsl:call-template with xsl:copy-of. I replace xsl:copy-of with a second xsl:call-template that is identical to that of the call that was done inside the variable.
Does Not Work: Used xsl:sequence instead of xsl:copy-of.
Does Not Work: Tried data typing(?) the xsl:variable that captures the results of xsl:call-template with the as attribute. I.e. as="node()*".
<xsl:template name="svg-link">
<xsl:param name="svg-id"/>
<xsl:param name="svg-class"/>
<xsl:param name="svg-title"/>
<span class="{$svg-class} svgstore svgstore--{$svg-loc}">
<svg>
<xsl:if test="$svg-title != ''">
<title><xsl:value-of select="$svg-title"/></title>
</xsl:if>
<use xlink:href="{concat( '#', $svg-loc )}"></use>
</svg>
</span>
</xsl:template>
Question: Why are some of the contents of the svg-link template being output as a string (instead of HTML) based on how the result of a call to xsl:call-template is captured/called? As you can see, I have working and non-working solutions - I would like to know why. Thanks!
First of all, disable-output-escaping is an optional serialization feature. Additionally, the XSLT 2 or 3 specs spell out when it doesn't work at all, see https://www.w3.org/TR/xslt-30/#disable-output-escaping
If output escaping is disabled for an xsl:value-of or xsl:text
instruction evaluated when temporary output state is in effect, the
request to disable output escaping is ignored.
and https://www.w3.org/TR/xslt-30/#dt-temporary-output-state
xsl:variable, xsl:param, xsl:with-param, xsl:function, xsl:key,
xsl:sort, xsl:accumulator-rule, and xsl:merge-key always evaluate the
instructions in their contained sequence constructor in temporary
output state
So inside your xsl:variable any disable-output-escaping can't work.
The whole attempt to use it to try to construct an SVG use element is completely unnecessary, you can create any result elements as literal result elements e.g. <use xlink:href="{concat( '#', $svg-loc )}"></use> (with an appropriate XLink namespace declaration in scope for the attribute from that namespace), or, if you need to compute part of the name or namespace, using xsl:element.

Passing an local varaible from one template to another template in xslt

I am trying to covert an xml to html using xslt.
Now I got struck with the use of variables in xslt.
Is it possible to pass variables from parent node to some other template other than child node?
I have amy xslt code like this
`
<xsl:template match="xpath"name="a">
<xsl:variable name="object" select="Hello">
<xsl:call-template="b">
<xsl:with-param name="object" select="$object"/>
</xsl:call-template>
</xsl:variable>
</xsl:template>
<xsl:template match="xpath" name="b">
<xsl:param name="object"/>
</xsl:template>
`
I am getting an following error
unexpected xslt element 'param'
Help me to resolve this issue.
There are multiple issues with the code that has been shared.
XSL template cannot have both match and name attributes specified. You can either have a match attribute or name attribute. The match attribute can go with the mode attribute if you are looking to match with same element from input XML.
XSL variable object has been declared and a value of Hello assigned to it. However you are also calling a template b from inside the <xsl:variable> which is not correct. XSL variables cannot have select and content both.
You may want to modify the code as below if you are looking for passing a parameter from one template to another.
<!-- template matching with input XML -->
<xsl:template match="xpath">
<!-- declare variable "object" and assign value as "Hello" -->
<xsl:variable name="object" select="'Hello'" />
<!-- call template "b" and pass the value of variable "object" -->
<xsl:call-template name="b">
<xsl:with-param name="object" select="$object" />
</xsl:call-template>
</xsl:template>
<!-- create template "b" using #name attribute -->
<xsl:template name="b">
<!-- declare parameter -->
<xsl:param name="object" />
<!-- print the value of parameter -->
<xsl:value-of select="$object" />
</xsl:template>

xslt pass parameters throuh all apply-template calls

In a rather complex xslt file some elements are to be processed twice. This is done by a template with a paramater.
<xsl:template macht="x">
<xsl:param name="modus"/>
<!-- comon things to do for both cases -->
<xsl:choose>
<xsl:when test="$modus='case1'"> <!-- things to do in case 1 --> </xsl:when>
<xsl:when test="$modus='case2'"> <!-- things to do in case 2 --> </xsl:when>
</xsl:choose>
</xsl:template>
The problem is: I cannot simply apply or call this template directly. The element x (for which this template with these two cases is for) is often at a quite low level of the xml input. Almost all ancestor elements have to be processed (in both cases) before it actually comes to x.
The call for the two cases is at the allmost top level.
In html it would be like this
<body>
<h1>Case 1</h1>
<xsl:apply-templates><xsl:with-parameter name="modus" select="case1"/>
<h1>Case 2</h1>
<xsl:apply-templates><xsl:with-parameter name="modus" select="case2"/>
</body>
So. How can I make sure, that the parameter reaches the template for x?
Of course, I could replace all
<xsl:apply-templates/>
calls within the templates for every single ancestor element of x by
<xsl:param name="modus">
<!-- What ever content here -->
<xsl:apply-templates><xsl:with-parameter name="modus" select="$modus"/></apply-templates>
But that would mean a lot of effort. Is there a better way to do this?
XSLT 2.0 has tunnel parameters e.g. with
<xsl:apply-templates>
<xsl:with-param name="modus" tunnel="yes" select="'foo'"/>
</xsl:apply-templates>
and
<xsl:template match="bar">
<xsl:param name="modus" tunnel="yes"/>
...
</xsl:template>
you don't have to pass on the parameter explicitly in the templates for ancestors of bar. So using an XSLT 2.0 processor like Saxon 9 you can do that.

xslt any way to get the name of calling template from within a template

I am very new to XSLT. I was wondering if there is any way to get the name of calling template from within a template.
I currently got the following with a little complex structure. One template is included once directly and once via another template. I need to add a new tag to this template only if it is called from a specific template.
<xsl:element name="parent">
<xsl:choose>
<xsl:when test="$myVariable = 'process1'">
<xsl:call-template name="templateA"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="templateB"/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
<xsl:template name="templateA">
<!-- Some Other Tags Here -->
<xsl:call-template name="templateB />"
</xsl:template>
<xsl:template name="templateb"> <!-- very big template -->
<!-- existing tags here -->
<!-- Add a new tag here only if called via templateA -->
</xsl:template>
To be clear,
As you can see, templateB is included either way, but templateA adds some more tags then includes templateB.
I want to add a new tag to templateB only if it is called from templateA. Is it possible to do?
You could use parameter
<xsl:template name="templateB"> <!-- very big template -->
<xsl:param name="calledFrom" select="" />
<!-- existing tags here -->
<xsl:if test="$calledFrom = 'templateA">
<!-- Add a new tag here only if called via templateA -->
</xsl:if>
</xsl:template>
And then called it in this way
<xsl:call-template name="templateB">
<xsl:with-param name="calledFrom" select="'templateA'" />
</xsl:call-template>
If a function/template needs to know where it was called from, then there's something wrong with the design. Passing a parameter is of course the immediate way to fix the code, but piling on parameters and adding conditional logic based on the parameter values leads to unmaintainable spaghetti.
There's not enough of your code here to assess the design, but I would ask why it's not making more use of template rules rather than named templates. It might well be that judicious use of apply-templates would solve the problem much more naturally.
Passing the parameter is the solution, I was not aware of if they are passed in nested templates.
The solution that proper suits my scenario is tunnel-params.
Parameters are tunneled(passed on ) to template called by default in xslt 2.0, but in xslt 1.0 we need to specify tunnel="yes". With tunelling myVariable can be accessible to the template called.

passing parameter to parsing of imported file

In parsing a WSDL, I come across many wsdl:import and xsd:import elements. I would like to parse the imports and pass the #location or #schemaLocation to the parser.
The intent is to have the file list grow when an imported file imports a file for example filea.wsdl;filez.xsd;filev.xsd. This way I can eliminate a previously imported file.
I would think something along along these lines:
<xsl:param name="file-list"/>
<xsl:template match="/">
<xsl:param name="file-list"/>
<xsl:apply-templates />
</xsl:template>
<xsl:template match="wsdl:import">
<xsl:apply-templates select="document(#location)">
<xsl:with-param name="file-list" select="concat($file-list, ';', #location)`"/>
</xsl:apply-templates>
</xsl:template>
Your basic idea seems to be fine. You just need to pass the the file-list parameter along when applying templates, so:
add a <xsl:with-param name="file-list" value="$file-list"/> to the xsl:apply-templates in your first template to actually pass the parameter, and
add a <xsl:param name="file-list"/> to your second template to introduce the parameter there.