Using <with-param> in <xsl:apply-templates> (and not in <xsl:call-templates/>)

Here should go questions about transforming XML with XSLT and FOP.
stutzmann
Posts: 7
Joined: Fri Mar 29, 2019 12:25 am

Using <with-param> in <xsl:apply-templates> (and not in <xsl:call-templates/>)

Post by stutzmann » Thu Jan 23, 2020 12:43 am

Hello,
I am transforming an XML-TEI document and want to pass on some parameters. It works in <xsl:call-template>, but not in <xsl:apply-templates/> with a @mode.

Oxygen version: <oXygen/> XML Editor 21.1, build 2019120214
Saxon HE 9.8.0.12

the following templates do not

Code: Select all


<xsl:template match="p" mode="add-facs">
 <xsl:variable name="p" select="."/>
 <xsl:variable name="coord" select="someSelection"/>
<<xsl:choose>
            <xsl:when test="pb">
            <xsl:copy>
                    <xsl:apply-templates select="@*"/>
                    <xsl:apply-templates mode="add-facs">
                   	<xsl:with-param name="p" select="$p"></xsl:with-param>
                  	 <xsl:with-param name="coord-toBe-Used" select="$coord"></xsl:with-param>
             	  </xsl:apply-templates>
                </xsl:copy>
                </xsl:when>
            <xsl:otherwise/>
        </xsl:choose>
        </xsl:template>
        
         <xsl:template match="hi" mode="add-facs">
        <xsl:param name="p"></xsl:param>
        <xsl:param name="coord"></xsl:param>
        <testElement>
            <xsl:copy-of select="$p"></xsl:copy-of>
            <xsl:copy-of select="$coord-toBe-Used"></xsl:copy-of>
        </testElement>
    </xsl:template>
    
The template works so far that <hi/> elements are replaced, but the parameters are not passed on, and instead of a copy-of $p, I get only

Code: Select all

<testElement xmlns="" xmlns:tei="http://www.tei-c.org/ns/1.0"> </testElement >
If I have an <xsl:call-template/> with parameters,

Code: Select all

<xsl:template match="p" mode="add-facs">
        <xsl:variable name="p" select="."/>
        <xsl:choose>
            <xsl:when test="pb">
                <xsl:variable name="coord-toBeUsed" select="someSelection"/>
                <xsl:copy>
                    <xsl:call-template name="add-facs">
                   <xsl:with-param name="p" select="$p"></xsl:with-param>
                   <xsl:with-param name="coord" select="$coord"></xsl:with-param>
               </xsl:call-template>
                </xsl:copy>
            </xsl:when>
            <xsl:otherwise/>
        </xsl:choose>
</xsl:template>

  <xsl:template name="add-facs">
        <xsl:param name="p"></xsl:param>
        <xsl:param name="coord-toBe-Used"></xsl:param>
        <testElement>
            <xsl:copy-of select="$p"></xsl:copy-of>
            <xsl:copy-of select="$coord-toBe-Used"></xsl:copy-of>
        </testElement>
    </xsl:template>
I have the right results

Code: Select all

<testElement xmlns="" xmlns:tei="http://www.tei-c.org/ns/1.0">
                  <p xmlns="http://www.tei-c.org/ns/1.0" part="N">
                     <pb n="4r" facs="#BnF_fr_1_zone1"/> [...] </p>
                     [someSelection]
                     </testElement>
                     
Is there a mistake or is it a bug?

Radu
Posts: 7029
Joined: Fri Jul 09, 2004 5:18 pm

Re: Using <with-param> in <xsl:apply-templates> (and not in <xsl:call-templates/>)

Post by Radu » Thu Jan 23, 2020 10:17 am

Hi,

This should work also when you have templates in various modes.
Here's what I'm testing on my side:

XML:

Code: Select all

<TEI xmlns="http://www.tei-c.org/ns/1.0">
  <text>
      <body>
         <p>Some <pb/>
         text <hi>HI THERE</hi>here.</p>
      </body>
  </text>
</TEI>
XSL:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    xpath-default-namespace="http://www.tei-c.org/ns/1.0"
    version="2.0">
    <xsl:template match="/">
        <xsl:apply-templates mode="add-facs"/>
    </xsl:template>
    <xsl:template match="p" mode="add-facs">
        <xsl:variable name="p" select="."/>
        <xsl:variable name="coord" select="someSelection"/>
        <xsl:choose>
            <xsl:when test="pb">
                <xsl:copy>
                    <xsl:apply-templates select="@*"/>
                    <xsl:apply-templates mode="add-facs">
                        <xsl:with-param name="p" select="$p"></xsl:with-param>
                        <xsl:with-param name="coord-toBe-Used" select="$coord"></xsl:with-param>
                    </xsl:apply-templates>
                </xsl:copy>
            </xsl:when>
            <xsl:otherwise/>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template match="hi" mode="add-facs">
        <xsl:param name="p"></xsl:param>
        <xsl:param name="coord"></xsl:param>
        <testElement>
            <xsl:copy-of select="$p"></xsl:copy-of>
            <xsl:copy-of select="$coord"></xsl:copy-of>
        </testElement>
    </xsl:template>
    
</xsl:stylesheet>
You can also use Oxygen's XSLT debugger to get a better idea about how various templates are called.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

stutzmann
Posts: 7
Joined: Fri Mar 29, 2019 12:25 am

Re: Using <with-param> in <xsl:apply-templates> (and not in <xsl:call-templates/>)

Post by stutzmann » Fri Jan 24, 2020 10:09 am

Thank you, Radu, for your reply! I will work along the lines, trying to have the most common processing and addressing the root.
:-D

Post Reply