Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 27th, 2003, 05:12 AM
Registered User
 
Join Date: Aug 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML/XSL BreadCrumb Trail

Im having problems with developing a solution for breadcrumb trail (navigation path - home > page 1 > page 1.1 > etc...) with xml data and xsl.

The data is stored in a sql server and is placed in a XMLDOM object......objRS.Save objXML, adPersistXML. The XML data is just a recordset containing all the pages for the website.

Each row in the XML contains the following attributes/values:
- PageID
- ParentID
- PageTitle

<rs:data>
<z:row PageID="1000" ParentID="0" PageTitle="Home"/>
<z:row PageID="1001" ParentID="1000" PageTitle="Page1"/>
<z:row PageID="1002" ParentID="1000" PageTitle="Page2"/>
<z:row PageID="1003" ParentID="1001" PageTitle="Page1.1"/>
....
....
</rs:data>

In the XSL(T) object i have included some ASP parameters, the current PageID and the parent PageID for the current page.

How should I design the XSL template to be able to get the following output (if the current PageID = 1003):

Home > Page1 > Page1.1

Hope to hear from you!!

 
Old August 27th, 2003, 07:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Interesting! ;)

Assuming the XML source is:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<rs:data xmlns:rs="namespace_for_rs" xmlns:z="namespace_for_z"> 
    <z:row PageID="1000" ParentID="0" PageTitle="Home"/> 
    <z:row PageID="1001" ParentID="1000" PageTitle="Page1"/> 
    <z:row PageID="1002" ParentID="1000" PageTitle="Page2"/> 
    <z:row PageID="1003" ParentID="1001" PageTitle="Page1.1"/> 
</rs:data>
the stylesheet is:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:rs="namespace_for_rs"
 xmlns:z="namespace_for_z"
 extension-element-prefixes="rs z"
 >
    <xsl:param name="page-id" select="1000"/>

    <xsl:template match="/">
        <xsl:call-template name="output-bread-crumb">
            <xsl:with-param name="current-row" select="/rs:data/z:row[@PageID = $page-id]"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="output-bread-crumb">
        <xsl:param name="current-row"/>
        <xsl:choose>
            <xsl:when test="$current-row/@ParentID != 0">
                <xsl:call-template name="output-bread-crumb">
                    <xsl:with-param name="current-row" select="$current-row/../z:row[@PageID = $current-row/@ParentID]"/>
                </xsl:call-template>
                &gt; <xsl:value-of select="$current-row/@PageTitle"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$current-row/@PageTitle"/>
            </xsl:otherwise> 
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
Regards,
Armen
 
Old August 27th, 2003, 07:37 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I came up with a similar solution:
Code:
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:rs="rs-ns" xmlns:z="z-ns">
<xsl:output omit-xml-declaration="yes"/>
<xsl:param name="separator" select="'&gt;'"/>
<xsl:param name="PageID">1003</xsl:param>
<xsl:template match="/">
  <xsl:call-template name="processRow">
    <xsl:with-param name="currentNode" select="rs:data/z:row[@PageID = $PageID]"/>
  </xsl:call-template>
</xsl:template>
<xsl:template name="processRow">
  <xsl:param name="currentNode"/>  
  <xsl:param name="path"></xsl:param>
  <xsl:variable name="title" select="$currentNode/@PageTitle"/>
  <xsl:variable name="ParentID" select="$currentNode/@ParentID"/>
  <xsl:variable name="fullPath" select="concat($separator, $title, $path)"/>
  <xsl:choose>
    <xsl:when test="$ParentID = 0">
      <xsl:value-of select="$fullPath"/>  
    </xsl:when>
    <xsl:otherwise>

      <xsl:call-template name="processRow">
        <xsl:with-param name="currentNode" select="/rs:data/z:row[@PageID = $ParentID]"/>
        <xsl:with-param name="path" select="$fullPath"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
</xsl:stylesheet>

--

Joe
 
Old August 27th, 2003, 08:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

