这里是我的XML的一部分,表示术语的层次结构。TopTerm是最外层的父元素,ChildTerm是子元素,它可以有尽可能多的子元素。
<TopTerm ID="1" Entity="Term" Name="ENVIRONMENTAL MANAGEMENT">
<ChildTerm Relationship="narrower" ID="8" Entity="Term" Name="Auditing">
<ChildTerm Relationship="narrower" ID="36" Entity="Term" Name="Environmental audit" />
<ChildTerm Relationship="narrower" ID="46" Entity="Term" Name="Type of audit []" />
</ChildTerm>
<ChildTerm Relationship="narrower" ID="11" Entity="Term" Name="Incidents">
<ChildTerm Relationship="narrower" ID="71" Entity="Term" Name="Bruce Beresford" />
<ChildTerm Relationship="narrower" ID="35" Entity="Term" Name="Case name" />
<ChildTerm Relationship="narrower" ID="83" Entity="Term" Name="Jack Lemmon" />
<ChildTerm Relationship="narrower" ID="87" Entity="Term" Name="Mary Pcikford" />
</ChildTerm>
<ChildTerm Relationship="narrower" ID="16" Entity="Term" Name="Monitoring" />
<ChildTerm Relationship="narrower" ID="18" Entity="Term" Name="Policies and procedures" />
</TopTerm>
我想要一个带有表的XSLT 1.0 HTML输出,结果应该如下所示
<table>
<tr>
<th>Level 1</th>
<th>Level 2</th>
<th>Level 3</th>
<th>Level 4</th>
</tr>
<tr>
<td>ENVIRONMENTAL MANAGEMENT</td>
<td>Auditing</td>
<td>Environmental audit</td>
</tr>
</table>
差不多。我的问题是我不知道这个层次结构的深度来添加适当的<th>Level x</th>
,其中x可以是基于深度的任何数字。术语级别应该与表标题匹配。
我的问题是我不知道这个层次结构的深度适当的
<th>Level x</th>
,其中x可以是基于的任意数深度。
好吧,你只需要抓住最深的那个,然后迭代它的祖先。试试这样:
XSLT 1.0<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="TopTerm">
<table border="1">
<thead>
<xsl:apply-templates select="descendant::ChildTerm[not(*)]" mode="header">
<xsl:sort select="count(ancestor-or-self::*)" data-type="number" order="descending"/>
</xsl:apply-templates>
</thead>
<tbody>
<xsl:apply-templates select="descendant::ChildTerm[not(*)]"/>
</tbody>
</table>
</xsl:template>
<xsl:template match="ChildTerm" mode="header">
<xsl:if test="position()=1">
<tr>
<xsl:for-each select="ancestor-or-self::*">
<th><xsl:value-of select="concat('Level ', position())"/></th>
</xsl:for-each>
</tr>
</xsl:if>
</xsl:template>
<xsl:template match="ChildTerm">
<tr>
<xsl:for-each select="ancestor-or-self::*">
<td><xsl:value-of select="@Name"/></td>
</xsl:for-each>
</tr>
</xsl:template>
</xsl:stylesheet>