SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    Halifax
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Recursion with XSL - Sanity Check

    I'm currently looking at how I could best acheive a recursive look up in an XML document.

    I want to create a hierachial structure to show a relationship between nodes, something similar to this:

    Code:
    <root>
     <entity id="1">Parent
       <sub-entity>
         <entity id="1-1">Child 1</entity>
       </sub-entity>
      </entity>
    </root>
    Would this be the best way to acheive this?

    Secondly I was wondering how I would best acheive this, would something like a template based method be the best approach - i.e something like this???

    Code:
    <xsl:template match="entity">
     <li><xsl:value-of select="@id"/> </li>	
    </xsl:template>
    		
    <xsl:template match="sub-entity">
     <xsl:apply-templates select="entity"/>
    </xsl:template>

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    ../entity in your xpath statement perhaps?

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    Halifax
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Decided to use make a slight amendment to the XML structure:

    Code:
    <root>
     <test>
     <entity id="1">Parent
       <sub-entity>
         <entity id="1-1">Child 1</entity>
       </sub-entity>
      </entity>
     </test>
    </root>
    I'm using XMLDOM and SelectSingleNode to select the "test" node, the entity selection seems to work without ../, however I'm not returning any sub-entity nodes.
    Last edited by furious5; Mar 14, 2005 at 14:57.

  4. #4
    SitePoint Addict Brak's Avatar
    Join Date
    Jul 2004
    Location
    Central Coast, CA
    Posts
    321
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'll need to call sub-entity.
    Code:
    <xsl:template match="entity">
     <li><xsl:value-of select="@id"/> </li>	
    <xsl:apply-templates select="sub-entity" />
    </xsl:template>
    Studio Rockstar's Blog - A journey to quitting the dayjob.

  5. #5
    SitePoint Enthusiast tinanoo's Avatar
    Join Date
    Sep 2004
    Location
    Lafayette, LA
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I have a similar situation

    I thought I knew how do to this but obviously I'm wrong . A PHP programmer put a site map into an xml page and wants it to display the full tree at the bottom of a page. Any help would be greatly appreciated.

    Here is what I have:

    XML excerpt:
    Code:
    <configuration>
     <site_map>
         <category>
    	 <category_id>13</category_id>
    	 <category_name>Massage Tables</category_name>
    	 <URL>/categories/Massage-Tables/13/1</URL>
    		<category>
    			<category_id>91</category_id>
    			<category_name>Massage Table Packages</category_name>
    			<URL>/categories/Massage-Table-Packages/91/1</URL>
    		</category>
    		<category>
    			<category_id>103</category_id>
    			<category_name>Portable Massage Tables</category_name>
    		        <URL>/categories/Portable-Massage-Tables/103/1</URL>
                           	<category>
      			     <category_id>105</category_id>
    			     <category_name>Stationary Massage Tables</category_name>
    	  	              <URL>/categories/Stationary-Massage-Tables/105/1</URL>
    		        </category>
    		</category>
           </category>
           ...
      </site_map>
    </configuration>
    XSLT exerpt:
    Code:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="utf-8" indent="no"/>
    <xsl:template match="configuration">
    ....
    <xsl:for-each select="/configuration/site_map/category">
    	<ul>
    		<li>
    		<p><xsl:value-of select="category_name" /></p>
    		<xsl:apply-template select="category" />
    
    			
    		</li>
    	</ul>		
    </xsl:for-each>
    </xsl:template>
    <xsl:template match="category">
    	<xsl:for-each select="descendant-or-self::category">
    	<ul>
    		<li><xsl:value-of select="category_name" /></li>
    	</ul>			
    	</xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

  6. #6
    SitePoint Enthusiast tinanoo's Avatar
    Join Date
    Sep 2004
    Location
    Lafayette, LA
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind. I seem to have figured it out, but just incase anyone is curious here is the solution I came up with...

    XSLT excerpt:
    Code:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="utf-8" indent="no"/>
    <xsl:template match="configuration">
    ....
    <xsl:for-each select="/configuration/site_map">
    	<xsl:call-template name="category_tree" />
    </xsl:for-each>
    </xsl:template>
    <xsl:template name="category_tree">
    	<xsl:for-each select="category">
    	<ul>
    		<li><xsl:value-of select="category_name" /></li>
    		<xsl:call-template name="category_tree" />
    	</ul>			
    	</xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

  7. #7
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A suggestion to make the xml a bit more friendly/light:

    Code:
    <site_map>
    	<category id="13" name="Massage Tables" URL="/categories/Massage-Tables/13/1">
    		<category id="91" name="Massage Table Packages" URL="/categories/Massage-Table-Packages/91/1" />
    		<category id="103" name="Portable Massage Tables" URL="/categories/Portable-Massage-Tables/103/1">
    			<category id="105" name="Stationary Massage Tables" URL="/categories/Stationary-Massage-Tables/105/1" />
    		</category>
    	</category>
    </site_map>

  8. #8
    SitePoint Enthusiast tinanoo's Avatar
    Join Date
    Sep 2004
    Location
    Lafayette, LA
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey jim. thanks for the response, but unfortunately i'm just in charge of the xsl. the xml comes to me from someone else.

    tina


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •