Th3Nic3Guy
Th3Nic3Guy

Reputation: 1979

Process multiple XSLTs and pass the result to another XSLT for transformation in Java

I have a requirement in my code and it is as follows:

The

As far as i have been studied the XSLT variables values cannot be modified once stored. and also that these values are received as parameters when calling <xsl:apply-template name="someTemplate" > and with parameter tags.

i can implement this functionality by retrieving the result into String in Java and again Passing them as parameters to the next template. but i wanted to know if the same can be done directly via XSLT.

Please help

Edit: Xalan removed as tag

Upvotes: 1

Views: 2132

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

i can implement this functionality by retrieving the result into String in Java and again Passing them as parameters to the next template. but i wanted to know if the same can be done directly via XSLT.

Yes, here is an XSLT 2.0 example:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:param name="pP1" select="2"/>
    <xsl:param name="pP2" select="3"/>
    <xsl:param name="pP3" select="5"/>

    <xsl:variable name="vPass1">
     <xsl:apply-templates mode="pass1">
       <xsl:with-param name="pP1" select="$pP1"/>
     </xsl:apply-templates>
    </xsl:variable>

    <xsl:variable name="vPass2">
     <xsl:apply-templates mode="pass2">
       <xsl:with-param name="pP2" select="$pP2"/>
     </xsl:apply-templates>
    </xsl:variable>

    <xsl:variable name="vPass3">
     <xsl:apply-templates mode="pass3">
       <xsl:with-param name="pP3" select="$pP3"/>
     </xsl:apply-templates>
    </xsl:variable>

 <xsl:template match="/">
     <xsl:value-of select="$vPass1 + $vPass2 + $vPass3"/>
 </xsl:template>

 <xsl:template match="/*" mode="pass1">
  <xsl:param name="pP1" as="xs:integer"/>
  <xsl:value-of select="sum(*[. mod $pP1 = 0])"/>
 </xsl:template>

 <xsl:template match="/*" mode="pass2">
  <xsl:param name="pP2" as="xs:integer"/>
  <xsl:value-of select="sum(*[. mod $pP2 = 0])"/>
 </xsl:template>

 <xsl:template match="/*" mode="pass3">
  <xsl:param name="pP3" as="xs:integer"/>
  <xsl:value-of select="sum(*[. mod $pP3 = 0])"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

three passes are performed, each with its own parameters. Each pass computes the sum of num elements, whose value is multiple of the supplied parameter. Finally, the results of the three passes are summed and returned as the final result:

63

Exactly the same transformation can be run with an XSLT 1.0 processor, with the exception that any string "as='xs:integer'" must be removed from the code.

Upvotes: 1

Related Questions