Page 1 of 1

URGENT - XML with n numbers of nested categories

Posted: Mon Jul 09, 2007 3:02 am
by seopower
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have a XML which can have n number of nested categories and subcategories. The problem is node names are all same for each level. Now can any please tell how can I display names with relation between each?

Or how will loop work or any other method of getting values?

I mean display should be:

Category 1
  - Subcategory
        - More Subcategory
Category 2
  - Subcategory
        - More Subcategory

and so on....

The XML I am having is something like this:

[syntax="xml"]
<taxonomy>
 <form id="96451" name="Cars">
  <taxonomy>
    <form id="96477" name="Auto Parts and Accessories"></form>
    <form id="96621" name="Automotive Security Systems"></form>
    <form id="96478" name="Motorcycle Parts and Accessories"></form>
    <form id="96456" name="New Cars">
    <form id="96457" name="Used Cars"></form>
  </taxonomy>
 </form>
 <form id="2010" name="Clothing and Accessories">
  <taxonomy>
    <form id="31515" name="Clothing"></form>
    <form id="68185" name="Luggage"></form>
    <form id="96602" name="Shoes"></form>
  </taxonomy>
 </form>
</taxonomy>


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jul 09, 2007 6:22 am
by superdezign

Posted: Mon Jul 09, 2007 6:35 am
by volka
and/or xsl(t)

e.g.

Code: Select all

<?php
$xsl_s = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" encoding="iso-8859-1" indent="no"/>
  <xsl:template match="node">
    <fieldset><legend><xsl:value-of select="@id" /></legend>
      <xsl:apply-templates/>
    </fieldset>
   </xsl:template>
  <xsl:template match="value">
  <xsl:apply-templates/>
    </xsl:template>
  </xsl:stylesheet>
';

$xml_s = '<node id="1">
  <value>a</value>
  <value>b</value>
  <value>c</value>
  <node id="2">
    <value>d</value>
    <node id="3">
      <value>e</value>
      <value>f</value>
    </node>
  </node>
</node>';

$dom = new DOMDocument;
$xsl = new XSLTProcessor;

$dom->loadxml($xsl_s);
$xsl->importStylesheet($dom);

$dom->loadxml($xml_s);
echo $xsl->transformToXML($dom);
see also: http://www.google.de/search?&q=xslt

Posted: Mon Jul 09, 2007 7:24 am
by seopower
I am novice in php and will Greatly appriciate ..... If any one can provide some code examples for DOM or simple xml.

I want to diaplay categories like it is being displayed on pricegrabber.com home page.

Posted: Mon Jul 09, 2007 7:39 am
by superdezign
The DOM documentation is pretty straight forward and provides a lot of examples. It feels like using the JavaScript DOM model.

Posted: Mon Jul 09, 2007 7:43 am
by volka
seopower wrote:I am novice in php and will Greatly appriciate ..... If any one can provide some code examples for DOM or simple xml.
Since I already gave you a working example of xsl(t) and as superdezign mentioned the documentation provides a lot of examples your statement -sorry to say so- translates to "I don't want to learn that stuff, please code my script" for me.
You cannot expect to accomplish a complex task in "no time" when you're new/unexperienced in all techniques involved.

Posted: Mon Jul 09, 2007 7:51 am
by superdezign
POW! Right in the kissah.

Posted: Mon Jul 09, 2007 7:59 am
by volka
Just for the record: There's nothing wrong with beeing new/unexperienced, we all were at some point. But then you have to allow yourself the time to learn or the money to hire someone if the pressure is already on.

Posted: Mon Jul 09, 2007 8:02 am
by superdezign
volka wrote:Just for the record: There's nothing wrong with beeing new/unexperienced, we all were at some point. But then you have to allow yourself the time to learn or the money to hire someone if the pressure is already on.
... And put forth a decent effort. The first step to failure is to not try.