I need to transform the XML and am having some issues…
Current XML:
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<ManagerFirstName>Joe</ManagerFirstName>
<ManagerLastName>Schmoe</ManagerLastName>
</Employee>
</Employees>
Desired Output:
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<supervisorName>Schmoe, Joe</supervisorName>
</Employee>
</Employees>
Current XSL:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" >
<xsl:template match="/">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="node()">
<xsl:copy><xsl:apply-templates select="node()"/></xsl:copy>
</xsl:template>
<xsl:template match="ManagerFirstName">
<supervisorName>
<xsl:apply-templates select="node()"/>
<xsl:value-of select="/ManagerLastName"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="/ManagerFirstName"/>
</supervisorName>
</xsl:template>
</xsl:stylesheet>
This is not working and I can not figure it out. The XML it is outputting at the moment looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<supervisorName>Joe, </supervisorName>
<ManagerLastName>Schmoe/ManagerLastName>
</Employee>
</Employees>
I feel like I’m so close…
UPDATE
How would I go about making sure that if ManagerFirstName and ManagerLastName are blank, that supervisorName does not have the comma inside of it?
UPDATE 2
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/> <xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Employee">
<tbl_EmployeeList><xsl:apply-templates select="@*|node()"/></tbl_EmployeeList>
</xsl:template>
<xsl:template match="tbl_EmployeeList">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<supervisorName>
<xsl:value-of select="(ManagerLastName,ManagerFirstName)" separator=", "/>
</supervisorName>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
You are close. In the
ManagerFirstNametemplate you need a slightly different XPath to pull out the last name:(the
apply-templatesis sufficient to give you the value of theManagerFirstName, you don’t need a specificvalue-offor that). You then need a no-op template to stop it copying the last name independentlyAlso note that the usual identity template would match and apply-templates to
@*|node()rather than justnode()– it makes no difference in your example document because you’re not using any attributes, but if your original XML had attributes then your version of the identity template would drop them.