0

几天来我一直在绞尽脑汁,而对于我的一生,我无法弄清楚如何做到这一点。采用以下 XML:

<Root>
   <container>
        <widget type="nav">
            <links>
                <tab type="label" header="Top Level Tab1"/>
                <tab type="page" header="submenu of tab1"/>
                <tab type="page" header="submenu of tab1"/>
                <tab type="page" header="submenu of tab1"/>
                <tab type="label" header="Top Level Tab2"/>
                <tab type="page" header="submenu of tab2"/>
                <tab type="page" header="submenu of tab2"/>
                <tab type="page" header="submenu of tab2"/>
                <tab type="page" header="submenu of tab2"/>
                <tab type="page" header="submenu of tab2"/>
                <tab type="label" header="Top Level Tab3"/>
                <tab type="page" header="submenu of tab3"/>
                <tab type="page" header="submenu of tab3"/>
                <tab type="page" header="submenu of tab3"/>
                <tab type="page" header="submenu of tab3"/>
                <tab type="page" header="submenu of tab3"/>
            </links>
        </widget>
   </container>
</root>

我需要做的是将其转换为导航区域,在每个选项卡上使用“标签”类型分隔,并将以下兄弟姐妹放在其下。例如

<ul>
    <li>
        <a href="link for label">Top Level Header1</a>
        <div class="submenu">
            <ul>
                <li>Submenu Link 1</li>
                <li>Submenu Link 1</li>
                <li>Submenu Link 1</li>
            </ul>
        </div>
    </li>
    <li>
        <a href="link for label">Top Level Header2</a>
        <div class="submenu">
            <ul>
                <li>Submenu Link 2</li>
                <li>Submenu Link 2</li>
                <li>Submenu Link 2</li>
            </ul>
        </div>
    </li>
    <li>
        <a href="link for label">Top Level Header3</a>
        <div class="submenu">
            <ul>
                <li>Submenu Link 3</li>
                <li>Submenu Link 3</li>
                <li>Submenu Link 3</li>
            </ul>
        </div>
    </li>
</ul>

任何指导肯定会受到赞赏

4

1 回答 1

1

那么 XPath 允许您选择具有相应轴的以下或前面的兄弟元素。为了提高效率,您可以使用密钥:

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="subs" match="widget[@type = 'nav']/links/tab[@type = 'page']"
  use="generate-id(preceding-sibling::tab[@type = 'label'][1])"/>

<xsl:output method="html" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="container/widget[@type = 'nav']">
  <ul>
    <xsl:apply-templates select="links/tab[@type = 'label' and starts-with(@header, 'Top Level')]"/>
  </ul>
</xsl:template>

<xsl:template match="links/tab[@type = 'label' and starts-with(@header, 'Top Level')]">
  <li>
    <a href="link for label">
      <xsl:value-of select="@header"/>
    </a>
    <div class="submenu">
      <ul>
        <xsl:apply-templates select="key('subs', generate-id())"/>
      </ul>
    </div>
  </li>
</xsl:template>

<xsl:template match="links/tab[@type = 'page']">
  <li>
    <xsl:value-of select="@header"/>
  </li>
</xsl:template>

</xsl:stylesheet>

该样式表转换

<Root>
   <container>
        <widget type="nav">
            <links>
                <tab type="label" header="Top Level Tab1"/>
                <tab type="page" header="submenu of tab1"/>
                <tab type="page" header="submenu of tab1"/>
                <tab type="page" header="submenu of tab1"/>
                <tab type="label" header="Top Level Tab2"/>
                <tab type="page" header="submenu of tab2"/>
                <tab type="page" header="submenu of tab2"/>
                <tab type="page" header="submenu of tab2"/>
                <tab type="page" header="submenu of tab2"/>
                <tab type="page" header="submenu of tab2"/>
                <tab type="label" header="Top Level Tab3"/>
                <tab type="page" header="submenu of tab3"/>
                <tab type="page" header="submenu of tab3"/>
                <tab type="page" header="submenu of tab3"/>
                <tab type="page" header="submenu of tab3"/>
                <tab type="page" header="submenu of tab3"/>
            </links>
        </widget>
   </container>
</Root>

进入

<ul>
   <li><a href="link for label">Top Level Tab1</a><div class="submenu">
         <ul>
            <li>submenu of tab1</li>
            <li>submenu of tab1</li>
            <li>submenu of tab1</li>
         </ul>
      </div>
   </li>
   <li><a href="link for label">Top Level Tab2</a><div class="submenu">
         <ul>
            <li>submenu of tab2</li>
            <li>submenu of tab2</li>
            <li>submenu of tab2</li>
            <li>submenu of tab2</li>
            <li>submenu of tab2</li>
         </ul>
      </div>
   </li>
   <li><a href="link for label">Top Level Tab3</a><div class="submenu">
         <ul>
            <li>submenu of tab3</li>
            <li>submenu of tab3</li>
            <li>submenu of tab3</li>
            <li>submenu of tab3</li>
            <li>submenu of tab3</li>
         </ul>
      </div>
   </li>
</ul>
于 2013-01-04T16:18:58.460 回答