Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Transformation

Status
Not open for further replies.

villarwl

Programmer
Jun 21, 2003
1
SA
How can i parse this xml....
<xml>
<row dept=&quot;A1&quot; craft=&quot;B1&quot; program=&quot;C1&quot; />
<row dept=&quot;A1&quot; craft=&quot;B1&quot; program=&quot;C2&quot; />
<row dept=&quot;A1&quot; craft=&quot;B3&quot; program=&quot;C2&quot; />
<row dept=&quot;A2&quot; craft=&quot;B1&quot; program=&quot;C1&quot; />
<row dept=&quot;A2&quot; craft=&quot;B2&quot; program=&quot;C2&quot; />
<row dept=&quot;A2&quot; craft=&quot;B3&quot; program=&quot;C1&quot; />
</xml>

Into these..................

<xml>
<dept name=&quot;A1&quot;>
<craft name=&quot;B1&quot;>
<program=&quot;C1&quot; />
<program=&quot;C2&quot; />
</craft>
<craft name=&quot;B3&quot;>
<program=&quot;C2&quot; />
</craft>
</dept>
<dept name=&quot;A2&quot;>
<craft name=&quot;B1&quot;>
<program=&quot;C1&quot; />
</craft>
<craft name=&quot;B2&quot;>
<program=&quot;C2&quot; />
</craft>
<craft name=&quot;B3&quot;>
<program=&quot;C1&quot; />
</craft>
</dept>
</xml>

Thanks in advance.
 
demo xml file:
----------------------------------------------

<?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;myfile.xsl&quot;?>
<xml>
<row dept=&quot;A1&quot; craft=&quot;B1&quot; program=&quot;C1&quot; />
<row dept=&quot;A1&quot; craft=&quot;B1&quot; program=&quot;C2&quot; />
<row dept=&quot;A1&quot; craft=&quot;B3&quot; program=&quot;C2&quot; />
<row dept=&quot;A2&quot; craft=&quot;B1&quot; program=&quot;C1&quot; />
<row dept=&quot;A2&quot; craft=&quot;B2&quot; program=&quot;C2&quot; />
<row dept=&quot;A2&quot; craft=&quot;B3&quot; program=&quot;C1&quot; />
</xml>

demo xsl file: myfile.xsl
----------------------------------------------
<xsl:stylesheet xmlns:xsl=&quot; version=&quot;1.0&quot;>
<xsl:template match=&quot;/&quot;>
<textarea rows=&quot;10&quot; cols=&quot;40&quot;>
<xsl:apply-templates />
</textarea>
</xsl:template>
<xsl:template match=&quot;xml&quot;>
<xsl:element name=&quot;xml&quot;>
<xsl:for-each select=&quot;row&quot;>
<xsl:call-template name=&quot;dept&quot; />
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template name=&quot;dept&quot;>
<xsl:variable name=&quot;position&quot; select=&quot;position()-1&quot; />
<xsl:variable name=&quot;last_dept&quot; select=&quot;../row[$position]/@dept&quot; />
<xsl:variable name=&quot;current_dept&quot; select=&quot;@dept&quot; />
<xsl:if test=&quot;not($current_dept=$last_dept)&quot;>
<xsl:element name=&quot;dept&quot;>
<xsl:attribute name=&quot;name&quot;>
<xsl:value-of select=&quot;@dept&quot; />
</xsl:attribute>
<xsl:for-each select=&quot;../row&quot;>
<xsl:sort select=&quot;@dept&quot; />
<xsl:sort select=&quot;@craft&quot; />
<xsl:if test=&quot;@dept=$current_dept&quot;>
<xsl:call-template name=&quot;craft&quot; />
</xsl:if>
</xsl:for-each>
</xsl:element>
<xsl:text>&#10;</xsl:text>
</xsl:if>
</xsl:template>
<xsl:template name=&quot;craft&quot;>
<xsl:variable name=&quot;position&quot; select=&quot;position()-1&quot; />
<xsl:variable name=&quot;last_craft&quot; select=&quot;../row[$position]/@craft&quot; />
<xsl:variable name=&quot;current_dept&quot; select=&quot;@dept&quot; />
<xsl:variable name=&quot;current_craft&quot; select=&quot;@craft&quot; />
<xsl:if test=&quot;not($current_craft=$last_craft)&quot;>
<xsl:element name=&quot;craft&quot;>
<xsl:attribute name=&quot;name&quot;>
<xsl:value-of select=&quot;@craft&quot; />
</xsl:attribute>
<xsl:for-each select=&quot;../row&quot;>
<xsl:sort select=&quot;@dept&quot; />
<xsl:sort select=&quot;@craft&quot; />
<xsl:sort select=&quot;@program&quot; />
<xsl:if test=&quot;(@craft=$current_craft) and (@dept=$current_dept)&quot;>
<xsl:call-template name=&quot;program&quot; />
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:if>
</xsl:template>
<xsl:template name=&quot;program&quot;>
<xsl:for-each select=&quot;.&quot;>
<xsl:element name=&quot;program&quot;>
<xsl:attribute name=&quot;name&quot;>
<xsl:value-of select=&quot;@program&quot; />
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


