它应该解析XML文件中的内容表,并输出带有有序列表的HTML。我尝试了如下所示,但它显示重复的条目(第一个标题两次)和一个空的li标记,我不知道如何删除它们。
我的XML:
<?xml version="1.0" encoding="UTF-8" ?>
<document>
<section>
<title>Section One</title>
</section>
<section>
<title>Section Two</title>
<section>
<title>Section Two.One</title>
</section>
<section>
<title>Section Two.Two</title>
<section>
<title>Section Two.Two.One</title>
</section>
</section>
</section>
<section>
<title>Section Three</title>
</section>
</document>
My XSLT:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="document">
<html>
<head>
</head>
<body>
<ol>
<xsl:apply-templates/>
</ol>
</body>
</html>
</xsl:template>
<xsl:template match="section">
<li>
<xsl:value-of select="title" />
<xsl:if test="section">
<ol>
<li>
<xsl:apply-templates select="title" />
<xsl:if test="section">
<li>
<xsl:apply-templates select="section" />
</li>
</xsl:if>
</li>
</ol>
</xsl:if>
</li>
</xsl:template>
</xsl:stylesheet>产出:

会不会是分组问题?我有顺序元素,我想在没有空li标记的情况下嵌套它。任何想法都会受到极大的赞赏。
发布于 2014-12-17 14:21:57
我相信你让这件事变得更复杂了。这样试一试:
<xsl:template match="/document">
<html>
<head>
</head>
<body>
<ol>
<xsl:apply-templates select="section" />
</ol>
</body>
</html>
</xsl:template>
<xsl:template match="section">
<li>
<xsl:value-of select="title" />
<xsl:if test="section">
<ol>
<xsl:apply-templates select="section" />
</ol>
</xsl:if>
</li>
</xsl:template>https://stackoverflow.com/questions/27527472
复制相似问题