Search code examples
xmlxsltxslt-2.0

How to include an XSL file into another XSL file


I need that 2.xsl file use function for changing date format (tag <Date>) from file 1.xsl. I use instruction <xsl:include> but I don't know what I should to change.

Here is my XML file:

<?xml version="1.0" encoding="UTF-8"?>
<ClientList>
    <Client>
        <Name>Jan</Name>
        <Surname>Kowalski</Surname>
        <Date>2018-03-23</Date>
    </Client>
    <Client>
        <Name>Piotr</Name>
        <Surname>Nowak</Surname>
        <Date>2018-04-25</Date>
    </Client>
</ClientList>

My 1.xsl file with function that changes date format:

<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <Date>
        <xsl:value-of select="concat(substring(Date, 9, 2), '-', substring(Date, 6, 2), '-', substring(Date, 1, 4))"/>
    </Date>
</xsl:stylesheet>

Here is my 2.xsl file (I need that this file use function from 1.xsl):

<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:include href="1.xsl"/>

    <xsl:template match="/">
        <ClientList>
            <xsl:for-each select="ClientList/Client">
                <Client>
                    <NameSurname>
                        <xsl:value-of select="concat(Name, ' ' , Surname)"/>
                    </NameSurname>
                </Client>
            </xsl:for-each>
        </ClientList>
    </xsl:template>
</xsl:stylesheet>

Solution

  • There are couple of issues with the XSLs created and rightly pointed out by @Martin Honnen in his comment related to 1.xsl.

    The <Date> format conversion logic needs to be wrapped inside an <xsl:template> or <xsl:function>. Below is the updated 1.xsl that uses <xsl:template>.

    <xsl:template name="DateConvertor">
        <Date>
            <xsl:value-of select="concat(substring(Date, 9, 2), '-', substring(Date, 6, 2), '-', substring(Date, 1, 4))"/>
        </Date>
    </xsl:template>
    

    In 2.xsl, you are including utils.xsl which I believe corresponds to 1.xsl. In this XSL, you need to invoke the template or function that you have created in the included XSL.

    <xsl:template match="/">
        <ClientList>
            <xsl:for-each select="ClientList/Client">
                <Client>
                    <NameSurname>
                        <xsl:value-of select="concat(Name, ' ', Surname)" />
                    </NameSurname>
                    <xsl:call-template name="DateConvertor" /> <!-- call the template here -->
                </Client>
            </xsl:for-each>
        </ClientList>
    </xsl:template>
    

    EDIT - Changes for using <xsl:function>

    XSLT 2.0 allows writing of own functions using <xsl:function>. Functions can be used for performing any computational logic and they return values based on the computations. Parameters can be passed to the function using <xsl:param>. Functions have to be associated to a namespace which is different than the XSLT namespace.

    The 1.xsl will need to be modified to include a function which accepts a single parameter of type string and returns a value which is of type string.

    <xsl:function name="ex:DateConvertor" as="xs:string">
        <xsl:param name="InputDate" as="xs:string" />
        <xsl:value-of select="concat(substring($InputDate, 9, 2), '-', substring($InputDate, 6, 2), '-', substring($InputDate, 1, 4))"/>
    </xsl:function>
    

    This function is associated with namespace xmlns:ex="http://canbeanything". Additionally since the datatypes are being used for the parameter and return value, you need to define namespace xmlns:xs="http://www.w3.org/2001/XMLSchema" also.

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:ex="http://canbeanything">
    

    Changes in 2.xsl to invoke the function ex:DateConvertor.

    Map the namespace necessary for the function.

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

    Call Function and pass Date value as a parameter

    <xsl:template match="/">
        <ClientList>
            <xsl:for-each select="ClientList/Client">
                <Client>
                    <NameSurname>
                        <xsl:value-of select="concat(Name, ' ', Surname)" />
                    </NameSurname>
                    <!-- Function Call -->
                    <!-- passing value of 'Date' as parameter -->
                    <Date>
                        <xsl:value-of select="ex:DateConvertor(Date)" /> 
                    </Date>
                </Client>
            </xsl:for-each>
        </ClientList>
    </xsl:template>
    

    The required output can be achieved in a single XSL, but I am assuming that this is a learning phase and the above is probably an example of <xsl:include>.