Rotan075
Rotan075

Reputation: 2615

Defining a variable which is been set in another xslt

I was wondering whether it is possible to set a variable in XSLT from another XSLT file?

Let me explain myself by some code.

As default I have the following file (default.xsl):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    exclude-result-prefixes="msxsl dom arr xsd i"
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
    xmlns:dom="http://www.test.nl/dom/">

    <xsl:output method="xml" indent="no" omit-xml-declaration="yes" />

    <!--Textbox-->
    <xsl:template match="dom:TextBox">
        <xsl:variable name="placeHolderText">placeholder tekst</xsl:variable>
        <xsl:element name="textarea">
            <xsl:attribute name="placeholder">
                <xsl:value-of select="$placeHolderText"/>
            </xsl:attribute>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>   

In some cases I want to overrule the current default implementation and there I create an extension file: extension.xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dom="http://www.test.nl/dom/" exclude-result-prefixes="dom">

    <xsl:import href="default.xsl"/>

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />

    <xsl:template match="dom:TextBox">      
        <xsl:variable name='placeHolderText'>unknown</xsl:variable> 
    </xsl:template>

</xsl:stylesheet>

In this extension file I want to set the placeHolderText. I was wondering is this even possible? Because the extension file will always be loaded after the default is already processed. Thank you for your feedback.

Upvotes: 0

Views: 79

Answers (1)

hr_117
hr_117

Reputation: 9627

One possible solution may be to use a template with mode and use xsl:param instead of xsl:variable. Try for default.xsl:

    <!--Textbox-->
    <xsl:template match="dom:TextBox" mode="useparam">
        <xsl:param name="placeHolderText">placeholder tekst</xsl:param>

        <xsl:element name="textarea">
            <xsl:attribute name="placeholder">
                <xsl:value-of select="$placeHolderText"/>
            </xsl:attribute>
        </xsl:element>
    </xsl:template>

   <xsl:template match="dom:TextBox">
     <xsl:apply-templates select="." mode="useparam" />
  </xsl:template>  

and in extension.xsl:

<xsl:template match="dom:TextBox">
  <xsl:apply-templates select="." mode="useparam" >
   <xsl:with-param name="placeHolderText" select="'unknown'" />
  </xsl:apply-templates>
</xsl:template>  

Upvotes: 1

Related Questions