使用XSL在页面导航中显示子页面

时间:2013-12-05 05:46:41

标签: xslt

我正在使用XSL列出出现在LHS导航列表上的XML上的页面和子页面。我需要tree/page/page[2]/page[2]/name路径下的所有网页名称。

不确定在纠正错误方面是否错误。

目前仅显示第一级页面。

<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:

感谢您的回复。预期的输出是我需要在decendant_level =“1”页面下显示decendant_level =“2”页面。截至目前,只有所有decendant_level =“1”页面都显示在LHS菜单上。

输入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>

1 个答案:

答案 0 :(得分:1)

看起来您只需要添加一行来递归调用现有的“页面”模板。

您可以在关闭 li 标记之前添加此声明。

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

因此,这会检查是否有任何页面元素,其中decendant_level设置为“2”,如果是,则启动新列表。

试试这个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>