Kita Ansari
Kita Ansari

Reputation: 121

Two XSL files need single final output file

I have a input file and two XSL files test1.xsl, test2.xsl

  1. test1.xsl file transforming on input file and generating test1-output.xml
  2. test2.xsl file transforming on test1-output.xml and generating the final output

Requirement:

I am using XSLT 2.0 I want run at a time both xslt files transform on input file and generate the final output. How to do this requirement, Please suggest!

Input file:

<?xml version="1.0" encoding="UTF-8"?>
<k>
    <page>content here</page>
    <page1>content here</page1>
</k>

test1.xsl file:

<?xml version="1.0"qqq encoding="UTF-8"?>
<xsl:stylesheet xmqlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output indent="yes" method="xml"/>
    <xsl:template match="@*| node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
<xsl:template match="page">
        <page>
            <kita>
                <xsl:apply-templates/>
            </kita>
        </page>
    </xsl:template>
</xsl:stylesheet>

output of test1.xsl

<?xml version="1.0" encoding="UTF-8"?>
<k>
    <page>
      <kita>content here</kita>
   </page>
    <page1>content here</page1>
</k>

test2.xsl

<?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"
    version="2.0">
    <xsl:output indent="yes" method="xml"/>
    <xsl:template match="@*| node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="kita">
        <kita>
            <kita1>
                <xsl:apply-templates/>
            </kita1>
        </kita>
    </xsl:template>
</xsl:stylesheet>

This is my final xml output from the 2nd XSLT file:

<?xml version="1.0" encoding="UTF-8"?>
<k>
    <page>
      <kita>
         <kita1>content here</kita1>
      </kita>
   </page>
    <page1>content here</page1>
</k>

Thanks in advance

Upvotes: 1

Views: 39

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167571

In XSLT 3 you could write a stylesheet with two parameters calling fn:transform twice e.g.

<xsl:param name="xslt1" as="document-node()"/>
<xsl:param name="xslt2" as="document-node()"/>

<xsl:template match="/">
  <xsl:sequence
    select="
    transform(map {
      'stylesheet-node' : $xslt1,
      'source-node' : .
    })?output ! transform(map {
      'stylesheet-node' : $xslt2,
      'source-node' : .
    })?output"/>
</xsl:template>

or if you want to pass in the file URIs

<xsl:param name="xslt1" as="xs:string">test1.xsl</xsl:param>
<xsl:param name="xslt2" as="xs:string">test2.xsl</xsl:param>

<xsl:template match="/">
  <xsl:sequence
    select="
    transform(map {
      'stylesheet-node' : doc($xslt1),
      'source-node' : .
    })?output ! transform(map {
      'stylesheet-node' : doc($xslt2),
      'source-node' : .
    })?output"/>
</xsl:template>

Upvotes: 1

Related Questions