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

XML & XSLT -> XML problem

Status
Not open for further replies.

ResolutionsNET

IS-IT--Management
Jul 31, 2000
70
0
0
GB
Can anyone help, can't seem to completely sus this xslt out.
<Navigation>
<Item name="Home" depth="1" navid="1" type="16">
<Item name="Consumers" depth="2" navid="2" type="128">
<Item name="Products" depth="3" navid="3" type="64">
<Item name="Products 1a" depth="4" navid="4" type="16"/>
<Item name="Products 1b" depth="4" navid="5" type="16">
<Item name="1b1" depth="5" navid="6" type="16"/>
<Item name="1b2" depth="5" navid="7" type="16"/>
</Item>
<Item name="Products 1c" depth="4" navid="8" type="16"/>
<Item name="Products 1d" depth="4" navid="9" type="16">
<Item name="1d1" depth="5" navid="10" type="16"/>
<Item name="1d2" depth="5" navid="11" type="16"/>
</Item>
<Item name="Products 1e" depth="4" navid="12" type="16"/>
</Item>
</Item>
<Item name="Non-Consumers" depth="2" navid="125" type="128">
<Item name="Services" depth="3" navid="126" type="64">
<Item name="Service 1" depth="4" navid="127" type="16">
<Item name="Service 1a" depth="5" navid="128" type="16"/>
<Item name="Service 1b" depth="5" navid="129" type="16"/>
</Item>
<Item name="Service 2" depth="4" navid="130" type="16">
<Item name="Service 2a" depth="5" navid="131" type="16"/>
<Item name="Service 2b" depth="5" navid="132" type="16"/>
<Item name="Service 2c" depth="5" navid="133" type="16"/>
<Item name="Service 2d" depth="5" navid="134" type="16"/>
</Item>
</Item>
</Item>
</Item>
</Navigation>

to something like

<h1>Consumer</h1>
<h2>Products</h2>
<ul>
<li>Products 1a</li>
<li>Products 1b
<ul>
<li>1b1</li>
<li>1b2</li>
</ul>
</li>
<li>Products 1c</li>
<li>Item 4</li>
<li>Products 1d
<ul>
<li>1d1</li>
<li>1d2</li>
</ul>
</li>
</ul>

obviously I'd need to pass in a starting point i.e. NavID = 2 or 125

Can anyone help, I seem to be going around in circles.

Thanks

Tony
 
Sorry forgot the tags

Can anyone help, can't seem to completely sus this xslt out.
Code:
<Navigation>
 <Item name="Home" depth="1" navid="1" type="16">
  <Item name="Consumers" depth="2"  navid="2" type="128">
   <Item name="Products" depth="3"  navid="3" type="64">
    <Item name="Products 1a" depth="4" navid="4" type="16"/>
    <Item name="Products 1b" depth="4" navid="5" type="16">
     <Item name="1b1" depth="5"  navid="6" type="16"/>
     <Item name="1b2" depth="5" navid="7" type="16"/>
    </Item>
    <Item name="Products 1c" depth="4"  navid="8" type="16"/>
    <Item name="Products 1d" depth="4" navid="9" type="16">
     <Item name="1d1" depth="5" navid="10" type="16"/>
     <Item name="1d2" depth="5" navid="11" type="16"/>
    </Item>
    <Item name="Products 1e" depth="4" navid="12" type="16"/>
   </Item>
  </Item>
  <Item name="Non-Consumers" depth="2"  navid="125" type="128">
   <Item name="Services" depth="3"  navid="126" type="64">
    <Item name="Service 1" depth="4" navid="127" type="16">
     <Item name="Service 1a" depth="5" navid="128" type="16"/>
     <Item name="Service 1b" depth="5" navid="129" type="16"/>
    </Item>
    <Item name="Service 2" depth="4"  navid="130" type="16">
     <Item name="Service 2a" depth="5" navid="131" type="16"/>
     <Item name="Service 2b" depth="5" navid="132" type="16"/>
     <Item name="Service 2c" depth="5"  navid="133" type="16"/>
     <Item name="Service 2d" depth="5" navid="134" type="16"/>
    </Item>
   </Item>
  </Item>
 </Item>
</Navigation>

to something like

Code:
<h1>Consumer</h1>
<h2>Products</h2>
<ul>
 <li>Products 1a</li>
 <li>Products 1b
 <ul>
  <li>1b1</li>
  <li>1b2</li>
 </ul>
 </li>
 <li>Products 1c</li>
 <li>Item 4</li>
 <li>Products 1d
 <ul>
  <li>1d1</li>
  <li>1d2</li>
 </ul>
 </li>
</ul>

obviously I'd need to pass in a starting point i.e. NavID = 2 or 125

Can anyone help, I seem to be going around in circles.

Thanks

Tony
 
I take it that the attribute 'depth' defines the way you want the node to be displayed?

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="html" encoding="Windows-1252" />
   <xsl:param name="NAV" select="'2'" />

   <xsl:template match="/">
      <html>
         <body>
            <xsl:apply-templates select="//Item[@navid=$NAV]" />
         </body>
      </html>
   </xsl:template>

   <xsl:template match="Item[@depth=2]">
      <h1>
         <xsl:value-of select="@name" />
      </h1>
      <xsl:apply-templates select="*" />
   </xsl:template>

   <xsl:template match="Item[@depth=3]">
      <h2>
         <xsl:value-of select="@name" />
      </h2>
      <xsl:if test="*">
         <ul>
            <xsl:apply-templates select="*" />
         </ul>
      </xsl:if>
   </xsl:template>

   <xsl:template match="Item[@depth &gt; 3]">
      <li>
         <xsl:value-of select="@name" />
         <xsl:if test="*">
            <ul>
               <xsl:apply-templates select="*" />
            </ul>
         </xsl:if>
      </li>
   </xsl:template>
</xsl:stylesheet>
 
Jel (or anyone :) )

What about extending this such that only the next level (in context was shown?

By this I mean that say, 127 (service1) was selected, the HTML rendered would be all top level elements and the only expanded element would be service1 and its immediate children?

e.g. (using 127)
Code:
Consumers
Non-Consumers
	Services
		Service 1
			Service 1a
			Service 1b
		Service 2

e.g. (using 126)
Code:
Consumers
Non-Consumers
	Services
		Service 1
		Service 2

e.g. (using 125)
Code:
Consumers
Non-Consumers
	Services

and so on :)

Cheers m (this is related to a post I've just made at
Mark Saunders :)
 
That's not too hard, because you can always find out which node contains the node you're looking for.
If you mess with this fragment a little, you'll work it out:
Code:
<xsl:choose>
  <xsl:when test="@navid = $NAV">
  <!-- selected node: parse childnodes -->
    <xsl:apply-templates select="*" />
  </xsl:when>

  <xsl:otherwise>
  <!-- only parse childnode that somewhere contains the desired node -->
    <xsl:apply-templates select="*[.//@navid=$NAV]" />
  </xsl:otherwise>
</xsl:choose>

The one in your other post is a lot more difficult: if the nodes are not nested but only refer to eachother using parent-ids it realy is a hell of a job to construct a tree.
Although it can be done, I would not advise you to try, unless you want to have headaches for the rest of the week...
 
I'll look at this and if possible will see to change the structure of the xml as advised in my other post.

thanks very much indeed!

mark

Mark Saunders :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top