c# - Transform xml to xml using XSLT - Advices -


i have 1 xml file :

<?xml version="1.0" encoding="utf-8" standalone="no"?> <cars>     <car>         <color>blue</color>         <model>car2</model>         <year>1988</year>         <speed>250</speed>     </car>     <car>         <color>blue</color>         <model>car2</model>         <year>1988</year>         <speed>250</speed>     </car> </cars> 

i want transform using xslt have :

<?xml version="1.0" encoding="utf-8" standalone="no"?> <vehicles>     <vehicle>         <vehiclecolor>blue</vehiclecolor>         <vehiclemodel>car2</vehiclemodel>         <vehicleyear>1988</vehicleyear>         <vehiclespeed>250</vehiclespeed>     </vehicle>     <vehicle>         <vehiclecolor>blue</vehiclecolor>         <vehiclemodel>car2</vehiclemodel>         <vehicleyear>1988</vehicleyear>         <vehiclespeed>250</vehiclespeed>     </vehicle> </vehicles> 

my xslt file :

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">      <xsl:template match="/">          <vehicles>             <xsl:for-each select="cars/car">                  <vehicle>                     <vehiclecolor><xsl:value-of select="color"/></vehiclecolor>                     <vehiclemodel><xsl:value-of select="model"/></vehiclemodel>                     <vehicleyear><xsl:value-of select="year"/></vehicleyear>                     <vehiclespeed><xsl:value-of select="speed"/></vehiclespeed>                 </vehicle>              </xsl:for-each>         </vehicles>      </xsl:template>  </xsl:stylesheet> 

it work want know if xslt file correct , if no, want advices.

also, in output file generated xslt, have not xml header. why ?

your xslt fine. think best way answer question benefit of helping people study xslt go through tags , explain why work correctly. first, need add xsl stylesheet , namespace, you've done here:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match ='/'> boilerplate. if did not have it, output incorrect.

the <xsl:for-each select="cars/car"> calls each element of xml dom matches tag specified select =.

next, used literal tags directly change names of tags in output file. tag literal in sense xslt drop output. <xsl:value-of select = somestring> tag writes value of tag in original file specified somestring. if wanted write attribute, need @ symbol @ first position of string. regardless of data had started with, output file contain correct results. if having doubts, because, say, learning xslt, getting trial version of xmlspy great way of validating xslt files verify doing want them to.


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 -