我想在使用 XSL 生成的 HTML 文档中生成目录标识符。
从以下格式:
<?xml version="1.0" encoding="utf-8"?>
<section>
<header>Heading 1</header>
<section>
<section>
<section>
<header>Heading 4</header>
</section>
</section>
</section>
<section>
<header>Heading 2</header>
<section>
<section>
<section>
<header>Heading 5</header>
</section>
</section>
</section>
<section>
<header>Heading 3</header>
</section>
</section>
<section>
<header>Heading 2</header>
</section>
</section>
我已经能够生成:
<?xml version="1.0" encoding="utf-8"?>
<h1>Heading 1</h1>
<h4>Heading 4</h4>
<h2>Heading 2</h2>
<h5>Heading 5</h5>
<h6>Heading 6</h6>
<h2>Heading 2</h2>
谢谢:
<?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" indent="yes" encoding="UTF-8" />
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="header">
<xsl:variable name="level" select="count(ancestor-or-self::section)"/>
<xsl:element name="h{$level}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
但我需要添加一个额外的 TOC 标识符才能拥有:
<?xml version="1.0" encoding="utf-8"?>
<a name="toc1"></a><h1>Heading 1</h1>
<a name="toc1_0_0_1"></a><h4>Heading 4</h4>
<a name="toc1_1"><h2>Heading 2</h2>
<a name="toc1_1_0_0_1"><h5>Heading 5</h5>
<a name="toc1_1_0_0_1_1"><h6>Heading 6</h6>
<a name="toc1_2"><h2>Heading 2</h2>
纯 XSLT 处理可以做到这一点吗?