所以你可以在下面看到我给定的 XML。我匹配了模板并且我已经在学生节点中 (xsl:template match="Class/Student"
):
<Class>
<Heading>This is a sentence.</Heading>
<Student>Alex</Student>
<Student>Emilia</Student>
<Student>John</Student>
</Class>
现在我需要得到所有学生的列表,我想要得到的应该是这样的:
<ul>
<li>Alex</li>
<li>Emilia</li>
<li>John</li>
</ul>
我认为我的思考方式有误,因为我的 XSLT 目前看起来是这样的:
<xsl:template match="Class/Student">
<ul>
<xsl:for-each select="../Student">
<li>
<xsl:apply-templates/>
</li>
</xsl:for-each>
</ul>
</xsl:template>
但我实际得到的是:
<ul>
<li>Alex</li>
<li>Emilia</li>
<li>John</li>
<ul>
<ul>
<li>Alex</li>
<li>Emilia</li>
<li>John</li>
<ul>
<ul>
<li>Alex</li>
<li>Emilia</li>
<li>John</li>
<ul>
我认为问题出在我使用的 for-each 上,但我不知道在这种情况下我还应该做什么。
最佳答案
你想要每个 Class
一个 ul
,而不是每个 Student
,所以改变
<xsl:template match="Class/Student">
到
<xsl:template match="Class">
然后改变
<xsl:for-each select="../Student">
到
<xsl:for-each select="Student">
为 Class
当前节点的每个 Student
子元素获取一个 li
。
关于html - 在 XSLT 中使用列表元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52407424/