I'm trying to transform an XML only to replace a node name with another, but the XSLT that I framed is messing up the transformed XML. This might be a small issue that I might have overlooked, but am stuck with this from a long time.
Input XML that I have:
<?xml version="1.0" encoding="UTF-8"?>
<dataCollection>
<queryConst language="DBSQL">
<queryUsed>
SELECT EMP.FirstName, EMP.LastName FROM (SELECT FirstName, LastName FROM
employees WHERE EmployeeID < 10) AS EMP
</queryUsed>
</queryConst>
<record>
<old>
<Employees>
<FirstName>James</FirstName>
<LastName>Gosling</LastName>
</Employees>
</old>
</record>
<record>
<old>
<Employees>
<FirstName>Rod</FirstName>
<LastName>Johnson</LastName>
</Employees>
</old>
</record>
</dataCollection>
The XSLT used is as follows:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="dataCollection/queryConst/@language">
<xsl:attribute name="language">DBVIEW</xsl:attribute>
</xsl:template>
<xsl:template match="dataCollection/record/old/Employees">
<xsl:element name="TABLE">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
The Output XML that I want is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<dataCollection>
<queryConst language="DBVIEW">
<queryUsed>
SELECT EMP.FirstName, EMP.LastName FROM (SELECT FirstName,
LastName FROM employees WHERE EmployeeID < 10) AS EMP
</queryUsed>
</queryConst>
<record>
<old>
<TABLE>
<FirstName>James</FirstName>
<LastName>Gosling</LastName>
</TABLE>
</old>
</record>
<record>
<old>
<TABLE>
<FirstName>Rod</FirstName>
<LastName>Johnson</LastName>
</TABLE>
</old>
</record>
</dataCollection>
Any help / tips / suggestions on the above is much appreciated.
You need to remove the xsl:copy
from the template matching Employees
. Also, there's no need to use xsl:element
when the element name is known. Try simply:
<xsl:template match="dataCollection/record/old/Employees">
<TABLE>
<xsl:apply-templates/>
</TABLE>
</xsl:template>
Note also that a match
pattern is not a select
expression . You don't need to use the path unless you have other Employees
nodes elsewhere in the input XML. With the given example,
<xsl:template match="Employees">
would be quite sufficient.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.