简体   繁体   English

如何使用XSLT 1.0将XML层次结构转换为表?

[英]How to convert XML hierarchy to table with XSLT 1.0?

I'm trying to convert this XML hierarchy to an HTML table 我正在尝试将此XML层次结构转换为HTML表

<TopTerm Id="1" Name="Company">
  <narrower Id="2" Name="Office">
    <narrower Id="2.1" Name="Desk">
      <narrower Id="2.1.1" Name="PC"/>
    </narrower>
    <narrower Id="2.2" Name="Cabinet">
      <narrower Id="2.2.1" Name="Folder"/>
      <narrower Id="2.2.2" Name="Files"/>
    </narrower>
  </narrower>
</TopTerm>

Desired output: 所需的输出:

<table>
    <tr>
        <th>Level 1</th>
        <th>Level 2</th>
        <th>Level 3</th>
        <th>Level 4</th>
        <th>Id</th>             
    </tr>
    <tr>
        <td>Company</td>
        <td>Office</td> 
        <td></td>
        <td></td>       
        <td>2</td>
    </tr>
    <tr>
        <td>Company</td>
        <td>Office</td> 
        <td>Desk</td>
        <td></td>       
        <td>2.1</td>
   </tr>                
</table>

Basically it needs to create 1 <tr> for each hierarchy level and includes its ancestors. 基本上,它需要为每个层次结构级别创建1 <tr> ,并包括其祖先。

I use this XSLT 我用这个XSLT

<xsl:template match="/">
      <table>
        <tr>
          <th>Level 1</th>
            <th>Level 2</th>
            <th>Level 3</th>
            <th>Level 4</th>
            <th>Id</th>             
        </tr>
      <xsl:apply-templates select="//TopTerm"/>
    </table>
</xsl:template> 
<xsl:template match="TopTerm">
        <tr>
          <td><xsl:value-of select="@Name"/></td>
          <td></td>
          <td></td>
          <td></td>
          <td><xsl:value-of select="@Id"/></td>     
        </tr>
        <xsl:apply-templates select="descendant-or-self::narrower"/>    
</xsl:template> 
<xsl:template match="narrower">
    <tr>
      <xsl:for-each select="ancestor-or-self::*">
         <td><xsl:value-of select="@Name"/></td>            
      </xsl:for-each>
      <td><xsl:value-of select="@Id"/></td> <!--Problem is here-->
   </tr>
 </xsl:template>

The problem is the <td> for Id is not always in the correct position because the number of ancestors in each level are different. 问题是<td>的ID并不总是处于正确的位置,因为每个级别中祖先的数量都不相同。 How do I get the correct Id for each node and put in it in the correct Id column? 如何获得每个节点的正确ID,并将其放入正确的ID列中?

I found another approach, I can add the required <td> by counting the number of ancestors of the current node. 我找到了另一种方法,可以通过计算当前节点的祖先数量来添加所需的<td>

<xsl:template match="narrower">
        <tr>
            <xsl:for-each select="ancestor-or-self::*">
                <td>
                    <xsl:value-of select="@Name"/>
                </td>            
            </xsl:for-each>
            <xsl:if test="count(ancestor-or-self::*) = 1">
                <td/>
                <td/>
                <td/>
                <td/>
            </xsl:if>
            <xsl:if test="count(ancestor-or-self::*) = 2">
                <td/>
                <td/>
            </xsl:if>
            <xsl:if test="count(ancestor-or-self::*) = 3">
                <td/>
            </xsl:if>
            <td>
                <xsl:value-of select="@Id"/>
            </td>           
        </tr>
    </xsl:template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM