Return to Snippet

Revision: 63552
at May 16, 2013 04:15 by iroybot


Initial Code
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet exclude-result-prefixes="xsl xsi html xsd" version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output encoding="UTF-8" indent="no" method="xml" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:param name="id" />
    
    <xsl:template match="/">
        <xsl:apply-templates select="//navigation[@id=$id]" />
    </xsl:template>
    
    <xsl:template match="navigation">
        <h1>Edit <xsl:value-of select="name" /> (<xsl:value-of select="language" />)</h1><br />
        <table class="table" id="links">
            <thead>
                <tr>
                    <th>Link</th>
                    <th>&#160;</th>
                    <th>URL</th>
                    <th>Title</th>
                    <th>Active</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <tr class="level0">
                    <td colspan="6"><i class="icon-home"><xsl:text> </xsl:text></i></td>
                </tr>
                <xsl:apply-templates select="links" />
                <tr><td colspan="6"><a data-navigation="{$id}" class="add-link btn btn-success btn-small" href="#add-link"><i class="icon-plus"><xsl:text> </xsl:text></i> Append a Link</a></td></tr>
            </tbody>
        </table>
    </xsl:template>
    
    <xsl:template match="link">
        <xsl:variable name="chain" select="ancestor-or-self::link" />
        <tr class="level{count($chain)}">
            <td>
                <xsl:apply-templates select="$chain" mode="tracks">
                    <xsl:with-param name="context" select="." />
                </xsl:apply-templates>
                <strong><xsl:value-of select="text" /></strong>
            </td>
            <td><a href="#" class="btn btn-success btn-mini add-link" data-navigation="{$id}" data-parent="{@id}"><i class="icon-plus"><xsl:text> </xsl:text></i> Append a Sublink</a><xsl:text> </xsl:text></td>
            <td><xsl:value-of select="url" /></td>
            <td>
                    <xsl:choose>
                        <xsl:when test="string-length(title)>0">
                            <xsl:value-of select="substring(title,0,50)" />
                        </xsl:when>
                        <xsl:otherwise>
                            <i class="red icon-bolt"><xsl:text> </xsl:text></i>
                        </xsl:otherwise>
                    </xsl:choose>
                </td>
                <td><xsl:value-of select="active" /></td>
                <td>
                    <div class="btn-group">
                        <xsl:choose>
                            <xsl:when test="active='yes'">
                                <a href="#" class="btn btn-mini deactivate" data-link="{@id}" data-navigation="{$id}"><i class="icon-check-empty"><xsl:text> </xsl:text></i> Deactivate</a>
                            </xsl:when>
                            <xsl:when test="active='no'">
                                <a href="#" class="btn btn-mini activate" data-link="{@id}" data-navigation="{$id}"><i class="icon-check"><xsl:text> </xsl:text></i> Activate</a>
                            </xsl:when>
                        </xsl:choose>
                        <a href="#" class="btn btn-mini edit" data-id="{@id}" data-navigation="{$id}"><i class="icon-edit"><xsl:text> </xsl:text></i> Edit</a>
                         
                        <a href="#" class="btn btn-mini edit" data-id="{@id}" data-navigation="{$id}"><i class="icon-edit"><xsl:text> </xsl:text></i> Advanced Edit</a>
                        
                        <a href="#">
                            <xsl:attribute name="class">btn btn-mini 
                                <xsl:choose>
                                    <xsl:when test="position()=1"> disabled</xsl:when>
                                </xsl:choose>
                            </xsl:attribute>
                            <i class="icon-circle-arrow-up"><xsl:text> </xsl:text></i> Up
                        </a>
                        <a href="#">
                            <xsl:attribute name="class">btn btn-mini 
                                <xsl:choose>
                                    <xsl:when test="position()=count($chain)"> disabled</xsl:when>
                                </xsl:choose>
                            </xsl:attribute>
                            <i class="icon-circle-arrow-down"><xsl:text> </xsl:text></i> Down
                        </a>
                        <a href="#" class="btn btn-mini btn-danger delete-link" data-id="{@id}" data-navigation="{$id}"><i class="icon-trash"><xsl:text> </xsl:text></i> Delete</a><xsl:text> </xsl:text>
                    </div>
                </td>
        </tr>
        <xsl:apply-templates select="links" />
    </xsl:template>
    
    <xsl:template match="link" mode="tracks">
        <xsl:param name="context" />
        <xsl:variable name="isSame" select="generate-id(.) = generate-id($context)" />
        <xsl:variable name="pos" select="count(preceding-sibling::link)" />
        <xsl:variable name="isLast" select="not(following-sibling::link)" />
        <xsl:variable name="type">
            <xsl:choose>
                <xsl:when test="$isSame">
                    <xsl:if test="(following::link or links/link) and not($isLast)">t</xsl:if>
                    <xsl:if test="$isLast">l</xsl:if>
                </xsl:when>
                <xsl:when test="$isLast and links/link">e</xsl:when>
                <xsl:when test="following::link and links/link">i</xsl:when>
                <xsl:when test="not(following::link)">e</xsl:when>
            </xsl:choose>
        </xsl:variable>
        <xsl:call-template name="node-type">
            <xsl:with-param name="type" select="$type" />
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="node-type">
        <xsl:param name="type" select="'t'" />
        <img class="textmiddle" src="/images/backend/{$type}-node.png" />
    </xsl:template>
</xsl:stylesheet>

Initial URL
http://stackoverflow.com/questions/15958557/tree-presentation-of-xml-structure-how-to-test-for-parents-preceding-siblings

Initial Description
a perfect tree representation of a XML structure (with i, l and t and empty node images)

Initial Title
XML structure to Tree View

Initial Tags

                                

Initial Language
XSLT