xml - XSLT add table header base on hierarchy level -


here part of xml represents hierarchy of terms. topterm outermost parent , childterm child, can have many children possible.

<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 , procedures" /> </topterm> 

i'd have xslt 1.0 html output table, , result should this

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

something that. problem don't know depth of hierarchy add appropriate <th>level x</th> x can number base on depth. , term level should match table heading.

my problem don't know depth of hierarchy add appropriate <th>level x</th> x can number base on depth.

well, have grab 1 deepest , iterate on ancestors. try way:

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> 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -