Unable to pass parameter when chaining stylesheets

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Unable to pass parameter when chaining stylesheets

Rudolf de Grijs

Is it possible to pass a parameter to a chained stylesheet?

 

I’ve created the following simple test case

 

xslt1 / not interested in the input to make it as simple as possible!

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:param name="contextPath"></xsl:param>

    <xsl:output method="xml"/>

 

    <xsl:template match="/">

        <first contextPath="{$contextPath}">

            First xslt template ..

        </first>

    </xsl:template>

 

</xsl:stylesheet>

 

xslt2

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

   <xsl:param name="contextPath"></xsl:param>

   <xsl:output method="xml"/>

 

    <xsl:template match="/">

        <second contextPath="{$contextPath}">

            <xsl:copy>

                <xsl:apply-templates/>

            </xsl:copy>   

        </second>

    </xsl:template>

   

    <xsl:template match="@* | node()">

        <xsl:copy>

                   <xsl:apply-templates select="@* | node()" />

                </xsl:copy>       

    </xsl:template>

 

</xsl:stylesheet>

 

Scenario 1 – run against 1 stylesheet

/** stf is SaxonTransformerFactory */

public void transformOne() throws TransformerConfigurationException, TransformerException {

 

        Transformer t = stf.newTransformer(loadSource("xslt1.xsl"));

       

       

        t.setParameter("contextPath", "/x/y/z");

        t.transform(loadSource("users.xml"), new StreamResult(System.out));

}

 

Result (which is ok!):

<?xml version="1.0" encoding="UTF-8"?><first contextPath="/x/y/z">

            First xslt template ..

</first>

 

And now chaining the two stylesheets

  public void transform() throws TransformerConfigurationException, TransformerException {

        TransformerHandler th1 = stf.newTransformerHandler(loadSource("xslt1.xsl"));

        TransformerHandler th2 = stf.newTransformerHandler(loadSource("xslt2.xsl"));

   

 

        /* setup XSLT pipeline */

        th1.setResult(new SAXResult(th2));

        th2.setResult(new StreamResult(System.out));

        Transformer t = stf.newTransformer();

       

        t.setParameter("contextPath", "/x/y/z");

        t.transform(loadSource("users.xml"), new SAXResult(th1));

    }

 

Result (can’t get access to the parameter!):

<?xml version="1.0" encoding="UTF-8"?>

<second contextPath="">

<first contextPath="">

                First xslt template ..

        </first>

</second>

 

Any help would be appreciated.

 

Kind regards,

Ruud de Grijs

ing. Rudolf de Grijs
Consultant

Contactgegevens
Jan Leentvaarlaan 21 - 3065 DC Rotterdam

T. +31(0)10 235 10 57 E. [hidden email]
M. +31(0)6 476 51 370 W. www.inovita.nl



BTW Nummer: NL 8091.30.198.B.01 - KvK: 24305459

De informatie in dit e-mailbericht is vertrouwelijk en uitsluitend bestemd voor de geadresseerde. Wanneer u dit bericht per abuis ontvangt, verzoeken wij u contact op te nemen met de afzender per kerende e-mail. Verder verzoeken wij u in dat geval dit e-mailbericht te vernietigen en de inhoud ervan aan niemand openbaar te maken. Wij aanvaarden geen aansprakelijkheid voor onjuiste, onvolledige dan wel ontijdige overbrenging van de inhoud van een verzonden e-mailbericht, noch voor daarbij overgebrachte virussen.

------------------------------------------------------------------------------

_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|

Re: Unable to pass parameter when chaining stylesheets

cmarchand
Parameters are XSL scoped. That means, you have to define the parameter value for each TransformerHandler you have in your pipe, if they need it - you can give the parameter, either it's not expected.

I use such pipes, that chains the same XSL, but with different values for the same parameter, at each step of the pipe. Something like

<pipe>
  <xslt href="step.xsl">
    <param name="p" value="step1"/>
  </xslt>
  <xslt href="step.xsl">
    <param name="p" value="step2"/>
  </xslt>
</pipe>

This constructs a pipe, where source.xml is transformed by step.xsl with p="step1", then by step.xsl with p="step2"

Hope this helps,
Christophe

Le 15/10/2015 14:23, Rudolf de Grijs a écrit :

Is it possible to pass a parameter to a chained stylesheet?

 

I’ve created the following simple test case

 

xslt1 / not interested in the input to make it as simple as possible!

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:param name="contextPath"></xsl:param>

    <xsl:output method="xml"/>

 

    <xsl:template match="/">

        <first contextPath="{$contextPath}">

            First xslt template ..

        </first>

    </xsl:template>

 

</xsl:stylesheet>

 

xslt2

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

   <xsl:param name="contextPath"></xsl:param>

   <xsl:output method="xml"/>

 

    <xsl:template match="/">

        <second contextPath="{$contextPath}">

            <xsl:copy>

                <xsl:apply-templates/>

            </xsl:copy>   

        </second>

    </xsl:template>

   

    <xsl:template match="@* | node()">

        <xsl:copy>

                   <xsl:apply-templates select="@* | node()" />

                </xsl:copy>       

    </xsl:template>

 

</xsl:stylesheet>

 

Scenario 1 – run against 1 stylesheet

/** stf is SaxonTransformerFactory */

public void transformOne() throws TransformerConfigurationException, TransformerException {

 

        Transformer t = stf.newTransformer(loadSource("xslt1.xsl"));

       

       

        t.setParameter("contextPath", "/x/y/z");

        t.transform(loadSource("users.xml"), new StreamResult(System.out));

}

 

Result (which is ok!):

<?xml version="1.0" encoding="UTF-8"?><first contextPath="/x/y/z">

            First xslt template ..

