tags:

views:

51

answers:

3

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"&gt;
<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>

A: 

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

For example:

<xsl:template match="@*|node()[name() !='a']">
Jason Aller
I only wish to execute the following template if global variable 'company' is equal to 'DEF Company'. Plus the value of the global variable will be passed in via a converter. `<xsl:template match="Company">`
johkar
+3  A: 

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>
Peter Cooper Jr.
+1 this is exactly what I had ready seconds after you. :)
Tomalak
$company = 'DEF Company' is wrong.I think you means . = $company ;)
Erlock
I was interpreting the question as being that if the parameter passed into the stylesheet matched a value, to replace the text of one element. If one wanted to match the parameter to the value of the input, then yes, you'd want to check with . = $company. Upon rereading, it's not clear to me which one of those the questioner wanted to do.
Peter Cooper Jr.
+1  A: 

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.

Erlock