LOL, and I came up with this one (I left out the namespaces coz I'm lazy :))
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:call-template name="bc">
            <xsl:with-param name="rowid" select="1003"/>
        </xsl:call-template>
    </xsl:template>
    <xsl:template name="bc">
        <xsl:param name="rowid"/>
        <xsl:if test="/data/row[@PageID=$rowid]/@ParentID!='0'">
            <xsl:call-template name="bc">
                <xsl:with-param name="rowid" select="/data/row[@PageID=$rowid]/@ParentID"/>
            </xsl:call-template>
            &gt;
        </xsl:if>
        <xsl:value-of select="/data/row[@PageID=$rowid]/@PageTitle"/>
    </xsl:template>
</xsl:stylesheet>
 
Old August 27th, 2003, 09:26 AM
Registered User
 
Join Date: Aug 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is how the XML source look like:

<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
- <s:Schema id="RowsetSchema">
- <s:ElementType name="row" content="eltOnly" rs:CommandTimeout="30">
- <s:AttributeType name="PageID" rs:number="1">
  <s:datatype dt:type="int" dt:maxLength="4" rs:precision="10" rs:fixedlength="true" rs:maybenull="false" />
  </s:AttributeType>
- <s:AttributeType name="ParentID" rs:number="2" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="int" dt:maxLength="4" rs:precision="10" rs:fixedlength="true" />
  </s:AttributeType>
- <s:AttributeType name="PageTitle" rs:number="3" rs:nullable="true" rs:writeunknown="true">
  <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="200" />
  </s:AttributeType>
  <s:extends type="rs:rowbase" />
  </s:ElementType>
  </s:Schema>
- <rs:data>
  <z:row PageID="1000" ParentID="0" PageTitle="Forsiden" />
  <z:row PageID="1001" ParentID="1000" PageTitle="Side 1" />
  <z:row PageID="1002" ParentID="1000" PageTitle="Side 2" />
  <z:row PageID="1003" ParentID="1000" PageTitle="Side 3" />
  <z:row PageID="1004" ParentID="1000" PageTitle="Side 4" />
  <z:row PageID="1005" ParentID="1001" PageTitle="Side 1.1" />
  <z:row PageID="1006" ParentID="1001" PageTitle="Side 1.2" />
  <z:row PageID="1007" ParentID="1002" PageTitle="Side 2.1" />
  <z:row PageID="1008" ParentID="1002" PageTitle="Side 2.2" />
  <z:row PageID="1009" ParentID="1004" PageTitle="Side 4.1" />
  <z:row PageID="1010" ParentID="1009" PageTitle="Side 4.1.1" />
  <z:row PageID="1011" ParentID="1004" PageTitle="Side 4.2" />
  <z:row PageID="1012" ParentID="1003" PageTitle="Side 3.1" />
  <z:row PageID="1013" ParentID="1003" PageTitle="Side 3.2" />
  <z:row PageID="1014" ParentID="1003" PageTitle="Side 3.3" />
  <z:row PageID="1015" ParentID="1003" PageTitle="Side 3.4" />
  <z:row PageID="1016" ParentID="1003" PageTitle="Side 3.5" />
  </rs:data>
  </xml>

 
Old August 27th, 2003, 10:07 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

So in my example where it reads
Code:
<xsl:with-param name="currentNode" select="/rs:data/z:row[@PageID = $ParentID]"/>
in the template named "processRow" change to
Code:
<xsl:with-param name="currentNode" select="/xml/rs:data/z:row[@PageID = $ParentID]"/>
or
Code:
<xsl:with-param name="currentNode" select="//z:row[@PageID = $ParentID]"/>

--

Joe
 
Old August 27th, 2003, 02:02 PM
Registered User
 
Join Date: Aug 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your solutions.


 
Old August 27th, 2003, 02:18 PM
Registered User
 
Join Date: Aug 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by joefawcett
 So in my example where it reads
Code:
<xsl:with-param name="currentNode" select="/rs:data/z:row[@PageID = $ParentID]"/>
in the template named "processRow" change to
Code:
<xsl:with-param name="currentNode" select="/xml/rs:data/z:row[@PageID = $ParentID]"/>
or
Code:
<xsl:with-param name="currentNode" select="//z:row[@PageID = $ParentID]"/>

--

Joe
Im using the solution you suggested, but im not quite sure how to
include link (a href) on each of the PageTitle(s) in the trail.

Any idea?

.fortgjort

 
Old August 28th, 2003, 04:26 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Try this:
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:rs="urn:schemas-microsoft-com:rowset" 
                              xmlns:z="#RowsetSchema"
                              exclude-result-prefixes="rs z">
  <xsl:output omit-xml-declaration="yes"/>

  <xsl:param name="PageID">1010</xsl:param>

  <xsl:param name="separator" select="' &gt; '"/>

  <xsl:param name="baseUrl" select="'http://mySite/'"/>

  <xsl:template match="/xml">  
    <xsl:call-template name="processRow">
      <xsl:with-param name="currentNode" select="rs:data/z:row[@PageID = $PageID]"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="processRow">
    <xsl:param name="currentNode"/>  
    <xsl:param name="path"></xsl:param>    
    <xsl:variable name="title" select="$currentNode/@PageTitle"/>    
    <xsl:variable name="ParentID" select="$currentNode/@ParentID"/>
    <xsl:variable name="fullPath">
      <xsl:call-template name="getUrl">
        <xsl:with-param name="currentNode" select="$currentNode"/>  
      </xsl:call-template>  
    </xsl:variable>      
    <xsl:choose>
      <xsl:when test="$ParentID = 0">
        <a href="{$fullPath}"><xsl:value-of select="$title"/></a> 
      </xsl:when>
      <xsl:otherwise>       
        <xsl:call-template name="processRow">
          <xsl:with-param name="currentNode" select="//z:row[@PageID = $ParentID]"/>        
        </xsl:call-template>
        <xsl:value-of select="$separator"/><a href="{$fullPath}"><xsl:value-of select="$title"/></a>
      </xsl:otherwise>
    </xsl:choose>    
  </xsl:template>

  <xsl:template name="getUrl">
    <xsl:param name="currentNode"/>
    <xsl:call-template name="addToPath">
      <xsl:with-param name="currentNode" select="$currentNode"/>      
    </xsl:call-template>      
  </xsl:template>

  <xsl:template name="addToPath">
    <xsl:param name="currentNode"/>
    <xsl:param name="currentPath" select="''"/>

    <xsl:variable name="fileName" select="$currentNode/@PageTitle"/>    
    <xsl:choose>
      <xsl:when test="$currentNode/@ParentID = 0">
        <xsl:value-of select="concat($baseUrl, $fileName, $currentPath)"/> 
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="addToPath">
          <xsl:with-param name="currentNode" select="//z:row[@PageID = $currentNode/@ParentID]"/>
          <xsl:with-param name="currentPath" select="concat('/', $fileName, $currentPath)"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>  
  </xsl:template>
</xsl:stylesheet>
In the last template there is a variable named "fileName". I think it would be better if you had two columns in your original data for the real file name and the file display name. At the moment you just have FileTitle which means the display has to show unfriendly names such as "mainMenu.asp" instead of "Main Menu" or you have to name files with awkward names.
Remember to change the baseUrl at the top of stylesheet if you don't need the site included in the links.





--

Joe





Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting Source Xml into Target Xml Using XSL. alapati.sasi XSLT 3 May 14th, 2007 10:54 AM
XML To XML, using XSL & XSD supercop75 XSLT 1 April 8th, 2006 02:48 AM
xml and xsl templates as input to xslt gives xml rameshnarayan XSLT 5 August 3rd, 2005 01:58 AM
Breadcrumb bluemat XSLT 2 October 7th, 2004 04:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.