如何使用XSLT用`div`元素包装`apply-template`元素?

时间:2013-06-12 17:38:41

标签: xslt

我正在尝试用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>

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

添加一个忽略系统文件夹的模板,该系统文件夹具有直接在先兄弟系统文件夹。

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

生成dif

的模板
<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>