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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT - recompose simple xml 1

Status
Not open for further replies.

russland

Programmer
Joined
Jan 9, 2003
Messages
315
Location
CH
hi,
i've got an xml storing all products on the same level(flat structure). A few products belong to other procucts (yes, products can be childs of products). how can i recompose my xml to fit my situation? resp. how can i move the products underneath their parent-product? i do have a prodID for each product and a parentProdID.
e.g.
<prod>
<prodName>Wheel</prodName>
<prodID>1234</prodName>
<parentProdID>23</parentProdID>
</prod

yes, i know. this cannot be a huge pile of work. but still though tough for a rookie in XSLT.

thanks
 
Try this, guess it's not too hard to figure out how it works. Good luck!
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
   <xsl:output method="xml" />

   <xsl:template match="/">
      <xsl:apply-templates select="//prod[not (parentProdID &gt; 0)]" />
   </xsl:template>

   <xsl:template match="prod">
      <xsl:element name="prod">
         <xsl:variable name="id" select="prodID" />
         <xsl:apply-templates />
         <xsl:apply-templates select="../prod[parentProdID=$id]" />
      </xsl:element>
   </xsl:template>

   <xsl:template match="prod/*">
      <xsl:copy-of select="." />
   </xsl:template>

</xsl:stylesheet>
 
great. works just fine. you deserver the star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top