如何使用XSLT1.0自动生成XHTML标题级别

如何使用XSLT1.0自动生成XHTML标题级别,xslt,xslt-1.0,Xslt,Xslt 1.0,例如,我将如何扭转这种局面: <chapter name="Chapter 1"> <chapter name="Chapter 1.1"> <chapter name="Chapter 1.1.1"> <chapter name="Chapter 1.1.1.1"/> </chapter> <chapter name="Chapter 1.1.2"/> </chapter&

例如,我将如何扭转这种局面:

<chapter name="Chapter 1">
  <chapter name="Chapter 1.1">
    <chapter name="Chapter 1.1.1">
      <chapter name="Chapter 1.1.1.1"/>
    </chapter>
    <chapter name="Chapter 1.1.2"/>
  </chapter>
  <chapter name="Chapter 1.2"/>
</chapter>

为此:

<h1>Chapter 1</h1>
<h2>Chapter 1.1</h2>
<h3>Chapter 1.1.1</h3>
<h4>Chapter 1.1.1.1</h4>
<h3>Chapter 1.1.2</h3>
<h2>Chapter 1.2</h2>
第1章
第1.1章
第1.1.1章
第1.1.1.1章
第1.1.2章
第1.2章

谢谢

有几种可能的方法可以做到这一点。它取决于标题级别的标志(属性
@name
中的数字?章节
元素的深度?)

我想这是
章节
元素的深度。因此,对于输入xml

<?xml version="1.0" encoding="UTF-8"?>
<chapter name="Chapter 1">
    <chapter name="Chapter 1.1">
        <chapter name="Chapter 1.1.1">
            <chapter name="Chapter 1.1.1.1"/>
        </chapter>
        <chapter name="Chapter 1.1.2"/>
    </chapter>
    <chapter name="Chapter 1.2"/>
</chapter>
它产生以下输出

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>xxx</title>
    </head>
    <body>
        <h1>Chapter 1</h1>
        <h2>Chapter 1.1</h2>
        <h3>Chapter 1.1.1</h3>
        <h4>Chapter 1.1.1.1</h4>
        <h3>Chapter 1.1.2</h3>
        <h2>Chapter 1.2</h2>
    </body>
</html>

xxx
第一章
第1.1章
第1.1.1章
第1.1.1.1章
第1.1.2章
第1.2章
编辑:另一个想法是像这样计算“父”章节元素的数量

<?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" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>xxx</title>
            </head>
            <body>
                <xsl:apply-templates select="//chapter" />
            </body>
        </html>
    </xsl:template>

    <xsl:template match="chapter">
        <xsl:variable name="level" select="count(ancestor-or-self::chapter)" />
        <xsl:element name="h{$level}">
            <xsl:value-of select="@name" />
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

xxx

有几种可能的方法。它取决于标题级别的标志(属性
@name
中的数字?章节
元素的深度?)

我想这是
章节
元素的深度。因此,对于输入xml

<?xml version="1.0" encoding="UTF-8"?>
<chapter name="Chapter 1">
    <chapter name="Chapter 1.1">
        <chapter name="Chapter 1.1.1">
            <chapter name="Chapter 1.1.1.1"/>
        </chapter>
        <chapter name="Chapter 1.1.2"/>
    </chapter>
    <chapter name="Chapter 1.2"/>
</chapter>
它产生以下输出

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>xxx</title>
    </head>
    <body>
        <h1>Chapter 1</h1>
        <h2>Chapter 1.1</h2>
        <h3>Chapter 1.1.1</h3>
        <h4>Chapter 1.1.1.1</h4>
        <h3>Chapter 1.1.2</h3>
        <h2>Chapter 1.2</h2>
    </body>
</html>

xxx
第一章
第1.1章
第1.1.1章
第1.1.1.1章
第1.1.2章
第1.2章
编辑:另一个想法是像这样计算“父”章节元素的数量

<?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" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>xxx</title>
            </head>
            <body>
                <xsl:apply-templates select="//chapter" />
            </body>
        </html>
    </xsl:template>

    <xsl:template match="chapter">
        <xsl:variable name="level" select="count(ancestor-or-self::chapter)" />
        <xsl:element name="h{$level}">
            <xsl:value-of select="@name" />
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

xxx

到目前为止,您做了哪些研究和尝试了哪些内容?我对XSLT非常陌生。我已经尝试了一些事情,但总是以我认为是“硬编码”的解决方案结束。我想我会以纯粹的形式提出这个问题,而不会用我的(可能是错误的)想法来污染它。你做了什么研究,到目前为止你尝试了什么?我对XSLT非常陌生。我已经尝试了一些事情,但总是以我认为是“硬编码”的解决方案结束。我想我会以纯粹的形式提出这个问题,而不会用我的(可能是错误的)想法来污染它。计算父母的数量是一个优雅的解决方案。谢谢。数一数父母的人数是一个很好的解决办法。谢谢