嵌套 XSL 问题 apply-templates
Issue with nested XSL apply-templates
我正在尝试使用 xsl 将 xml 文件转换为 html。有一次我需要从特定标签(具有相同的嵌套结构)填充标题列表。
下面是我的 xml 文件的一部分。
<description>
<levelledPara>
<title>Heading [1]</title>
<para>Contains paragraph items</para>
<levelledPara>
<title>Heading [1.1]</title>
<para>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</para>
</levelledPara>
<levelledPara>
<title>Heading [1.2]</title>
<para>Fusce aliquet pede non pede</para>
</levelledPara>
<levelledPara>
<title>Heading [1.3]</title>
<para>Suspendisse dui purus, scelerisque at, vulputate vitae, pretium mattis, nunc.</para></levelledPara>
<levelledPara>
<title>Heading [1.4]</title>
<para>Aenean nec lorem. In porttitor. Donec laoreet nonummy augue.</para>
<levelledPara>
<title>Heading [1.4.1]</title>
<para>Pellentesque porttitor, velit lacinia egestas auctor</para>
</levelledPara>
<levelledPara>
<title>Heading [1.4.2]</title>
<para>Pellentesque porttitor, velit lacinia egestas auctor</para>
</levelledPara>
<levelledPara>
<title>Heading [1.4.3]</title>
<para>Fusce aliquet pede non pede. Suspendisse dapibus lorem pellentesque magna. </para>
<levelledPara>
<title>Heading [1.4.3.1]</title>
<para>Proin semper, ante vitae sollicitudin posuere, metus quam iaculis nibh.</para>
<levelledPara>
<title>Heading [1.4.3.1.1]</title>
<para>Ut tincidunt volutpat urna. Mauris eleifend nulla eget mauris.</para>
</levelledPara>
<levelledPara>
<title>Heading [1.4.3.1.2]</title>
<para>Ut tincidunt volutpat urna.</para>
</levelledPara>
</levelledPara>
<levelledPara>
<title>Heading [1.4.3.2]</title>
<para>Proin semper, ante vitae sollicitudin posuere.</para>
</levelledPara>
<levelledPara>
<title>Heading [1.4.3.3]</title>
<para>Proin semper, ante vitae sollicitudin posuere.</para>
</levelledPara>
<levelledPara>
<title>Heading [1.4.3.4]</title>
<para>Proin semper, ante vitae sollicitudin posuere.</para>
</levelledPara>
</levelledPara>
<levelledPara>
<title>Heading [1.4.4]</title>
<para>In hac habitasse platea dictumst.</para>
</levelledPara>
<levelledPara>
<title>Heading [1.4.5]</title>
<para>Quisque ornare placerat risus.</para>
</levelledPara>
</levelledPara>
<levelledPara>
<title>Heading [1.5]</title>
<para> Sed aliquam odio vitae tortor. Proin hendrerit tempus arcu.</para>
</levelledPara>
<levelledPara>
<title>Heading [1.6]</title>
<para>Vivamus a mi. Morbi neque.</para>
</levelledPara>
</levelledPara>
<levelledPara>
<title>Heading [2]</title>
<para>Proin semper, ante vitae sollicitudin posuere.</para>
</levelledPara>
<levelledPara>
<title>Heading [3]</title>
<para>Etiam at ligula et tellus ullamcorper ultrices.</para>
</levelledPara>
</description>
XSL 代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="html"></xsl:output>
<xsl:template match="/">
...
<body>
...
<nav><ul><xsl:apply-templates select="//levelledPara" mode="toc"/></ul></nav>
...
</body>
...
<xsl:template match="levelledPara" mode="toc">
<li>
<xsl:value-of select="title"/>
<xsl:if test="levelledPara">
<ul>
<xsl:apply-templates select="levelledPara" mode="toc"/>
</ul>
</xsl:if>
</li>
</xsl:template>
期望的输出:
<nav>
<ul>
<li>Heading [1]
<ul>
<li>Heading [1.1]</li>
<li>Heading [1.2]</li>
<li>Heading [1.3]</li>
<li>Heading [1.4]
<ul>
<li>Heading [1.4.1]</li>
<li>Heading [1.4.2]</li>
<li>Heading [1.4.3]
<ul>
<li>Heading [1.4.3.1]
<ul>
<li>Heading [1.4.3.1.1]</li>
<li>Heading [1.4.3.1.2]</li>
</ul>
</li>
<li>Heading [1.4.3.2]</li>
<li>Heading [1.4.3.3]</li>
<li>Heading [1.4.3.4]</li>
</ul>
</li>
<li>Heading [1.4.4]</li>
<li>Heading [1.4.5]</li>
</ul>
</li>
<li>Heading [1.5]</li>
<li>Heading [1.6]</li>
</ul>
</li>
<li>Heading [2]</li>
<li>Heading [3]</li>
</ul>
</nav>
实际输出:
<nav>
<ul>
<li>Heading [1]
<ul>
<li>Heading [1.1]</li>
<li>Heading [1.2]</li>
<li>Heading [1.3]</li>
<li>Heading [1.4]
<ul>
<li>Heading [1.4.1]</li>
<li>Heading [1.4.2]</li>
<li>Heading [1.4.3]
<ul>
<li>Heading [1.4.3.1]
<ul>
<li>Heading [1.4.3.1.1]</li>
<li>Heading [1.4.3.1.2]</li>
</ul>
</li>
<li>Heading [1.4.3.2]</li>
<li>Heading [1.4.3.3]</li>
<li>Heading [1.4.3.4]</li>
</ul>
</li>
<li>Heading [1.4.4]</li>
<li>Heading [1.4.5]</li>
</ul>
</li>
<li>Heading [1.5]</li>
<li>Heading [1.6]</li>
</ul>
</li>
<li>Heading [1.1]</li>
<li>Heading [1.2]</li>
<li>Heading [1.3]</li>
<li>Heading [1.4]
<ul>
<li>Heading [1.4.1]</li>
<li>Heading [1.4.2]</li>
<li>Heading [1.4.3]
<ul>
<li>Heading [1.4.3.1]
<ul>
<li>Heading [1.4.3.1.1]</li>
<li>Heading [1.4.3.1.2]</li>
</ul>
</li>
<li>Heading [1.4.3.2]</li>
<li>Heading [1.4.3.3]</li>
<li>Heading [1.4.3.4]</li>
</ul>
</li>
<li>Heading [1.4.4]</li>
<li>Heading [1.4.5]</li>
</ul>
</li>
<li>Heading [1.4.1]</li>
<li>Heading [1.4.2]</li>
<li>Heading [1.4.3]
<ul>
<li>Heading [1.4.3.1]
<ul>
<li>Heading [1.4.3.1.1]</li>
<li>Heading [1.4.3.1.2]</li>
</ul>
</li>
<li>Heading [1.4.3.2]</li>
<li>Heading [1.4.3.3]</li>
<li>Heading [1.4.3.4]</li>
</ul>
</li>
<li>Heading [1.4.3.1]
<ul>
<li>Heading [1.4.3.1.1]</li>
<li>Heading [1.4.3.1.2]</li>
</ul>
</li>
<li>Heading [1.4.3.1.1]</li>
<li>Heading [1.4.3.1.2]</li>
<li>Heading [1.4.3.2]</li>
<li>Heading [1.4.3.3]</li>
<li>Heading [1.4.3.4]</li>
<li>Heading [1.4.4]</li>
<li>Heading [1.4.5]</li>
<li>Heading [1.5]</li>
<li>Heading [1.6]</li>
<li>Heading [2]</li>
<li>Heading [3]</li>
</ul>
</nav>
似乎对整个嵌套内容一遍又一遍地应用了模板。我不知道这里可能是什么问题。
看来你只需要 select
<nav><ul><xsl:apply-templates select="/description/levelledPara" mode="toc"/></ul></nav>
对于第一个处理步骤,您的其他模板负责嵌套元素的递归处理。
我正在尝试使用 xsl 将 xml 文件转换为 html。有一次我需要从特定标签(具有相同的嵌套结构)填充标题列表。
下面是我的 xml 文件的一部分。
<description>
<levelledPara>
<title>Heading [1]</title>
<para>Contains paragraph items</para>
<levelledPara>
<title>Heading [1.1]</title>
<para>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</para>
</levelledPara>
<levelledPara>
<title>Heading [1.2]</title>
<para>Fusce aliquet pede non pede</para>
</levelledPara>
<levelledPara>
<title>Heading [1.3]</title>
<para>Suspendisse dui purus, scelerisque at, vulputate vitae, pretium mattis, nunc.</para></levelledPara>
<levelledPara>
<title>Heading [1.4]</title>
<para>Aenean nec lorem. In porttitor. Donec laoreet nonummy augue.</para>
<levelledPara>
<title>Heading [1.4.1]</title>
<para>Pellentesque porttitor, velit lacinia egestas auctor</para>
</levelledPara>
<levelledPara>
<title>Heading [1.4.2]</title>
<para>Pellentesque porttitor, velit lacinia egestas auctor</para>
</levelledPara>
<levelledPara>
<title>Heading [1.4.3]</title>
<para>Fusce aliquet pede non pede. Suspendisse dapibus lorem pellentesque magna. </para>
<levelledPara>
<title>Heading [1.4.3.1]</title>
<para>Proin semper, ante vitae sollicitudin posuere, metus quam iaculis nibh.</para>
<levelledPara>
<title>Heading [1.4.3.1.1]</title>
<para>Ut tincidunt volutpat urna. Mauris eleifend nulla eget mauris.</para>
</levelledPara>
<levelledPara>
<title>Heading [1.4.3.1.2]</title>
<para>Ut tincidunt volutpat urna.</para>
</levelledPara>
</levelledPara>
<levelledPara>
<title>Heading [1.4.3.2]</title>
<para>Proin semper, ante vitae sollicitudin posuere.</para>
</levelledPara>
<levelledPara>
<title>Heading [1.4.3.3]</title>
<para>Proin semper, ante vitae sollicitudin posuere.</para>
</levelledPara>
<levelledPara>
<title>Heading [1.4.3.4]</title>
<para>Proin semper, ante vitae sollicitudin posuere.</para>
</levelledPara>
</levelledPara>
<levelledPara>
<title>Heading [1.4.4]</title>
<para>In hac habitasse platea dictumst.</para>
</levelledPara>
<levelledPara>
<title>Heading [1.4.5]</title>
<para>Quisque ornare placerat risus.</para>
</levelledPara>
</levelledPara>
<levelledPara>
<title>Heading [1.5]</title>
<para> Sed aliquam odio vitae tortor. Proin hendrerit tempus arcu.</para>
</levelledPara>
<levelledPara>
<title>Heading [1.6]</title>
<para>Vivamus a mi. Morbi neque.</para>
</levelledPara>
</levelledPara>
<levelledPara>
<title>Heading [2]</title>
<para>Proin semper, ante vitae sollicitudin posuere.</para>
</levelledPara>
<levelledPara>
<title>Heading [3]</title>
<para>Etiam at ligula et tellus ullamcorper ultrices.</para>
</levelledPara>
</description>
XSL 代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="html"></xsl:output>
<xsl:template match="/">
...
<body>
...
<nav><ul><xsl:apply-templates select="//levelledPara" mode="toc"/></ul></nav>
...
</body>
...
<xsl:template match="levelledPara" mode="toc">
<li>
<xsl:value-of select="title"/>
<xsl:if test="levelledPara">
<ul>
<xsl:apply-templates select="levelledPara" mode="toc"/>
</ul>
</xsl:if>
</li>
</xsl:template>
期望的输出:
<nav>
<ul>
<li>Heading [1]
<ul>
<li>Heading [1.1]</li>
<li>Heading [1.2]</li>
<li>Heading [1.3]</li>
<li>Heading [1.4]
<ul>
<li>Heading [1.4.1]</li>
<li>Heading [1.4.2]</li>
<li>Heading [1.4.3]
<ul>
<li>Heading [1.4.3.1]
<ul>
<li>Heading [1.4.3.1.1]</li>
<li>Heading [1.4.3.1.2]</li>
</ul>
</li>
<li>Heading [1.4.3.2]</li>
<li>Heading [1.4.3.3]</li>
<li>Heading [1.4.3.4]</li>
</ul>
</li>
<li>Heading [1.4.4]</li>
<li>Heading [1.4.5]</li>
</ul>
</li>
<li>Heading [1.5]</li>
<li>Heading [1.6]</li>
</ul>
</li>
<li>Heading [2]</li>
<li>Heading [3]</li>
</ul>
</nav>
实际输出:
<nav>
<ul>
<li>Heading [1]
<ul>
<li>Heading [1.1]</li>
<li>Heading [1.2]</li>
<li>Heading [1.3]</li>
<li>Heading [1.4]
<ul>
<li>Heading [1.4.1]</li>
<li>Heading [1.4.2]</li>
<li>Heading [1.4.3]
<ul>
<li>Heading [1.4.3.1]
<ul>
<li>Heading [1.4.3.1.1]</li>
<li>Heading [1.4.3.1.2]</li>
</ul>
</li>
<li>Heading [1.4.3.2]</li>
<li>Heading [1.4.3.3]</li>
<li>Heading [1.4.3.4]</li>
</ul>
</li>
<li>Heading [1.4.4]</li>
<li>Heading [1.4.5]</li>
</ul>
</li>
<li>Heading [1.5]</li>
<li>Heading [1.6]</li>
</ul>
</li>
<li>Heading [1.1]</li>
<li>Heading [1.2]</li>
<li>Heading [1.3]</li>
<li>Heading [1.4]
<ul>
<li>Heading [1.4.1]</li>
<li>Heading [1.4.2]</li>
<li>Heading [1.4.3]
<ul>
<li>Heading [1.4.3.1]
<ul>
<li>Heading [1.4.3.1.1]</li>
<li>Heading [1.4.3.1.2]</li>
</ul>
</li>
<li>Heading [1.4.3.2]</li>
<li>Heading [1.4.3.3]</li>
<li>Heading [1.4.3.4]</li>
</ul>
</li>
<li>Heading [1.4.4]</li>
<li>Heading [1.4.5]</li>
</ul>
</li>
<li>Heading [1.4.1]</li>
<li>Heading [1.4.2]</li>
<li>Heading [1.4.3]
<ul>
<li>Heading [1.4.3.1]
<ul>
<li>Heading [1.4.3.1.1]</li>
<li>Heading [1.4.3.1.2]</li>
</ul>
</li>
<li>Heading [1.4.3.2]</li>
<li>Heading [1.4.3.3]</li>
<li>Heading [1.4.3.4]</li>
</ul>
</li>
<li>Heading [1.4.3.1]
<ul>
<li>Heading [1.4.3.1.1]</li>
<li>Heading [1.4.3.1.2]</li>
</ul>
</li>
<li>Heading [1.4.3.1.1]</li>
<li>Heading [1.4.3.1.2]</li>
<li>Heading [1.4.3.2]</li>
<li>Heading [1.4.3.3]</li>
<li>Heading [1.4.3.4]</li>
<li>Heading [1.4.4]</li>
<li>Heading [1.4.5]</li>
<li>Heading [1.5]</li>
<li>Heading [1.6]</li>
<li>Heading [2]</li>
<li>Heading [3]</li>
</ul>
</nav>
似乎对整个嵌套内容一遍又一遍地应用了模板。我不知道这里可能是什么问题。
看来你只需要 select
<nav><ul><xsl:apply-templates select="/description/levelledPara" mode="toc"/></ul></nav>
对于第一个处理步骤,您的其他模板负责嵌套元素的递归处理。