Monobus
Monobus

Reputation: 147

If statement test with an attribute from another file

I'm working on an XSL template that has an if statement in it. Here is the xsl:

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

<xsl:variable name="config"
    select="document('config.xml')" />

<xsl:template match="/">

    <xsl:if test="config/value/@toggle = true">
        <xsl:text>IT WORKED</xsl:text>
    </xsl:if>

</xsl:template>

What I need to do is test the if statement with attributes from an XML that I have loaded into a variable.

Here is the other(config.xml):

<?xml version="1.0" encoding="UTF-8"?>
<config>

    <value toggle="true" />

</config>

Obviously this isnt working for me, so I was wondering if anyone could point out the correct way.

Upvotes: 1

Views: 46

Answers (1)

Tim C
Tim C

Reputation: 70638

Firstly, if you want to reference a variable, you need to use the $ prefix, so it should be $config not config.

Secondly, the $config then references the document node, not the root element (which also happens to be named config).

Taking both of this into account, your xsl:if statement should be as follows:

<xsl:if test="$config/config/value/@toggle = 'true'">

Upvotes: 2

Related Questions