Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8631941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:17:08+00:00 2026-06-12T09:17:08+00:00

I have difficulties to write a template that generates a breadcrumb trial out of

  • 0

I have difficulties to write a template that generates a breadcrumb trial out of a node structure. It is not working correctly up to now, there is some flaw in my thinking how it should walk the item path.

Consider the following page structure:

<!-- ===== SITE PAGE STRUCTURE ===================================== -->
<index>
   <item section="home" id="index"></item>
   <item section="service" id="index">
      <item id="content-management-systems">
         <item id="p1-1"/>
         <item id="p1-2"/>
         <item id="p1-3"/>
      </item>
      <item id="online-stores"></item>
      <item id="search-engines-and-ir"></item>
      <item id="web-applications"></item>
   </item>

   <item section="solutions" id="index">
      <item id="document-clustering"></item>
   </item>
   <item section="company" id="index">
      <item section="company" id="about"></item>
      <item section="company" id="philosophy" ></item>
      ...
   </item>
...
</item>

This site index represents a site-structure of xml content pages in its hierarchy (consider it to be a menu). It contains of sections, that represent the site sections just as home, company, service, solutions, etc. These sections can contain sub-sections with pages, or just regular content pages. A content page (its xml contents such as title, text content, etc) is identified by the @id attribute in the item tree. The @id attribute mainly is used to fetch the content of the entire page that will be rendered to html.
The breadcrumb template uses the item node @id attribute to get the title of the page (which will be shown in the breadcrumb trail).

I try to implement the following template that walks the tree by checking the target section attribute @section and the target page attribute @id in the tree. I expect it to walk the axis down until the target item_target is found by comparing the ancestors @section attribute and the @id with $item_target of each node in that axis.

For example: Attribute *$item_section=service* and the page id *target item_target=p1-1* should now recursively “walk” to the section branch “service” (depth 1), check if the target page @id is found on this level. In this case it is not found, so it makes the next recurive call (via apply-templates) to the next item node level (in this case it would be content-management-systems, there the target item page p1-1 is found, so the trail process is finished:

The result should like this:

home >> service >> content management systems >> p1-1

But unfortunately it is not working correct, at least not in every case. Also maybe it can be solved more easily. I try to implement it as an recursive template that walks from the top (level 0) to the target page (item node) as a leaf.

    <!-- walk item path to generate a breadcrumb trail -->
    <xsl:template name="breadcrumb">
        <a>
            <xsl:attribute name="href">
                <xsl:text>/</xsl:text>
                <xsl:value-of select="$req-lg"/>
                <xsl:text>/home/index</xsl:text>
            </xsl:attribute>
            <xsl:value-of select="'Home'"/>
        </a>

        <xsl:apply-templates select="$content/site/index" mode="Item-Path">
            <xsl:with-param name="item_section" select="'service'"/>
            <xsl:with-param name="item_target" select="'search-engines-and-ir'"/>
            <xsl:with-param name="depth" select="0"/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="item" mode="Item-Path">
        <xsl:param name="item_section" />
        <xsl:param name="item_target" />
        <xsl:param name="depth" />
        <!--
        depth=<xsl:value-of select="$depth"/>
        count=<xsl:value-of select="count(./node())"/><br/>
-->
        <xsl:variable name="cur-id" select="@id"/>
        <xsl:variable name="cur-section" select="@section"/>
        <xsl:choose>    
            <xsl:when test="@id=$item_target">
                &gt;&gt;
                <a>
                    <xsl:attribute name="href">
                        <xsl:text>/</xsl:text>
                                            <!-- req-lg: global langauge variable -->
                        <xsl:value-of select="$req-lg"/>
                        <xsl:text>/</xsl:text>
                        <xsl:value-of select="$item_section"/>
                        <xsl:text>/</xsl:text>
                        <xsl:if test="$depth = 2">
                            <xsl:value-of select="../@id"/>
                            <xsl:text>/</xsl:text>
                        </xsl:if>
                        <xsl:value-of select="@id"/>
                    </xsl:attribute>
                    <xsl:value-of 
                        select="$content/page[@id=$cur-id]/title"/>
                </a>
            </xsl:when>
            <xsl:otherwise>
                <xsl:if test="ancestor-or-self::item/@section = $item_section and count(./node()) > 0">
                &gt;&gt;:
                <a>
                    <xsl:attribute name="href">
                        <xsl:text>/</xsl:text>
                                            <!-- req-lg: global langauge variable -->
                        <xsl:value-of select="$req-lg"/>
                        <xsl:text>/</xsl:text>
                        <xsl:value-of select="$item_section"/>
                        <xsl:text>/</xsl:text>
                        <xsl:if test="$depth = 2">
                            <xsl:value-of select="../@id"/>
                            <xsl:text>/</xsl:text>
                        </xsl:if>
                        <xsl:value-of select="@id"/>
                    </xsl:attribute>
                    <xsl:value-of 
                        select="$content/page[@id=$cur-id and @section=$item_section]/title"/>
                </a>
                </xsl:if>
            </xsl:otherwise>
        </xsl:choose>

        <xsl:apply-templates select="item" mode="Item-Path">
            <xsl:with-param name="item_section" select="$item_section"/>
            <xsl:with-param name="item_target" select="$item_target"/>
            <xsl:with-param name="depth" select="$depth + 1"/>
        </xsl:apply-templates>

    </xsl:template>

So as the hardcoded parameters in the template breadcrumb, target section = ‘service’ and target page = ‘search-engines-and-ir’, I expect an output like

home >> service >> search-engines-and-ir

But the output is

home >> service >> content-management-systems >> search-engines-and-ir

which is obviously not correct.

Can anybody give me a hint how to correct this issue? It would be even more elegant to avoid that depth checking, but up to now I cannot think of a other way, I am sure there is a more elegant solution.

I work with XSLT 1.0 (libxml via PHP5).

Hope my question is clear enough, if not, please ask 🙂 Thanks for the help in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:17:08+00:00Added an answer on June 12, 2026 at 9:17 am

    As simple as this:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="text"/>
    
     <xsl:key name="kNodeById" match="item" use="@id"/>
    
     <xsl:template match="/">
      <xsl:text>home</xsl:text>
      <xsl:call-template name="findPath">
       <xsl:with-param name="pStart" select="'service'"/>
       <xsl:with-param name="pEnd" select="'search-engines-and-ir'"/>
      </xsl:call-template>
     </xsl:template>
    
     <xsl:template name="findPath">
      <xsl:param name="pStart"/>
      <xsl:param name="pEnd"/>
    
      <xsl:for-each select=
      "key('kNodeById', $pEnd)
           [ancestor::item[@section=$pStart]]
            [1]
             /ancestor-or-self::item
                    [not(descendant::item[@section=$pStart])]
      ">
    
       <xsl:value-of select=
        "concat('>>', @id[not(../@section)], @section)"/>
      </xsl:for-each>
     </xsl:template>
    </xsl:stylesheet>
    

    the wanted, correct result is produced:

    home>>service>>search-engines-and-ir
    

    Do Note:

    1. This solution prints the breadcrumb from any node — anywhere in the hierarchy to any of its descendent nodes — anywhere in the hierarchy. More precisely, for the first item (in document order) with id attribute equal to $pEnd, the breadcrumb is generated from its inner-most ancestor whose section attribute is equal to $pStart — to that item element.

    2. This solution should be much more efficient than any solution using //, because we are using a key to locate efficiently the “end” item element.


    II. XSLT 2.0 solution:

    Much shorter and easier — an XPathe 2.0 single expression:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="text"/>
    
     <xsl:key name="kNodeById" match="item" use="@id"/>
    
     <xsl:template match="/">
      <xsl:value-of select=
      "string-join(
           (
            'home',
           key('kNodeById', $pEnd)
              [ancestor::item[@section=$pStart]]
                  [1]
                    /ancestor-or-self::item
                    [not(descendant::item[@section=$pStart])]
                           /(@id[not(../@section)], @section)[1]
    
            ),
          '>>'
            )
      "/>
     </xsl:template>
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to programing and I have some difficulties getting AlertDialog working. I
I have a data set of item difficulties that correspond to items on a
I have some difficulties understanding how the access method Do not move cursor automatically
I have difficulties developing 4x1 widget for Android. This is the appprovider <?xml version=1.0
I know this question had been asked a hundred times, but I have difficulties
I am reading the book: Intel Threading Building Blocks. I often have difficulties understanding
I have some difficulties with understanding BindingSource's behaviour. Let's look at following example: Creating
I have some difficulties in my project. I got the RGB values from a
I have some difficulties for using Ruby block, passing in a method. As in
I'm have some difficulties here, I am unable to successfully call a method which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.