This is going to be a really simple question I’m sure. I have an xml document that I’m transforming via XSL. The important part of this xml looks like this:
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Transaction>
<EnrollmentModel>
<FutureContributionsModel>
<FutureContributionsElectionType>ACertainThirdParty</FutureContributionsElectionType>
</FutureContributionsModel>
</EnrollmentModel>
<Transaction>
</root>
And I want to add the following if the value of <FutureContributionsElectionType> does, in fact, equal ACertainThirdParty:
<fo:table-row>
<fo:table-cell>
<fo:block font-family="verdanaPS" font-size="9" padding-bottom="15px" padding-top="10px">
The Participant has successfully opted in to use ACertainThirdParty as the managed provider for the account.
</fo:block>
</fo:table-cell>
</fo:table-row>
Note that there is only one third party, so I don’t need to get the value of the node for the custom text, I can just hard-code it in there.
If the value of <FutureContributionsElectionType> does NOT equal ACertainThirdParty, I wan’t to add a whole bunch of other stuff.
Here’s what I tried:
So this seems like a job for <xsl:choose> and <xsl:when> / <xsl:otherwise>, right?
Here’s what I got:
<xsl:choose>
<xsl:when test="FutureContributionsModel/FutureContributionsElectionType='ACertainThirdParty'">
<fo:table-row>
<fo:table-cell>
<fo:block font-family="verdanaPS" font-size="9" padding-bottom="15px" padding-top="10px">
The Participant has successfully opted in to use ACertainThirdParty as the managed provider for the account.
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:when>
<xsl:otherwise>
<fo:table-row>
<fo:table-cell>
...
Lots of stuff
...
</fo:table-cell>
</fo:table-row>
</xsl:otherwise>
</xsl:choose>
But when I transform it, the otherwise code gets hit rather than the correct code (in my xml the value is indeed ACertainThirdParty. My guess is my problem is that I don’t know XPath, so I’m probably assuming I can do things that I can’t. What’s going on here?
Most probably the context (current node) isn’t
Transaction.You can use an absolute XPath expression:
Much better, avoid using explicit conditionals — use templates and template match patterns: