对表格进行编号

发布于 2024-11-05 21:20:09 字数 1228 浏览 4 评论 0原文

输出应该是html,使用xsl输出目录,下面是xml

<book title="D">
<author>
  <name>abc</name>
</author>

<chapter title="chapter1">
  <section title="section1.1"/>
  <section title="section1.2">
    <section title="section1.2.1"/>
<section title="section1.2.2"/>
  </section>
  <section title="section1.3">
<section title="section1.3.1"/>
  </section>
</chapter>

<chapter title="chapter2"/>

</book>

结果是 html,如下所示:

<body>
  <h2>D</h2>
  <p>
     by abc
  </p>
  <h3>Table of contents</h3>
  <ul>
     <li>[1]chapter1
     <ul>
        <li>[1.1]section1.1</li>
        <li>[1.2]section1.2
        <ul>
           <li>[1.2.1]section1.2.1</li>
           <li>[1.2.2]section1.2.2</li>
        </ul>
        </li>
        <li>[1.3]section1.3
        <ul>
           <li>[1.3.1]section1.3.1</li>
        </ul>
        </li>
     </ul>
     </li>
     <li>[2]chapter2</li>
  </ul>
  </body>

The output should be html, use xsl to output the table of contents, following is the xml

<book title="D">
<author>
  <name>abc</name>
</author>

<chapter title="chapter1">
  <section title="section1.1"/>
  <section title="section1.2">
    <section title="section1.2.1"/>
<section title="section1.2.2"/>
  </section>
  <section title="section1.3">
<section title="section1.3.1"/>
  </section>
</chapter>

<chapter title="chapter2"/>

</book>

the result is html, as this:

<body>
  <h2>D</h2>
  <p>
     by abc
  </p>
  <h3>Table of contents</h3>
  <ul>
     <li>[1]chapter1
     <ul>
        <li>[1.1]section1.1</li>
        <li>[1.2]section1.2
        <ul>
           <li>[1.2.1]section1.2.1</li>
           <li>[1.2.2]section1.2.2</li>
        </ul>
        </li>
        <li>[1.3]section1.3
        <ul>
           <li>[1.3.1]section1.3.1</li>
        </ul>
        </li>
     </ul>
     </li>
     <li>[2]chapter2</li>
  </ul>
  </body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悸初 2024-11-12 21:20:09

更新:OP 在发布此解决方案后更改了他提供的 XML。下面的解决方案产生所需的正确编号。我更新它并不是为了赶上OP的更新,因为我不能把所有时间都花在等待下一次更新发生时。

使用 的强大之处在于,无论对标题的字符串值进行何种更新,生成的编号仍然是正确的。 :)

此转换

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="book">
        <body>
            <xsl:apply-templates select="node()|@*"/>
        </body>
 </xsl:template>

 <xsl:template match="book/@title">
        <h2>
            <xsl:value-of select="."/>
        </h2>
 </xsl:template>

 <xsl:template match="author">
        <p>by 
            <xsl:value-of select="name"/>
        </p>
        <h3>Table of Contents</h3>
        <ul>
            <xsl:apply-templates mode="TC"
                 select="following-sibling::*"/>
        </ul>
 </xsl:template>

 <xsl:template mode="TC"
      match="chapter[section]|section[section]">
        <li>
            [<xsl:number level="multiple"
             count="chapter|section"/>] <xsl:text/>
            <xsl:value-of select="@title"/>
            <ul>
                <xsl:apply-templates mode="TC"/>
            </ul>
        </li>
 </xsl:template>

 <xsl:template mode="TC" match=
    "chapter[not(section)]|section[not(section)]">
        <li>
            [<xsl:number level="multiple"
             count="chapter|section"/>] <xsl:text/>
            <xsl:value-of select="@title"/>
        </li>
 </xsl:template>

 <xsl:template match="chapter|section"/>
</xsl:stylesheet>

应用于提供的 XML 文档时

<book title="D">
    <author>
        <name>abc</name>
    </author>
    <chapter title="chapter1">
        <section title="section1.1"/>
        <section title="section1.2">
            <section title="section1.2.1"/>
            <section title="section1.2.2"/></section>
        <section title="section3">
            <section title="section3.1"/></section>
    </chapter>
    <chapter title="chapter2"/>
