Построение дерева при помощи XSLT

kost

Новичок
Построение дерева при помощи XSLT

Добрый день. Помогите, пожалуйста. Прочитал статейку про построение дерева при помощи XSLT. Захотелось переделать под общие нужды.

Тоесть, хотелось, чтоб шаблон выводит дерево входных данных.

Сделал так:
Код:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" encoding="utf-8" indent="no"/>

  <xsl:template match="*">
    <h1 class="tree">
    	<b class="tree"><xsl:value-of select="name()"/></b>
      <xsl:text> :: </xsl:text>
      <xsl:value-of select="@name"/>
    </h1>
    <xsl:apply-templates mode="line"/>
  </xsl:template>

  <xsl:template match="*" mode="line">
    <div class="tree">
      <xsl:call-template name="graft"/>
      <xsl:apply-templates select="." mode="item"/>
    </div>
    
    <xsl:apply-templates mode="line"/>    
  </xsl:template>
  
  <xsl:template match="*" mode="item">
    <b class="tree"><xsl:value-of select="name()"/></b>
    <xsl:text> data: </xsl:text>
    <xsl:value-of select="."/>
    <xsl:for-each select="attribute::*">
      <xsl:text> | </xsl:text>
      <xsl:value-of select="name()"/>
      <xsl:text> -> </xsl:text>
      <xsl:value-of select="."/>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="graft">
    <xsl:apply-templates select="ancestor::*" mode="tree"/>
    <xsl:choose>
      <xsl:when test="following-sibling::*">
        <img src="./images/tree_tee.gif" alt="" class="tree"/>
      </xsl:when>
      <xsl:otherwise>
        <img src="./images/tree_corner.gif" alt="" class="tree"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="*" mode="tree">
    <xsl:choose>
      <xsl:when test="following-sibling::*">
        <img src="./images/tree_bar.gif" alt="" class="tree"/>
      </xsl:when>
      <xsl:otherwise>
        <img src="./images/tree_spacer.gif" alt="" class="tree"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
Все работает, только вот почему-то повторно выдаются значения ячеек. Если дерево будет без внутренностей ячеек, а только с параметрами - все ок. Если, к примеру,
Код:
<something name="SomeName">
 <child>childinfo</child>
</something>
то после строки с child'ом выдается повторно childinfo. Помогите найти косяк или же пошлите на человеческий бесплатный xsl-debugger. Буду очень благодарен.
 

kost

Новичок
an_kalinovski
Придумайте XML сами. Главное чтоб был один parent-элемент.

------------

Проблема решена!!!

Вывешу, пожалуй, полное решение, т.к. штука полезная. Повторяю. Она выводит ЛЮБОЕ дерево элементов xml'а и показывает все аттрибуты и значения ячеек, поэтому входные данные не важны.

Решение:

Код:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" encoding="utf-8" indent="no"/>

  <xsl:template match="*">
    <h1 class="tree">
    	<b class="tree"><xsl:value-of select="name()"/></b>
      <xsl:text> :: </xsl:text>
      <xsl:value-of select="@name"/>
    </h1>
    <xsl:apply-templates mode="line"/>
  </xsl:template>

  <xsl:template match="*" mode="line">
    <div class="tree">
      <xsl:call-template name="graft"/>
      <xsl:apply-templates select="." mode="item"/>
    </div>
    
    <xsl:apply-templates select="./*" mode="line"/>
  </xsl:template>
  
  <xsl:template match="*" mode="item">
    <b class="tree"><xsl:value-of select="name()"/></b>
    <xsl:text> data: </xsl:text>
    <xsl:value-of select="self::*/text()"/>
    <xsl:for-each select="attribute::*">
      <xsl:text> | </xsl:text>
      <xsl:value-of select="name()"/>
      <xsl:text> -> </xsl:text>
      <xsl:value-of select="."/>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="graft">
    <xsl:apply-templates select="ancestor::*" mode="tree"/>
    <xsl:choose>
      <xsl:when test="following-sibling::*">
        <img src="./images/tree_tee.gif" alt="" class="tree"/>
      </xsl:when>
      <xsl:otherwise>
        <img src="./images/tree_corner.gif" alt="" class="tree"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="*" mode="tree">
    <xsl:choose>
      <xsl:when test="following-sibling::*">
        <img src="./images/tree_bar.gif" alt="" class="tree"/>
      </xsl:when>
      <xsl:otherwise>
        <img src="./images/tree_spacer.gif" alt="" class="tree"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
-------------------

Исправлен еще один баг. Надеюсь (наверное зря), что последний.
 
Сверху