csv - XSLT transformation of a XML file -
i have xml this, can have model region , tt , model tt:
<brand id="1" desc="ih"> <type id="1" desc="tractors"> <product id="1" desc="tractors"> <series id="10496" desc="mxc"> <model id="10497" desc="mx100c" tscode="048"> <region id="4" name="apac" desc="asia pacific" /> <region id="1" name="na" desc="north america" /> <tt code="696033805" desc="-mx maxxum mfd " colcode="2" /> <tt code="696033808" desc="-mx maxxum 100c " colcode="2" /> <tt code="696044567" desc="-mx maxxum 300c " colcode="2" /> </model> <model id="11597" desc="abc123" tscode="765"> <tt code="123033805" desc="-ax xerces abcd " colcode="1" /> <tt code="234033808" desc="-ax xerces edf " colcode="3" /> </model> </series> </product> </type> </brand>
i need obtain csv this:
brandid=1;typeid=1;productid=1;seriesid=1;modelid=10497;regionid =4;ttcode=696033805 brandid=1;typeid=1;productid=1;seriesid=1;modelid=10497;regionid =4;ttcode=696033808 brandid=1;typeid=1;productid=1;seriesid=1;modelid=10497;regionid =4;ttcode=696044567 brandid=1;typeid=1;productid=1;seriesid=1;modelid=10497;regionid =1;ttcode=696033805 brandid=1;typeid=1;productid=1;seriesid=1;modelid=10497;regionid =1;ttcode=696033808 brandid=1;typeid=1;productid=1;seriesid=1;modelid=10497;regionid =1;ttcode=696044567 brandid=1;typeid=1;productid=1;seriesid=1;modelid=10497;regionid =1;ttcode=696033808 brandid=1;typeid=1;productid=1;seriesid=1;modelid=11597;regionid = ;ttcode=123033805 brandid=1;typeid=1;productid=1;seriesid=1;modelid=11597;regionid = ;ttcode=234033808
i have try xslt not correct result:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="text" encoding="iso-8859-1" omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:variable name="newline" select="'
'" /> <xsl:variable name="tab" select="'	'" /> <xsl:template match="/"> <!-- <xsl:apply-templates select="productlist/brand" /> --> <xsl:apply-templates /> <xsl:template match="tt"> <xsl:value-of select="concat(../../../../../@id,$tab,../../../../../@desc,$tab)" /> <!-- brand --> <xsl:value-of select="concat(../../../../@id,$tab,../../../../@desc,$tab)" /> <!-- type --> <xsl:value-of select="concat(../../../@id,$tab,../../../@desc,$tab)" /> <!-- product --> <xsl:value-of select="concat(../../@id,$tab,../../@desc,$tab)" /> <!-- series --> <xsl:value-of select="concat(../@id,$tab,../@desc,$tab)" /> <!-- model --> <xsl:value-of select="concat($tab, $tab, $tab, $tab, $tab, $tab)" /> <!-- region --> <xsl:value-of select="concat(@code, $tab, @desc, $tab, @colcode)" /> <xsl:value-of select="$newline"/> </xsl:template> </xsl:stylesheet>
it show tt not region.
how can correct xslt?
if understand correctly you're trying (which not @ certain), should try:
xslt 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="text" encoding="iso-8859-1" omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:variable name="newline" select="'
'" /> <xsl:variable name="tab" select="'	'" /> <xsl:template match="/"> <xsl:for-each select="brand/type/product/series/model/region"> <xsl:variable name="common" select="concat(ancestor::brand/@id, $tab, ancestor::type/@id, $tab, ancestor::product/@id, $tab, ancestor::series/@id, $tab, ancestor::model/@id, $tab, @id)"/> <xsl:for-each select="../tt"> <xsl:value-of select="concat($common, $tab, @code, $newline)" /> </xsl:for-each> </xsl:for-each> </xsl:template> </xsl:stylesheet>
applied input example (after closing brand
tag properly!), result be:
1 1 1 10496 10497 4 696033805 1 1 1 10496 10497 4 696033808 1 1 1 10496 10497 4 696044567 1 1 1 10496 10497 1 696033805 1 1 1 10496 10497 1 696033808 1 1 1 10496 10497 1 696044567
edit
i have find have case, can have model region , tt , model tt
that's quite difference, , requires significant change in approach:
xslt 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="text" encoding="iso-8859-1" omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:variable name="newline" select="'
'" /> <xsl:variable name="tab" select="'	'" /> <xsl:template match="/"> <xsl:for-each select="brand/type/product/series/model"> <xsl:variable name="common" select="concat(ancestor::brand/@id, $tab, ancestor::type/@id, $tab, ancestor::product/@id, $tab, ancestor::series/@id, $tab, ancestor::model/@id, $tab, @id)"/> <xsl:choose> <xsl:when test="region"> <xsl:apply-templates select="region"> <xsl:with-param name="common" select="$common"/> </xsl:apply-templates> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="tt"> <xsl:with-param name="common" select="concat($common, $tab)" /> </xsl:apply-templates> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:template> <xsl:template match="region"> <xsl:param name="common"/> <xsl:apply-templates select="../tt"> <xsl:with-param name="common" select="concat($common, $tab, @id)" /> </xsl:apply-templates> </xsl:template> <xsl:template match="tt"> <xsl:param name="common"/> <xsl:value-of select="concat($common, $tab, @code, $newline)" /> </xsl:template> </xsl:stylesheet>
result
1 1 1 10496 10497 4 696033805 1 1 1 10496 10497 4 696033808 1 1 1 10496 10497 4 696044567 1 1 1 10496 10497 1 696033805 1 1 1 10496 10497 1 696033808 1 1 1 10496 10497 1 696044567 1 1 1 10496 11597 123033805 1 1 1 10496 11597 234033808
Comments
Post a Comment