</book>

产生所需的正确结果

<body>
   <h2>D</h2>
   <p>by 
            abc</p>
   <h3>Table of Contents</h3>
   <ul>
      <li>
            [1] chapter1<ul>
            <li>
            [1.1] section1.1</li>
            <li>
            [1.2] section1.2<ul>
                  <li>
            [1.2.1] section1.2.1</li>
                  <li>
            [1.2.2] section1.2.2</li>
               </ul>
            </li>
            <li>
            [1.3] section3<ul>
                  <li>
            [1.3.1] section3.1</li>
               </ul>
            </li>
         </ul>
      </li>
      <li>
            [2] chapter2</li>
   </ul>
</body>

并由浏览器为

d


abc

目录

  • [1] 第 1 章

    • [1.1]第1.1节
    • [1.2] 第1.2节

      • [1.2.1]第1.2.1节
      • [1.2.2]第1.2.2节
    • [1.3]第3节

      • [1.3.1]第3.1节
  • [2] 第二章

说明

使用,其中 level="multiple" 同时计算 chaptersection

UPDATE: The OP changed his provided XML after this solution was posted. The solution below produces the wanted, correct numbering. I am not updating it in order to catch-up with the OP's update, because I cannot spend all my time waiting when every next update will happen.

The power of using <xsl:number> is that no matter what update is done to the string values of the titles, the produced numbering continues to be correct. :)

This transformation:

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="book">
        <body>
            <xsl:apply-templates select="node()|@*"/>
        </body>
 </xsl:template>

 <xsl:template match="book/@title">
        <h2>
            <xsl:value-of select="."/>
        </h2>
 </xsl:template>

 <xsl:template match="author">
        <p>by 
            <xsl:value-of select="name"/>
        </p>
        <h3>Table of Contents</h3>
        <ul>
            <xsl:apply-templates mode="TC"
                 select="following-sibling::*"/>
        </ul>
 </xsl:template>

 <xsl:template mode="TC"
      match="chapter[section]|section[section]">
        <li>
            [<xsl:number level="multiple"
             count="chapter|section"/>] <xsl:text/>
            <xsl:value-of select="@title"/>
            <ul>
                <xsl:apply-templates mode="TC"/>
            </ul>
        </li>
 </xsl:template>

 <xsl:template mode="TC" match=
    "chapter[not(section)]|section[not(section)]">
        <li>
            [<xsl:number level="multiple"
             count="chapter|section"/>] <xsl:text/>
            <xsl:value-of select="@title"/>
        </li>
 </xsl:template>

 <xsl:template match="chapter|section"/>
</xsl:stylesheet>

when applied on the provided XML document:

<book title="D">
    <author>
        <name>abc</name>
    </author>
    <chapter title="chapter1">
        <section title="section1.1"/>
        <section title="section1.2">
            <section title="section1.2.1"/>
            <section title="section1.2.2"/></section>
        <section title="section3">
            <section title="section3.1"/></section>
    </chapter>
    <chapter title="chapter2"/>
</book>

produces the wanted, correct result:

<body>
   <h2>D</h2>
   <p>by 
            abc</p>
   <h3>Table of Contents</h3>
   <ul>
      <li>
            [1] chapter1<ul>
            <li>
            [1.1] section1.1</li>
            <li>
            [1.2] section1.2<ul>
                  <li>
            [1.2.1] section1.2.1</li>
                  <li>
            [1.2.2] section1.2.2</li>
               </ul>
            </li>
            <li>
            [1.3] section3<ul>
                  <li>
            [1.3.1] section3.1</li>
               </ul>
            </li>
         </ul>
      </li>
      <li>
            [2] chapter2</li>
   </ul>
</body>

and it is displayed by the browser as:

D

by
abc

Table of Contents

  • [1] chapter1

    • [1.1] section1.1
    • [1.2] section1.2

      • [1.2.1] section1.2.1
      • [1.2.2] section1.2.2
    • [1.3] section3

      • [1.3.1] section3.1
  • [2] chapter2