Build web applications faster with a few lines of XML Code using DataML[tm]
 
actually use this xsl source:
--------------------------------------------------------

<xsl:stylesheet xmlns:xsl=&quot; version=&quot;1.0&quot;>
<xsl:template match=&quot;/&quot;>
<xsl:apply-templates />
</xsl:template>
<xsl:template match=&quot;xml&quot;>
<xsl:element name=&quot;xml&quot;>
<xsl:for-each select=&quot;row&quot;>
<xsl:call-template name=&quot;dept&quot; />
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template name=&quot;dept&quot;>
<xsl:variable name=&quot;position&quot; select=&quot;position()-1&quot; />
<xsl:variable name=&quot;last_dept&quot; select=&quot;../row[$position]/@dept&quot; />
<xsl:variable name=&quot;current_dept&quot; select=&quot;@dept&quot; />
<xsl:if test=&quot;not($current_dept=$last_dept)&quot;>
<xsl:element name=&quot;dept&quot;>
<xsl:attribute name=&quot;name&quot;>
<xsl:value-of select=&quot;@dept&quot; />
</xsl:attribute>
<xsl:for-each select=&quot;../row&quot;>
<xsl:sort select=&quot;@dept&quot; />
<xsl:sort select=&quot;@craft&quot; />
<xsl:if test=&quot;@dept=$current_dept&quot;>
<xsl:call-template name=&quot;craft&quot; />
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:if>
</xsl:template>
<xsl:template name=&quot;craft&quot;>
<xsl:variable name=&quot;position&quot; select=&quot;position()-1&quot; />
<xsl:variable name=&quot;last_craft&quot; select=&quot;../row[$position]/@craft&quot; />
<xsl:variable name=&quot;current_dept&quot; select=&quot;@dept&quot; />
<xsl:variable name=&quot;current_craft&quot; select=&quot;@craft&quot; />
<xsl:if test=&quot;not($current_craft=$last_craft)&quot;>
<xsl:element name=&quot;craft&quot;>
<xsl:attribute name=&quot;name&quot;>
<xsl:value-of select=&quot;@craft&quot; />
</xsl:attribute>
<xsl:for-each select=&quot;../row&quot;>
<xsl:sort select=&quot;@dept&quot; />
<xsl:sort select=&quot;@craft&quot; />
<xsl:sort select=&quot;@program&quot; />
<xsl:if test=&quot;(@craft=$current_craft) and (@dept=$current_dept)&quot;>
<xsl:call-template name=&quot;program&quot; />
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:if>
</xsl:template>
<xsl:template name=&quot;program&quot;>
<xsl:for-each select=&quot;.&quot;>
<xsl:element name=&quot;program&quot;>
<xsl:attribute name=&quot;name&quot;>
<xsl:value-of select=&quot;@program&quot; />
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


Build web applications faster with a few lines of XML Code using DataML[tm]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top