找到章节的最大深度

2024-01-08

每个人 。在这种情况下,我想计算章节的最大深度。例如,一本没有章节的书的高度为 0 。一本书只有章节没有章节,高度应该为1。以下是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>

顺便说一下,我用的是撒克逊语。我想尝试仅使用匹配模板。在这种情况下,输出是文本,结果是

 3

这是我用于计算每个音符深度的 XSL?是吗?那么如何通过调用名为 max 的模板来输出电流的最大值呢?

<xsl:transform version="2.0"
           xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/">
    <xsl:apply-templates select="book"/>
    <xsl:text>&#10;</xsl:text>
</xsl:template>

<xsl:template match="book">
    <xsl:apply-templates select="chapter">
        <xsl:with-param name ="depth" select ="1"/>
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="chapter|section">
    <xsl:param name="depth"  as="item()*"/>
    <xsl:variable name ="current" select ="$depth"/>
    <xsl:sequence select ="$depth"/>
    <xsl:if test ="not(empty(section))">
        <xsl:apply-templates select="section">
            <xsl:with-param name="depth" select="$depth+1"/>
        </xsl:apply-templates>

    </xsl:if>
 </xsl:template>
</xsl:transform >

XSLT 1.0 解决方案:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
     <xsl:apply-templates select="//*[self::chapter or self::section]">
       <xsl:sort data-type="number" order="descending" select=
        "count(ancestor::*[self::chapter or self::section])
        "/>
     </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="chapter|section">
    <xsl:if test="position()=1">
     <xsl:value-of select=
     "count(ancestor::*[self::chapter or self::section]) +1
     "/>
    </xsl:if>
 </xsl:template>
</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="section1.3">
            <section title="section1.3.1"/></section>
    </chapter>
    <chapter title="chapter2"/>
</book>

产生了想要的正确答案:

3
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

找到章节的最大深度 的相关文章

  • 如何处理XSL中的非法HTML字符

    我有一个存储数据的 XML 文件 我正在使用 XSL 从该 XML 文件生成 HTML 文件 当我尝试这样做时 我收到错误Illegal HTML character decimal 150 我不允许更改 XML 文件 我必须将一个和许多其
  • 设置 HTML 表格的最大显示行数

    具有动态生成的 HTML 表 行数未知 的 JSP 页面 在后端有属性 设置最大行数 例如 最大行数 15 如何将 HTML 表格的行数限制为max rows价值 表的其他部分应该可以通过垂直滚动访问 这意味着用户可以看到 15 行 如果向
  • Scipy max_filter 太疯狂了

    我对 scipy 的 Maximum filter 函数有一个小问题 但没有得到解决方案 我有三个不同的 numpy 数组 a np array 152 nan 30 nan nan nan nan nan nan nan nan nan
  • 提供 xdm_node 时,saxonc 的 transform_to_file() 不生成输出文件

    这似乎是由 alinaOS 回答的SaxonC 11 1 transform to file 不生成输出文件 https stackoverflow com questions 71021778 saxonc 11 1 transform
  • 如何在 C# 中启用 XSLT 脚本..?

    找到答案后我修改了问题的标题 P 我正在通过 C 程序加载 XML 文件和 XSL 文件并触发 XSL 转换 以下是其代码 static void Main string args Create the XslCompiledTransfo
  • 具有最大高度和滚动的动态内容的对话框+页脚CSS

    我有一个dialog with 位置 绝对 and a 最大高度放 这最大高度财产是set从外面by a javascript框架 jQuery UI 对话框 所以我无法控制它 里面有 2 个 div 其中一个充满了动态内容 and a 静
  • 带有嵌套表的 XSLT 中的数据对齐问题

    我正在通过 FoundationPHP 使用 XSLT 和嵌套表生成 Word 文档 我想要的结果是 Table 1 Table 2 Image 1 Image 2 Label 1 Label 2 Data 1 Data 2 Table 3
  • 随机推荐