Transforming XSL with XSL

marcovolpe

New Member
A question about Martin's answer:Martin Honnen's answer works great, but not with the root element. Let's say I have "cars" as a root element: \[code\]<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="foo"> <cars> <car1 /> <car2 /> </cars> </xsl:template></xsl:stylesheet>\[/code\]And I want to obtain: \[code\]<xsl:template match="foo"> <cars> <car1 /> <car2 /> <TANK /> </cars></xsl:template>\[/code\]For this, I'd use: \[code\]<xsl:template match="cars" > <xsl:copy> <xsl:apply-templates/> <TANK /> </xsl:copy></xsl:template> \[/code\]Which outputs the exact input, without changing anything. I can try: \[code\]<xsl:template match="/" > <xsl:copy> <xsl:apply-templates/> <TANK /> </xsl:copy></xsl:template> \[/code\]But it will place the TANK node outside the stylesheet, like this:\[code\]<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="order"> <cars> <car1/> <car2/> </cars></xsl:template>\[/code\]How to get the TANK element inside cars?Original question:I have a XSL that I use to transform an XML:\[code\]XML_format1 -> XSL1 -> XML_format2\[/code\]I need to transform this first XSL file (using a second XSL) to obtain a third XSL file, which will output an XML with a third format. In short:\[code\]XSL1 -> XSL2 -> XSL3XML_format1 -> XSL3 -> XML_format3\[/code\]Using the following stylesheet, I am able to copy the first XSL's contents and also skip certain nodes: \[code\]<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy></xsl:template><xsl:template match="//skipThisNode"/></xsl:stylesheet>\[/code\]My problem: in addition to this, I also need to change some nodes' structure (add something), from this: \[code\]<car> <color>green</color> <fuel>petrol</fuel></car>\[/code\]To this: \[code\]<car> <color>green</color> <fuel>petrol</fuel> <topSpeed>99</topSpeed></car>\[/code\]LE: I could create a template to match the specific nodes that I need to add children to, like so: \[code\]<xsl:template match="car"> <color> <!-- existing value-of --> </color> <fuel> <!-- existing value-of --> </fuel> <topSpeed> <!-- * new value-of * --> </topSpeed></xsl:template>\[/code\]But this seems like going over the top. Is there a simpler way of achieving what I want?
 
Back
Top