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!

xslt advice? 1

Status
Not open for further replies.

markSaunders

Programmer
Jun 23, 2000
196
0
0
GB
Any ideas on resources that would lead me to the answer to the following:

I have an xml file (which I control so can add attributes etc. as necessary)

Code:
<page id="1" parent=0">
<page id="2" parent=0">
<page id="3" parent=2">
<page id="4" parent=2">
<page id="5" parent=4">
<page id="6" parent=4">
<page id="7" parent=4">
<page id="8" parent=2">
<page id="9" parent=2">
<page id="10" parent=0">
<page id="11" parent=0">
<page id="12" parent=11">
<page id="13" parent=11">
<page id="14" parent=0">
<page id="15" parent=0">

Thus, and example of the full structure indicated above would be:

Code:
1
2
	3
	4
		5
		6
		7
	8
	9
10
11
	12
	13
14
15

As you've probably guessed this is for a left nav! but I need to offer upto three levels where the page ID will drive the transformation (I can certainly access the page ID and am assuming I can use a parameter in the XLST), sucj that:

=> where page ID = 6
Code:
1
2
	3
	4
		5
		*6
		7
10
11
14
15

=> where page ID = 12
Code:
1
2
10
11
	*12
	13
14
15

=> where page ID = 2
Code:
*1
2
10
11
14
15

Any help would be splendid!!!

Thanks.
M

Mark Saunders :)
 
OK, just for fun...
I warned you it would be easier if the xml is already nested...
Code:
<root>
   <page id="1" parent="0">ONE</page>
   <page id="2" parent="0">TWO</page>
   <page id="3" parent="2">THREE</page>
   <page id="4" parent="2">FOUR</page>
   <page id="5" parent="4">FIVE</page>
   <page id="6" parent="4">SIX</page>
   <page id="7" parent="4">SEVEN</page>
   <page id="8" parent="2">EIGHT</page>
   <page id="9" parent="2">NINE</page>
   <page id="10" parent="0">TEN</page>
   <page id="11" parent="0">ELEVEN</page>
   <page id="12" parent="11">TWELVE</page>
   <page id="13" parent="11">THIRTEEN</page>
   <page id="14" parent="0">FOURTEEN</page>
   <page id="15" parent="0">FIFTEEN</page>
</root>
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:variable name="pageId" select="6" />

   <xsl:template match="/">
      <html>
         <head>
         </head>

         <body>
            <xsl:call-template name="makeLevel">
               <xsl:with-param name="selectedPage" select="//page[@id=$pageId]" />
            </xsl:call-template>
         </body>
      </html>
   </xsl:template>

   <xsl:template name="makeLevel">
      <xsl:param name="selectedPage" />
      <xsl:param name="insert" />

      <xsl:choose>
         <xsl:when test="$selectedPage">
            <xsl:call-template name="makeLevel">
               <xsl:with-param name="selectedPage" select="//page[@id=$selectedPage/@parent]" />

               <xsl:with-param name="insert">
                  <table border="1">
                     <xsl:for-each select="//page[@parent=$selectedPage/@parent]">
                        <tr>
                           <td>
                              <xsl:if test="@id = $pageId">*</xsl:if>

                              <xsl:value-of select="." />
                           </td>
                        </tr>
                        <xsl:if test="@id=$selectedPage/@id and $insert">
                           <tr>
                              <td width="10">
                              </td>
                              <td>
                                 <xsl:copy-of select="$insert" />
                              </td>
                           </tr>
                        </xsl:if>
                     </xsl:for-each>
                  </table>
               </xsl:with-param>
            </xsl:call-template>
         </xsl:when>

         <xsl:otherwise>
            <xsl:copy-of select="$insert" />
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>
</xsl:stylesheet>
 
I'll try this out now - thanks very much for the input!

mark

Mark Saunders :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top