Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- <xsl:output method="xml" indent="yes"/>
- <xsl:strip-space elements="*"/>
- <!-- copy all other nodes -->
- <xsl:template match="node() | @*">
- <xsl:copy>
- <xsl:apply-templates select="node() | @*"/>
- </xsl:copy>
- </xsl:template>
- <!-- find every list element which has a preceding non-list element -->
- <xsl:template match="list[not(preceding-sibling::*[1][self::list])]">
- <!-- now walk recursive through all lists -->
- <xsl:apply-templates select="self::list" mode="recurse">
- <xsl:with-param name="level1_margin" select="@margin"/>
- <xsl:with-param name="level" select="1"/>
- </xsl:apply-templates>
- </xsl:template>
- <!-- remove other list elements, because they are recursive processed -->
- <xsl:template match="list"/>
- <!-- remove @margin from list -->
- <xsl:template match="list/@margin"/>
- <!-- go recursive through all following lists -->
- <xsl:template match="list" mode="recurse">
- <xsl:param name="level1_margin" select="0"/>
- <xsl:param name="level" select="1"/>
- <xsl:variable name="nextStep" select="self::list/following-sibling::*[1][self::list]"/>
- <!-- create current list element with its level -->
- <xsl:apply-templates select="self::list" mode="create">
- <xsl:with-param name="level" select="$level"/>
- </xsl:apply-templates>
- <xsl:if test="$nextStep">
- <xsl:choose>
- <!-- new start margin/point for level 1 -->
- <xsl:when test="($nextStep/@margin <= $level1_margin) or ($nextStep/@margin < @margin and $level = 2)">
- <xsl:apply-templates select="$nextStep" mode="recurse">
- <xsl:with-param name="level1_margin" select="$nextStep/@margin"/>
- <xsl:with-param name="level" select="1"/>
- </xsl:apply-templates>
- </xsl:when>
- <!-- -1 -->
- <xsl:when test="$nextStep/@margin < @margin and $level > 1">
- <xsl:apply-templates select="$nextStep" mode="recurse">
- <xsl:with-param name="level1_margin" select="$level1_margin"/>
- <xsl:with-param name="level" select="$level - 1"/>
- </xsl:apply-templates>
- </xsl:when>
- <!-- +1 -->
- <xsl:when test="$nextStep/@margin > @margin">
- <xsl:apply-templates select="$nextStep" mode="recurse">
- <xsl:with-param name="level1_margin" select="$level1_margin"/>
- <xsl:with-param name="level" select="$level + 1"/>
- </xsl:apply-templates>
- </xsl:when>
- <!-- +-0 -->
- <xsl:otherwise>
- <xsl:apply-templates select="$nextStep" mode="recurse">
- <xsl:with-param name="level1_margin" select="$level1_margin"/>
- <xsl:with-param name="level" select="$level"/>
- </xsl:apply-templates>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:if>
- </xsl:template>
- <!-- create list element with level attribute -->
- <xsl:template match="list" mode="create">
- <xsl:param name="level"/>
- <list>
- <xsl:attribute name="level">
- <xsl:value-of select="$level"/>
- </xsl:attribute>
- <xsl:apply-templates select="@*"/>
- <xsl:apply-templates/>
- </list>
- </xsl:template>
- </xsl:stylesheet>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement