Displaying sub pages in page navigation using XSL

2019-08-06 21:33发布

I am working on listing pages and sub pages appearing on the XML on the LHS Navigation list using XSL. I need all pages name under tree/page/page[2]/page[2]/name path.

Not sure if am wrong in putting things right.

Currently this is displaying only the first level pages.

<xsl:template match="/">
<textarea><xsl:copy-of select="*"/></textarea>

 <div class="BOXContentPane nopadd-BOXContentPane clearfix">
      <ul id="nav-main" class="navigation qnav">
         <xsl:apply-templates select="//tree/page/page"/>
      </ul>
</div>

</xsl:template>

<xsl:template match="page">
<li>

    <xsl:attribute name="class">
    <xsl:choose>
      <xsl:when test="current_page">link active</xsl:when> 
      <xsl:otherwise>link</xsl:otherwise>
    </xsl:choose>
  </xsl:attribute>

  <a href="{href}" title="{title}">
    <xsl:if test="@current_page='true' or current_page_ancestor">
      <xsl:attribute name="class">active</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="name" disable-output-escaping="yes"/>


  </a>
 </li>


 </xsl:template>


 </xsl:stylesheet>

@Tim C:

Thanks for your reply. Expected output is that I need decendant_level="2" pages to be displayed under decendant_level="1" pages. As of now only all decendant_level="1" pages are showing on the LHS menu.

Input XML:

  <?xml version="1.0" encoding="UTF-8"?>
  <portlet_output xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:noNamespaceSchemaLocation="http://www.vyre.com/navigator_portlet.xsd">
<tree>
    <site id="2">
        <name>onamet</name>
    </site>
    <page id="128" access_controlled="false" site_level="1" secure="maybe" current_page="true">
        <name>About onam</name>
        <description>onamet about onam page</description>
        <current_page />
        <href>/about_onam/</href>
        <hreftitle>onamet - about onam</hreftitle>
        <page_mode>saved_search</page_mode>
        <publish_status>published</publish_status>
        <page id="210" access_controlled="false" site_level="2" secure="maybe" decendant_level="1">
            <name>Basic Information</name>
            <description>basic_info</description>
            <current_page_decendant />
            <href>/about_onam/basic_info/</href>
            <hreftitle>Basic Information</hreftitle>
            <page_mode>saved_search</page_mode>
            <publish_status>published</publish_status>
        </page>
        <page id="211" access_controlled="false" site_level="2" secure="maybe" decendant_level="1">
            <name>History</name>
            <description>history</description>
            <current_page_decendant />
            <href>/about_onam/history/</href>
            <hreftitle>History</hreftitle>
            <page_mode>saved_search</page_mode>
            <publish_status>published</publish_status>
            <page id="212" access_controlled="false" site_level="3" secure="maybe" decendant_level="2">
                <name>Time Line</name>
                <description>time_line</description>
                <current_page_decendant />
                <href>/about_onam/history/time_line/</href>
                <hreftitle>Time Line</hreftitle>
                <page_mode>saved_search</page_mode>
                <publish_status>published</publish_status>
            </page>
            <page id="215" access_controlled="false" site_level="3" secure="maybe" decendant_level="2">
                <name>Historical Sites</name>
                <description>historical_sites</description>
                <current_page_decendant />
                <href>/about_onam/history/historical_sites/</href>
                <hreftitle>Historical Sites</hreftitle>
                <page_mode>saved_search</page_mode>
                <publish_status>published</publish_status>
            </page>
        </page>
        <page id="216" access_controlled="false" site_level="2" secure="maybe" decendant_level="1">
            <name>Geography</name>
            <description>geography</description>
            <current_page_decendant />
            <href>/about_onam/geography/</href>
            <hreftitle>Geography</hreftitle>
            <page_mode>saved_search</page_mode>
            <publish_status>published</publish_status>
            <page id="220" access_controlled="false" site_level="3" secure="maybe" decendant_level="2">
                <name>Geography</name>
                <description>geography</description>
                <current_page_decendant />
                <href>/about_onam/geography/geography/</href>
                <hreftitle>Geography</hreftitle>
                <page_mode>saved_search</page_mode>
                <publish_status>published</publish_status>
            </page>
            <page id="222" access_controlled="false" site_level="3" secure="maybe" decendant_level="2">
                <name>Geology</name>
                <description>geology</description>
                <current_page_decendant />
                <href>/about_onam/geography/geology/</href>
                <hreftitle>Geology</hreftitle>
                <page_mode>saved_search</page_mode>
                <publish_status>published</publish_status>
            </page>
        </page>
        <page id="224" access_controlled="false" site_level="2" secure="maybe" decendant_level="1">
            <name>Governerate</name>
            <description>governerate</description>
            <current_page_decendant />
            <href>/about_onam/governerate/</href>
            <hreftitle>Governerate</hreftitle>
            <page_mode>saved_search</page_mode>
            <publish_status>published</publish_status>
            <page id="225" access_controlled="false" site_level="3" secure="maybe" decendant_level="2">
                <name>Wilayats</name>
                <description>wilayats</description>
                <current_page_decendant />
                <href>/about_onam/governerate/wilayats/</href>
                <hreftitle>Wilayats</hreftitle>
                <page_mode>saved_search</page_mode>
                <publish_status>published</publish_status>
            </page>
        </page>
    </page>
</tree>
</portlet_output>

标签: xslt
1条回答
戒情不戒烟
2楼-- · 2019-08-06 22:07

It looks like you simple need to add a line to call your existing "page" template recursively.

You can do this simply be adding this statement just before you close the li tag.

  <xsl:if test="page[@decendant_level='2']">
       <ul id="nav-main" class="navigation qnav">
         <xsl:apply-templates select="page"/>
      </ul>
  </xsl:if>

So, this checks if there are any page elements with decendant_level set to '2', and if so, starts a new list.

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes" />

    <xsl:template match="/">
     <div class="BOXContentPane nopadd-BOXContentPane clearfix">
          <ul id="nav-main" class="navigation qnav">
             <xsl:apply-templates select="//tree/page/page"/>
          </ul>
    </div>
    </xsl:template>

    <xsl:template match="page">
    <li>
       <xsl:attribute name="class">
        <xsl:choose>
          <xsl:when test="current_page">link active</xsl:when> 
          <xsl:otherwise>link</xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <a href="{href}" title="{title}">
        <xsl:if test="@current_page='true' or current_page_ancestor">
          <xsl:attribute name="class">active</xsl:attribute>
        </xsl:if>
        <xsl:value-of select="name" disable-output-escaping="yes"/>
      </a>
      <xsl:if test="page[@decendant_level='2']">
           <ul id="nav-main" class="navigation qnav">
             <xsl:apply-templates select="page"/>
          </ul>
      </xsl:if>
     </li>
   </xsl:template>
</xsl:stylesheet>
查看更多
登录 后发表回答