Transform XML using XSLT - Root Node not present -


i have 1 xml file :

<?xml version="1.0" encoding="utf-8" standalone="no"?> <file>     <customer>         <lastname>mylastname</lastname>     </customer>     <cars>         <car>             <color>blue</color>             <model>car2</model>             <year>1988</year>             <speed>250</speed>         </car>         <car>             <color>green</color>             <model>car3</model>             <year>1989</year>             <speed>350</speed>         </car>     </cars> </file> 

i want transform using xslt have :

<?xml version="1.0" encoding="utf-8"?> <file>     <purchaser>         <name>mylastname</name>     </purchaser>     <vehicles>         <vehicle>             <vehiclecolor>blue</vehiclecolor>             <vehiclemodel>car2</vehiclemodel>             <vehicleyear>1988</vehicleyear>             <vehiclespeed>250</vehiclespeed>         </vehicle>         <vehicle>             <vehiclecolor>green</vehiclecolor>             <vehiclemodel>car3</vehiclemodel>             <vehicleyear>1989</vehicleyear>             <vehiclespeed>350</vehiclespeed>         </vehicle>     </vehicles> </file> 

my xslt file :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">     <xsl:output method="xml"             indent="yes"             encoding="utf-8"             media-type="text/xml"/>       <xsl:template match="file/customer">         <xsl:element name="purchaser">             <xsl:element name="name">                 <xsl:value-of select="lastname"/>             </xsl:element>         </xsl:element>     </xsl:template>       <xsl:template match="file/cars">         <xsl:element name="vehicles">             <xsl:for-each select="car">                 <xsl:element name="vehicle">                     <xsl:element name="vehiclecolor">                         <xsl:value-of select="color"/>                     </xsl:element>                     <xsl:element name="vehiclemodel">                         <xsl:value-of select="model"/>                     </xsl:element>                     <xsl:element name="vehicleyear">                         <xsl:value-of select="year"/>                     </xsl:element>                     <xsl:element name="vehiclespeed">                         <xsl:value-of select="speed"/>                     </xsl:element>                 </xsl:element>             </xsl:for-each>         </xsl:element>     </xsl:template>  </xsl:stylesheet> 

but have not root node in output file. how this?

also, way make transformation? several templates stacked?

is way make transformation? several templates stacked?

you can use several templates, or 1 template - in case, neither method has distinct advantage or disadvantage against other. personally, keep short , simple , stick 1 template (mainly because there's 1 node you're copying without modification):

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>  <xsl:template match="/file">     <file>         <purchaser>             <name>                 <xsl:value-of select="customer/lastname"/>             </name>         </purchaser>         <vehicles>             <xsl:for-each select="cars/car">                 <vehicle>                     <xsl:for-each select="*">                         <xsl:element name="vehicle{name()}">                             <xsl:value-of select="."/>                         </xsl:element>                     </xsl:for-each>                 </vehicle>             </xsl:for-each>         </vehicles>     </file> </xsl:template>  </xsl:stylesheet> 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -