0

我正在尝试用标签包装重复system-folder元素。div我可以有两到三个system-folder元素都用div.

一个system-page元素可以是之前或之后的兄弟姐妹。

关于我需要改变什么来实现这个的任何想法?我是否需要利用前面的兄弟姐妹并进行 3 次应用模板调用?

XML

<system-index-block current-time="1370982198860" name="index-block" type="folder">

  <system-page id="3499046a0a1e023a19a05c9328c6e360">
    <name>calendar</name>
    <title>College Catalog</title>
    <display-name>Enter a display name (For Navigation)</display-name>
    <path>/web/catalog/calendar</path>
  </system-page>

  <system-folder id="348ce18d0a1e023a19a05c93941a001d">
    <name>general</name>
    <display-name>General Information</display-name>
    <path>/web/catalog/general</path>
    <system-page id="348d29520a1e023a19a05c9362978f0d">
      <name>history</name>
      <title>History of the College</title>
      <display-name>Enter a display name (For Navigation)</display-name>
      <path>/web/catalog/general/history</path>
    </system-page>
  </system-folder>

  <system-folder id="348ce18d0a1e023a19a05c93941a001d">
    <name>general</name>
    <display-name>General Information</display-name>
    <path>/web/catalog/general</path>
    <system-page id="348d29520a1e023a19a05c9362978f0d">
      <name>history</name>
      <title>History of the College</title>
      <display-name>Enter a display name (For Navigation)</display-name>
      <path>/web/catalog/general/history</path>
    </system-page>
  </system-folder>

  <system-page current="true" id="341f70a80a1e023a19a05c93a172e372">
    <name>index</name>
    <title>College Catalog</title>
    <display-name>Enter a display name (For Navigation)</display-name>
    <path>/web/catalog/index</path>
  </system-page>

</system-index-block>

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="html" encoding="utf-8"/>

  <xsl:template match="/">
    <xsl:apply-templates select="node()" />
  </xsl:template>

  <xsl:template match="system-page">

    <xsl:variable name="x">
      <xsl:element name="a">
        <xsl:if test="@current='true'">
          <xsl:attribute name="class">current-nav-link</xsl:attribute>
        </xsl:if>
        <xsl:attribute name="href"><xsl:value-of select="path"/></xsl:attribute>
        <xsl:value-of select="title" />
      </xsl:element>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="parent::system-index-block">
        <div class="nav-entry-link">
          <xsl:copy-of select="$x" />
        </div>
      </xsl:when>
      <xsl:otherwise>
        <li>
          <xsl:copy-of select="$x" />
        </li>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="system-folder">
    <h2><xsl:value-of select="display-name" /></h2>
    <ul>
      <xsl:for-each select="system-page">
        <xsl:apply-templates select="current()" />
      </xsl:for-each>
    </ul>
  </xsl:template>

</xsl:stylesheet>

输出

<div class="nav-entry-link">
  <a href="/web/catalog/calendar">College Catalog</a>
</div>

<h2>General Information</h2>
<ul>
  <li>
    <a href="/web/catalog/general/history">History of the College</a>
  </li>
  <li>
    <a href="/web/catalog/general/payment">test</a>
  </li>
  <li>
    <a href="/web/catalog/general/governance">Governance</a>
  </li>
</ul>

<h2>General Information</h2>
<ul>
  <li>
    <a href="/web/catalog/general/history">History of the College</a>
  </li>
  <li>
    <a href="/web/catalog/general/payment">test</a>
  </li>
  <li>
    <a href="/web/catalog/general/governance">Governance</a>
  </li>
</ul>

<div class="nav-entry-link">
  <a class="current-nav-link" href="/web/catalog/index">College Catalog</a>
</div>

我想像这样包装所有h2ul元素:

<div class="nav-entry-link">
  <a href="/web/catalog/calendar">College Catalog</a>
</div>
    <div class="accordion">
      <h2>test</h2>
      <ul>test</ul>
      <h2>test</h2>
      <ul>test</ul>
    </div>
<div class="nav-entry-link">
  <a href="/web/catalog/calendar">College Catalog</a>
</div>
4

1 回答 1

2

尝试这样的事情:

添加一个模板,该模板忽略具有直接前同级系统文件夹的系统文件夹。

<xsl:template match="system-folder[preceding-sibling::*[1][name() = 'system-folder']]" />

和一个生成差异的模板

<xsl:template match="system-folder">
    <div>
        <xsl:apply-templates select="." mode="following" />
    </div>
</xsl:template>

并递归调用以下兄弟姐妹。尝试这个:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="html" encoding="utf-8"/>

    <xsl:template match="/">
        <xsl:apply-templates select="node()" />
    </xsl:template>

    <xsl:template match="system-page">

        <xsl:variable name="x">
            <xsl:element name="a">
                <xsl:if test="@current='true'">
                    <xsl:attribute name="class">current-nav-link</xsl:attribute>
                </xsl:if>
                <xsl:attribute name="href">
                    <xsl:value-of select="path"/>
                </xsl:attribute>
                <xsl:value-of select="title" />
            </xsl:element>
        </xsl:variable>

        <xsl:choose>
            <xsl:when test="parent::system-index-block">
                <div class="nav-entry-link">
                    <xsl:copy-of select="$x" />
                </div>
            </xsl:when>
            <xsl:otherwise>
                <li>
                    <xsl:copy-of select="$x" />
                </li>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="system-folder[preceding-sibling::*[1][name() = 'system-folder']]" />
    <xsl:template match="system-folder">
        <div>
            <xsl:apply-templates select="." mode="following" />
        </div>
    </xsl:template>
    <xsl:template match="system-folder" mode="following">

        <h2>
            <xsl:value-of select="display-name" />
        </h2>
        <ul>
            <xsl:for-each select="system-page">
                <xsl:apply-templates select="current()" />
            </xsl:for-each>
        </ul>
        <xsl:apply-templates select="following-sibling::*[1][name() = 'system-folder']" mode="following" />
    </xsl:template>

</xsl:stylesheet>

这将生成以下输出:

<div class="nav-entry-link">
    <a href="/web/catalog/calendar">College Catalog</a>
</div>

<div>
    <h2>General Information</h2>
    <ul>
        <li>
            <a href="/web/catalog/general/history">History of the College</a>
        </li>
    </ul>
    <h2>General Information</h2>
    <ul>
        <li>
            <a href="/web/catalog/general/history">History of the College</a>
        </li>
    </ul>
</div>



<div class="nav-entry-link">
    <a class="current-nav-link" href="/web/catalog/index">College Catalog</a>
</div>
于 2013-06-12T19:35:33.757 回答