2

我正在使用 XSLT 将用作文档集 TOC 的 XML 文件转换为 Excel 可以分隔的格式。例如,在 Excel 中打开时,分隔版本可能类似于:

+-----------+---------------+------+-----------+---------------------+
|URL        |Title          |Depth |Level 1    |Level 2              |
+-----------+---------------+------+-----------+---------------------+
|dogs.html  |Dogs are cool  |2     |Animals    |Domesticated Animals |
+-----------+---------------+------+-----------+---------------------+

这部分我已经完成了,我将在下面展示示例代码。

我的问题是我想在顶部插入列标题,每个级别都有一个列标题。目前,我手动执行此操作,但我想在具有不同级别数的多个文档集上使用转换。

所以我的问题是我如何找出最高级别的数量(即哪个节点有最多的祖先)并创建那么多列标题?

这是示例 XML:

<contents Url="toc_animals.html" Title="Animals">
<contents Url="toc_apes.html" Title="Apes">
    <contents Url="chimps.html" Title="Some Stuff About Chimps" />
</contents>
<contents Url="toc_cats" Title="Cats">
    <contents Url="hairless_cats.html" Title="OMG Where Did the Hair Go?"/>
    <contents Url="wild_cats.html" Title="These Things Frighten Me"/>
</contents>
<contents Url="toc_dogs.html" Title="Dogs">
    <contents Url="toc_snorty_dogs.html" Title="Snorty Dogs">
        <contents Url="boston_terriers.html" Title="Boston Terriers" />
        <contents Url="french_bull_dogs.html" Title="Frenchies" />
    </contents>
</contents>
</contents>

这是 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="text" indent="no"/>

<!-- This variable sets the delimiter symbol that Excel will use to seperate the cells -->
<xsl:variable name="delimiter">@</xsl:variable>

<xsl:template match="contents">

    <!-- Prints the URL -->
    <xsl:value-of select="@Url"/>
    <xsl:copy-of select="$delimiter" />

    <!-- Prints the title -->
    <xsl:apply-templates select="@Title"/>
    <xsl:copy-of select="$delimiter" />

    <!-- Prints the depth--i.e., the number of categories, sub-categories, etc. -->
    <xsl:value-of select="count(ancestor::*[@Title])"/>
    <xsl:copy-of select="$delimiter" />

    <!-- Sets up the categories --> 
    <xsl:for-each select="ancestor::*[@Title]">
        <xsl:apply-templates select="@Title"/>
        <xsl:if test="position() != last()">
            <xsl:copy-of select="$delimiter" />
        </xsl:if>
    </xsl:for-each>
    <xsl:text>&#xA;</xsl:text>
</xsl:template>

<xsl:template match="/">

    <!-- Creates the column headings manually -->
    <xsl:text>URL</xsl:text>
    <xsl:copy-of select="$delimiter" />
    <xsl:text>Title</xsl:text>
    <xsl:copy-of select="$delimiter" />
    <xsl:text>Depth</xsl:text>
    <xsl:copy-of select="$delimiter" />
    <xsl:text>Level 1</xsl:text>
    <xsl:copy-of select="$delimiter" />
    <xsl:text>Level 2</xsl:text>
    <xsl:copy-of select="$delimiter" />
    <xsl:text>&#xA;</xsl:text>

    <xsl:apply-templates select="//contents"/>
</xsl:template>

</xsl:stylesheet>
4

1 回答 1

0

某些东西的最大值(在这种情况下是祖先计数)很容易选择。按该质量降序排序,将第一个值存储到变量中。

<xsl:variable name="levels">
  <xsl:for-each select="//contents[not(*)]">
    <xsl:sort select="count(ancestor::*)" data-type="number" order="descending" />
    <xsl:if test="position() = 1">
      <xsl:value-of select="count(ancestor::*)" />
    </xsl:if>
  </xsl:for-each>
</xsl:variable>

但是您对数字并不真正感兴趣,您对标题感兴趣:

<xsl:for-each select="//contents[not(*)]">
  <xsl:sort select="count(ancestor::*)" data-type="number" order="descending" />
  <xsl:if test="position() = 1">
    <xsl:for-each select="ancestor::*">
      <!-- one header for each ancestor of the most deeply nested node -->
      <xsl:value-of select="concat('Level ', position())" />
      <xsl:choose>
        <xsl:when test="position() = last()">
          <xsl:text>&#xA;</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$delimiter" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:if>
</xsl:for-each>
于 2013-05-14T20:24:06.587 回答