XSLT creating new tags from the value in other tags

hsrbapjvob

New Member
Please can someone help a newbie who is really struggling.I am new to XSL and I just cannot work this out.I can get it close, but cannot get the \[code\]<Material_Data>\[/code\], \[code\]<Material>\[/code\] and \[code\]<Comp_Class>\[/code\] tags sorted. Below is the source document:\[code\] <?xml version="1.0" encoding="UTF-8"?><Variable_Attributes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\ZZ\Test Var Attribs.xsd"> <Material>1</Material> <Comp_Class>Aerosol - Aluminium</Comp_Class> <Material_Data> <Field_Name>ZMDM_Diameter</Field_Name> <Field_Value>45</Field_Value> <Field_Name>ZMDM_Height</Field_Name> <Field_Value>60</Field_Value> <Field_Name>ZMDM_Supplier</Field_Name> <Field_Value>Tony</Field_Value> <Field_Name>ZMDM_Brimful</Field_Name> <Field_Value>78</Field_Value> <Field_Name>ZMDM_Size</Field_Name> <Field_Value>440</Field_Value> </Material_Data> <Material>2</Material> <Comp_Class>Aerosol Valves</Comp_Class> <Material_Data> <Field_Name>ZMDM_UoM</Field_Name> <Field_Value>EA</Field_Value> <Field_Name>ZMDM_Free_Text</Field_Name> <Field_Value>Test Aerosol</Field_Value> <Field_Name>ZMDM_Size</Field_Name> <Field_Value>18</Field_Value> </Material_Data></Variable_Attributes>\[/code\]what I need to get to is this:\[code\]<?xml version="1.0" encoding="UTF-8"?><Material_Data> <Material>1</Material> <Comp_Class>Aerosol - Aluminium</Comp_Class> <ZMDM_Diameter>45</ZMDM_Diameter> <ZMDM_Height>60</ZMDM_Height> <ZMDM_Supplier>Tony</ZMDM_Supplier> <ZMDM_Brimful>78</ZMDM_Brimful> <ZMDM_Size>440</ZMDM_Size> <ZMDM_UoM>EA</ZMDM_UoM></Material_Data><Material_Data> <Material>2</Material> <Comp_Class>Aerosol Valves</Comp_Class> <ZMDM_Free_Text>Test Aerosol</ZMDM_Free_Text> <ZMDM_Size>18</ZMDM_Size></Material_Data>\[/code\]Any help would be much appreciated.Thanks in advanceSteveBelow is what I have tried so far:\[code\]<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/"> <Material_Data> <xsl:for-each select="/Variable_Attributes/Material_Data/Field_Name"> <xsl:variable name="val" select="current()"/> <xsl:variable name="val2" select="current()/following-sibling::*[1]"/> <xsl:element name="{$val}"> <xsl:value-of select="$val2"/> </xsl:element> </xsl:for-each> </Material_Data> </xsl:template></xsl:stylesheet>\[/code\]
 
Back
Top