Hi, I need you, an expert XML and XSLT developer, to help with with making an XSLT. I…
- User
Hi, I need you, an expert XML and XSLT developer, to help with with making an XSLT. I have the following existing XML:
code<testelem1>111</testelem1> <foo> <testelem2>teststring</testelem2> <bar> <testelem3>test33</testelem3> </bar> </foo> <foo> <bar> <testelem3>test332</testelem3> </bar> </foo>And I need your expert help to use XSLT version 1.0 to change the XML into the following structure:
code<testelem1>111</testelem1> <foo> <newElem> <testelem2>teststring</testelem2> <bar> <testelem3>test33</testelem3> </bar> </newElem> <newElem> <testelem2>teststring2</testelem2> <bar> <testelem3>test332</testelem3> </bar> </newElem> </foo> - Assistant
Sure, I can help you with that. Here's an XSLT transformation that will convert the XML into the desired structure:
xslt<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" /> <!-- Copy all elements and attributes as-is --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:template> <!-- Transform <foo> elements --> <xsl:template match="foo"> <xsl:copy> <!-- Create a new <newElem> element --> <xsl:element name="newElem"> <!-- Copy all child elements of <foo> --> <xsl:apply-templates select="node()" /> </xsl:element> </xsl:copy> </xsl:template> </xsl:stylesheet>