Hi
I'm not sure exactly what you mean, I've used xsl:otherwise because there are 2 outputs that I'm after:
(The previously displayed outputs were manually created)
1. If Company details are provided (even if Individual details are provided) I only want the Company details transformed.
eg.
<Clients>
<Client>
<ClientName>
<Business>
<BusinessName>Company X</BusinessName>
</Business>
</ClientName>
</Client>
</Clients>
2. If only Individual details are provided then only these are transformed i.e. no blank Company tags
eg.
<Clients>
<Client>
<ClientName>
<Individual>
<IndividualTitle>Mr</IndividualTitle>
<IndividualFirstName>Joe</IndividualFirstName>
<IndividualMiddleName>John</IndividualMiddleName>
<IndividualLastName>Bloggs</IndividualLastName>
</Individual>
</ClientName>
</Client>
</Clients>
But I'm only getting Output 2 even if the company Details are provided.
Input XML
Code:
<?xml version="1.0"?>
<Letter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<CompanyName xmlns="http://www.xyz.org/ABC">Company X</CompanyName> <!-- As Company is here I should get Output 1, but I'm not.
<Title xmlns="http://www.xyz.org/ABC">Mr</Title>
<FirstName xmlns="http://www.xyz.org/ABC">Joe</FirstName>
<MiddleName xmlns="http://www.xyz.org/ABC">John</MiddleName>
<LastName xmlns="http://www.xyz.org/ABC">Bloggs</LastName>
</Letter>
XSLT
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" exclude-result-prefixes="xx" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xx="http://www.xyz.org/ABC" xmlns="http://www.xyz.org/XYZ">
<xsl:template match="Letter">
<Details>
<Clients>
<Client>
<xsl:apply-templates select="xx:Client"/>
<ClientName>
<xsl:choose>
<xsl:when test="/Letter/xx:CompanyName!=''">
<Business>
<xsl:apply-templates select="xx:CompanyName"/>
</Business>
</xsl:when>
<xsl:otherwise>
<Individual>
<xsl:apply-templates select="xx:Title"/>
<xsl:apply-templates select="xx:FirstName"/>
<xsl:apply-templates select="xx:MiddleName"/>
<xsl:apply-templates select="xx:LastName"/>
</Individual>
</xsl:otherwise>
</xsl:choose>
</ClientName>
</Client>
</Clients>
</Details>
</xsl:template>
<xsl:template match="xx:CompanyName">
<BusinessName>
<xsl:value-of select ="/Letter/xx:CompanyName"/>
</BusinessName>
</xsl:template>
<xsl:template match="xx:Title">
<IndividualTitle>
<xsl:value-of select ="."/>
</IndividualTitle>
</xsl:template>
<xsl:template match="xx:FirstName">
<IndividualFirstName>
<xsl:value-of select ="."/>
</IndividualFirstName>
</xsl:template>
<xsl:template match="xx:LastName">
<IndividualLastName>
<xsl:value-of select ="."/>
</IndividualLastName>
</xsl:template>
<xsl:template match="xx:MiddleName">
<IndividualMiddleName>
<xsl:value-of select ="."/>
</IndividualMiddleName>
</xsl:template>
</xsl:stylesheet>