johkar
johkar

Reputation: 1

Conditional templates based on global variable

I want to copy most of the XML as is except for a couple of nodes that depend on the company which will be passed in to the stylesheet. If I have am using an identity template and I only want something to happen if a global variable equals a specific value, how do I make that happen since you can't put a check in the match between the []...at least in 1.0? So in the example below I only want to swap out the company name when the variable equals a certain value like 'DEF Company'. The 'company' variable will not be part of the XML.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>

<!-- Dummy example variable -->
<xsl:variable name="company"><xsl:text>DEF Company</xsl:text></xsl:variable>

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

<xsl:template match="Company">  
    <xsl:copy>  
        <xsl:text>ABC Company</xsl:text>  
    </xsl:copy>  
</xsl:template>

</xsl:stylesheet>

Upvotes: 0

Views: 285

Answers (3)

Erlock
Erlock

Reputation: 1968

I'd rather use an embedded element in the stylesheet for this purpose:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my-own-namespace">
    <my:company match="DEF Company" replace="ABC Company" />
    <xsl:template match="Company[.=document('')/*/my:company/@match]">
        <xsl:copy>
            <xsl:value-of select="document('')/*/my:company/@replace" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template> 
</xsl:stylesheet>

document() function with an empty string parameter refers to the stylesheet's root itself.

Upvotes: 1

user65839
user65839

Reputation:

Well, you can check inside the match. It might not be the most elegant, but it should work.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:param name="company" select="'DEF Company'"/>

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

    <xsl:template match="Company">
        <xsl:copy>
            <xsl:choose>
                <xsl:when test="$company = 'DEF Company'">
                    <xsl:text>ABC Company</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="@* | node()" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 3

Jason Aller
Jason Aller

Reputation: 3652

I use [] with template match and a version 1.0 XSLT engine (Xalan).

For example:

<xsl:template match="@*|node()[name() !='a']">

Upvotes: 0

Related Questions