Explanation:

Use of <xsl:number> with level="multiple" counting both chapter and section.

栀梦 2024-11-12 21:20:09

此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="book">
        <body>
            <h2><xsl:apply-templates select="@title" /></h2>
            <p>by <xsl:apply-templates select="author/name" /></p>
            <h3>Table of contents</h3>
            <ul>
                <xsl:apply-templates select="chapter" />
            </ul>
        </body>
    </xsl:template>
    <xsl:template match="chapter|section">
        <li><xsl:call-template name="extract-num" /></li>
    </xsl:template>
    <xsl:template match="chapter[child::*]|section[child::*]">
        <li>
            <xsl:call-template name="extract-num" />
            <ul>
                <xsl:apply-templates />
            </ul>
        </li>
    </xsl:template>
    <xsl:template name="extract-num">
        <xsl:value-of select="concat('[', substring(@title, 8), ']', @title)" />
    </xsl:template>
</xsl:stylesheet>

在此输入上:

<book title="D">
    <author>
        <name>abc</name>
    </author>
    <chapter title="chapter1">
        <section title="section1.1" />
        <section title="section1.2">
            <section title="section1.2.1" />
            <section title="section1.2.2" />
        </section>
        <section title="section3">
            <section title="section3.1" />
        </section>
    </chapter>
    <chapter title="chapter2" />
</book>

生成:

<body>
    <h2>D</h2>
    <p>by abc</p>
    <h3>Table of contents</h3>
    <ul>
        <li>[1]chapter1
            <ul>
                <li>[1.1]section1.1</li>
                <li>[1.2]section1.2
                    <ul>
                        <li>[1.2.1]section1.2.1</li>
                        <li>[1.2.2]section1.2.2</li>
                    </ul>
                </li>
                <li>[3]section3
                    <ul>
                        <li>[3.1]section3.1</li>
                    </ul>
                </li>
            </ul>
        </li>
        <li>[2]chapter2</li>
    </ul>
</body>

注意:调整了格式的空白。如果您实际上需要在请求的输出中提供的确切空格,那么您需要进行相应的修改。

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="book">
        <body>
            <h2><xsl:apply-templates select="@title" /></h2>
            <p>by <xsl:apply-templates select="author/name" /></p>
            <h3>Table of contents</h3>
            <ul>
                <xsl:apply-templates select="chapter" />
            </ul>
        </body>
    </xsl:template>
    <xsl:template match="chapter|section">
        <li><xsl:call-template name="extract-num" /></li>
    </xsl:template>
    <xsl:template match="chapter[child::*]|section[child::*]">
        <li>
            <xsl:call-template name="extract-num" />
            <ul>
                <xsl:apply-templates />
            </ul>
        </li>
    </xsl:template>
    <xsl:template name="extract-num">
        <xsl:value-of select="concat('[', substring(@title, 8), ']', @title)" />
    </xsl:template>
</xsl:stylesheet>

On this input:

<book title="D">
    <author>
        <name>abc</name>
    </author>
    <chapter title="chapter1">
        <section title="section1.1" />
        <section title="section1.2">
            <section title="section1.2.1" />
            <section title="section1.2.2" />
        </section>
        <section title="section3">
            <section title="section3.1" />
        </section>
    </chapter>
    <chapter title="chapter2" />
</book>

Produces:

<body>
    <h2>D</h2>
    <p>by abc</p>
    <h3>Table of contents</h3>
    <ul>
        <li>[1]chapter1
            <ul>
                <li>[1.1]section1.1</li>
                <li>[1.2]section1.2
                    <ul>
                        <li>[1.2.1]section1.2.1</li>
                        <li>[1.2.2]section1.2.2</li>
                    </ul>
                </li>
                <li>[3]section3
                    <ul>
                        <li>[3.1]section3.1</li>
                    </ul>
                </li>
            </ul>
        </li>
        <li>[2]chapter2</li>
    </ul>
</body>

Note: Whitespace adjusted for formatting. If you actually need the exact whitespace you provide in your requested output, then you'll need to modify accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文