</first>

 

And now chaining the two stylesheets

  public void transform() throws TransformerConfigurationException, TransformerException {

        TransformerHandler th1 = stf.newTransformerHandler(loadSource("xslt1.xsl"));

        TransformerHandler th2 = stf.newTransformerHandler(loadSource("xslt2.xsl"));

   

 

        /* setup XSLT pipeline */

        th1.setResult(new SAXResult(th2));

        th2.setResult(new StreamResult(System.out));

        Transformer t = stf.newTransformer();

       

        t.setParameter("contextPath", "/x/y/z");

        t.transform(loadSource("users.xml"), new SAXResult(th1));

    }

 

Result (can’t get access to the parameter!):

<?xml version="1.0" encoding="UTF-8"?>

<second contextPath="">

<first contextPath="">

                First xslt template ..

        </first>

</second>

 

Any help would be appreciated.

 

Kind regards,

Ruud de Grijs

ing. Rudolf de Grijs
Consultant

Contactgegevens
Jan Leentvaarlaan 21 - 3065 DC Rotterdam

T. +31(0)10 235 10 57 E. [hidden email]
M. +31(0)6 476 51 370 W. www.inovita.nl



BTW Nummer: NL 8091.30.198.B.01 - KvK: 24305459

De informatie in dit e-mailbericht is vertrouwelijk en uitsluitend bestemd voor de geadresseerde. Wanneer u dit bericht per abuis ontvangt, verzoeken wij u contact op te nemen met de afzender per kerende e-mail. Verder verzoeken wij u in dat geval dit e-mailbericht te vernietigen en de inhoud ervan aan niemand openbaar te maken. Wij aanvaarden geen aansprakelijkheid voor onjuiste, onvolledige dan wel ontijdige overbrenging van de inhoud van een verzonden e-mailbericht, noch voor daarbij overgebrachte virussen.


------------------------------------------------------------------------------


_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 


------------------------------------------------------------------------------

_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|

Re: Unable to pass parameter when chaining stylesheets

Michael Kay
In reply to this post by Rudolf de Grijs
Have you tried th1.getTransformer().setParameter()?

Alternatively, use the s9api interface instead of JAXP…

Michael Kay
Saxonica

On 15 Oct 2015, at 13:23, Rudolf de Grijs <[hidden email]> wrote:

Is it possible to pass a parameter to a chained stylesheet?

 

I’ve created the following simple test case

 

xslt1 / not interested in the input to make it as simple as possible!

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:param name="contextPath"></xsl:param>

    <xsl:output method="xml"/>

 

    <xsl:template match="/">

        <first contextPath="{$contextPath}">

            First xslt template ..

        </first>

    </xsl:template>

 

</xsl:stylesheet>

 

xslt2

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

   <xsl:param name="contextPath"></xsl:param>

   <xsl:output method="xml"/>

 

    <xsl:template match="/">

        <second contextPath="{$contextPath}">

            <xsl:copy>

                <xsl:apply-templates/>

            </xsl:copy>   

        </second>

    </xsl:template>

   

    <xsl:template match="@* | node()">

        <xsl:copy>

                   <xsl:apply-templates select="@* | node()" />

                </xsl:copy>       

    </xsl:template>

 

</xsl:stylesheet>

 

Scenario 1 – run against 1 stylesheet

/** stf is SaxonTransformerFactory */

public void transformOne() throws TransformerConfigurationException, TransformerException {

 

        Transformer t = stf.newTransformer(loadSource("xslt1.xsl"));

       

       

        t.setParameter("contextPath", "/x/y/z");

        t.transform(loadSource("users.xml"), new StreamResult(System.out));

}

 

Result (which is ok!):

<?xml version="1.0" encoding="UTF-8"?><first contextPath="/x/y/z">

            First xslt template ..

</first>

 

And now chaining the two stylesheets

  public void transform() throws TransformerConfigurationException, TransformerException {

        TransformerHandler th1 = stf.newTransformerHandler(loadSource("xslt1.xsl"));

        TransformerHandler th2 = stf.newTransformerHandler(loadSource("xslt2.xsl"));

   

 

        /* setup XSLT pipeline */

        th1.setResult(new SAXResult(th2));

        th2.setResult(new StreamResult(System.out));

        Transformer t = stf.newTransformer();

       

        t.setParameter("contextPath", "/x/y/z");

        t.transform(loadSource("users.xml"), new SAXResult(th1));

    }

 

Result (can’t get access to the parameter!):

<?xml version="1.0" encoding="UTF-8"?>

<second contextPath="">

<first contextPath="">

                First xslt template ..

        </first>

</second>

 

Any help would be appreciated.

 

Kind regards,

Ruud de Grijs

ing. Rudolf de Grijs
Consultant

Contactgegevens
Jan Leentvaarlaan 21 - 3065 DC Rotterdam

T. +31(0)10 235 10 57 E. [hidden email]
M. +31(0)6 476 51 370 W. www.inovita.nl



BTW Nummer: NL 8091.30.198.B.01 - KvK: 24305459

De informatie in dit e-mailbericht is vertrouwelijk en uitsluitend bestemd voor de geadresseerde. Wanneer u dit bericht per abuis ontvangt, verzoeken wij u contact op te nemen met de afzender per kerende e-mail. Verder verzoeken wij u in dat geval dit e-mailbericht te vernietigen en de inhoud ervan aan niemand openbaar te maken. Wij aanvaarden geen aansprakelijkheid voor onjuiste, onvolledige dan wel ontijdige overbrenging van de inhoud van een verzonden e-mailbericht, noch voor daarbij overgebrachte virussen.
------------------------------------------------------------------------------
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help


------------------------------------------------------------------------------

_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help