【问题标题】:How to convert XML hierarchy to table with XSLT 1.0?如何使用 XSLT 1.0 将 XML 层次结构转换为表?
【发布时间】:2018-05-15 05:11:47
【问题描述】:

我正在尝试将此 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>

期望的输出:

<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>

基本上它需要为每个层次结构级别创建 1 个&lt;tr&gt; 并包括其祖先。

我使用这个 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>

问题是 ID 的 &lt;td&gt; 并不总是在正确的位置,因为每个级别的祖先数量不同。如何获取每个节点的正确 Id 并将其放入正确的 Id 列?

【问题讨论】:

    标签: html xml html-table xslt-1.0 hierarchy


    【解决方案1】:

    我找到了另一种方法,我可以通过计算当前节点的祖先数量来添加所需的&lt;td&gt;

    <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>
    

    【讨论】:

      猜你喜欢
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      相关资源
      最近更新 更多