xsl.xmltohtml

来源:互联网 发布:阿里云搭建ss 贴吧 编辑:程序博客网 时间:2024/04/29 14:02
HTMLXSL.xslt.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="#"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
<xsl:param name="prefix" select="generate-id()"/>
    
<xsl:template match="/">
        
<style>
        div.
<xsl:value-of select="$prefix"/>global
        {
            font-family:arial;
        }
        span.
<xsl:value-of select="$prefix"/>button
        {
            cursor:s-resize;
        }
        span.
<xsl:value-of select="$prefix"/>nodemark
        {
            color:blue;
        }
        span.
<xsl:value-of select="$prefix"/>nodename
        {
            color:brown;
        }
        div.
<xsl:value-of select="$prefix"/>indent
        {
            margin-left:20;
        }
        span.
<xsl:value-of select="$prefix"/>attributename
        {
            color:red;
        }
        span.
<xsl:value-of select="$prefix"/>attributemark
        {
            color:blue;
        }
        span.
<xsl:value-of select="$prefix"/>attributevalue
        {
            font-weight:bold;
        }
        
</style>
        
<div>
            
<xsl:attribute name="class"><xsl:value-of select="$prefix"/>global</xsl:attribute>
            
<xsl:apply-templates select="." mode="node"/>
        
</div>
    
</xsl:template>
    
<xsl:template match="*" mode="node">
        
<xsl:choose>
            
<xsl:when test="count(*)=0"><xsl:apply-templates select="." mode="nodenochild"/></xsl:when>
            
<xsl:otherwise><xsl:apply-templates select="." mode="nodehavechild"/></xsl:otherwise>
        
</xsl:choose>
    
</xsl:template>
    
<xsl:template match="*" mode="nodenochild">
        
<div>
            
<xsl:call-template name="lt"/>
            
<xsl:apply-templates select="." mode="nodename"/>
            
<xsl:apply-templates select="." mode="attributes"/>
            
<xsl:call-template name="slash"/>
            
<xsl:call-template name="gt"/>
        
</div>
    
</xsl:template>
    
<xsl:template match="*" mode="nodehavechild">
        
<xsl:variable name="id" select="generate-id()"/>
        
<div style="display='none'">
            
<xsl:attribute name="id"><xsl:value-of select="$id"/>shrink</xsl:attribute>
            
<span>
                
<xsl:attribute name="class"><xsl:value-of select="$prefix"/>button</xsl:attribute>
                
<xsl:attribute name="onclick"><xsl:value-of select="$id"/>expand.style.display='inline';<xsl:value-of select="$id"/>shrink.style.display='none';</xsl:attribute>
                
<xsl:text>+</xsl:text>
            
</span>
            
<xsl:call-template name="lt"/>
            
<xsl:apply-templates select="." mode="nodename"/>
            
<xsl:apply-templates select="." mode="attributes"/>
            
<xsl:call-template name="slash"/>
            
<xsl:call-template name="gt"/>
        
</div>
        
<div>
            
<xsl:attribute name="id"><xsl:value-of select="$id"/>expand</xsl:attribute>
            
<div>
                
<span>
                    
<xsl:attribute name="class"><xsl:value-of select="$prefix"/>button</xsl:attribute>
                    
<xsl:attribute name="onclick"><xsl:value-of select="$id"/>expand.style.display='none';<xsl:value-of select="$id"/>shrink.style.display='inline';</xsl:attribute>
                    
<xsl:text>-</xsl:text>
                
</span>
                
<xsl:call-template name="lt"/>
                
<xsl:apply-templates select="." mode="nodename"/>
                
<xsl:apply-templates select="." mode="attributes"/>
                
<xsl:call-template name="gt"/>
            
</div>
            
<div>
                
<xsl:attribute name="class"><xsl:value-of select="$prefix"/>indent</xsl:attribute>
                
<xsl:apply-templates select="*" mode="node"/>
            
</div>
            
<div>
                
<xsl:call-template name="lt"/>
                
<xsl:call-template name="slash"/>
                
<xsl:apply-templates select="." mode="nodename"/>
                
<xsl:call-template name="gt"/>
            
</div>
        
</div>
    
</xsl:template>
    
<xsl:template match="*" mode="attributes">
        
<xsl:for-each select="@*">
            
<xsl:text> </xsl:text>
            
<span>
                
<xsl:attribute name="class"><xsl:value-of select="$prefix"/>attributename</xsl:attribute>
                
<xsl:value-of select="name()"/>
            
</span>
            
<span>
                
<xsl:attribute name="class"><xsl:value-of select="$prefix"/>attributemark</xsl:attribute>
                
<xsl:text>="</xsl:text>
            
</span>
            
<span>
                
<xsl:attribute name="class"><xsl:value-of select="$prefix"/>attributevalue</xsl:attribute>
                
<xsl:value-of select="."/>
            
</span>
            
<span>
                
<xsl:attribute name="class"><xsl:value-of select="$prefix"/>attributemark</xsl:attribute>
                
<xsl:text>"</xsl:text>
            
</span>
        
</xsl:for-each>
    
</xsl:template>
    
<xsl:template name="lt">
        
<span>
            
<xsl:attribute name="class"><xsl:value-of select="$prefix"/>nodemark</xsl:attribute>
            
<xsl:text>&lt;</xsl:text>
        
</span>
    
</xsl:template>
    
<xsl:template name="gt">
        
<span>
            
<xsl:attribute name="class"><xsl:value-of select="$prefix"/>nodemark</xsl:attribute>
            
<xsl:text>&gt;</xsl:text>
        
</span>
    
</xsl:template>
    
<xsl:template name="slash">
        
<span>
            
<xsl:attribute name="class"><xsl:value-of select="$prefix"/>nodemark</xsl:attribute>
            
<xsl:text>/</xsl:text>
        
</span>
    
</xsl:template>
    
<xsl:template match="*" mode="nodename">
        
<span>
            
<xsl:attribute name="class"><xsl:value-of select="$prefix"/>nodename</xsl:attribute>
            
<xsl:value-of select="name()"/>
        
</span>
    
</xsl:template>
</xsl:transform>
 生成效果如下
-<xsl:transform version="1.0">
<xsl:param name="prefix" select="generate-id()"/>
-<xsl:template match="/">
-<style>
<xsl:value-of select="$prefix"/>
<xsl:value-of select="$prefix"/>
<xsl:value-of select="$prefix"/>
<xsl:value-of select="$prefix"/>
<xsl:value-of select="$prefix"/>
<xsl:value-of select="$prefix"/>
<xsl:value-of select="$prefix"/>
<xsl:value-of select="$prefix"/>
</style>
-<div>
-<xsl:attribute name="class">
<xsl:value-of select="$prefix"/>
</xsl:attribute>
<xsl:apply-templates select="." mode="node"/>
</div>
</xsl:template>
-<xsl:template match="*" mode="node">
-<xsl:choose>
-<xsl:when test="count(*)=0">
<xsl:apply-templates select="." mode="nodenochild"/>
</xsl:when>
-<xsl:otherwise>
<xsl:apply-templates select="." mode="nodehavechild"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
-<xsl:template match="*" mode="nodenochild">
-<div>
<xsl:call-template name="lt"/>
<xsl:apply-templates select="." mode="nodename"/>
<xsl:apply-templates select="." mode="attributes"/>
<xsl:call-template name="slash"/>
<xsl:call-template name="gt"/>
</div>
</xsl:template>
-<xsl:template match="*" mode="nodehavechild">
<xsl:variable name="id" select="generate-id()"/>
-<div style="display='none'">
-<xsl:attribute name="id">
<xsl:value-of select="$id"/>
</xsl:attribute>
-<span>
-<xsl:attribute name="class">
<xsl:value-of select="$prefix"/>
</xsl:attribute>
-<xsl:attribute name="onclick">
<xsl:value-of select="$id"/>
<xsl:value-of select="$id"/>
</xsl:attribute>
<xsl:text/>
</span>
<xsl:call-template name="lt"/>
<xsl:apply-templates select="." mode="nodename"/>
<xsl:apply-templates select="." mode="attributes"/>
<xsl:call-template name="slash"/>
<xsl:call-template name="gt"/>
</div>
-<div>
-<xsl:attribute name="id">
<xsl:value-of select="$id"/>
</xsl:attribute>
-<div>
-<span>
-<xsl:attribute name="class">
<xsl:value-of select="$prefix"/>
</xsl:attribute>
-<xsl:attribute name="onclick">
<xsl:value-of select="$id"/>
<xsl:value-of select="$id"/>
</xsl:attribute>
<xsl:text/>
</span>
<xsl:call-template name="lt"/>
<xsl:apply-templates select="." mode="nodename"/>
<xsl:apply-templates select="." mode="attributes"/>
<xsl:call-template name="gt"/>
</div>
-<div>
-<xsl:attribute name="class">
<xsl:value-of select="$prefix"/>
</xsl:attribute>
<xsl:apply-templates select="*" mode="node"/>
</div>
-<div>
<xsl:call-template name="lt"/>
<xsl:call-template name="slash"/>
<xsl:apply-templates select="." mode="nodename"/>
<xsl:call-template name="gt"/>
</div>
</div>
</xsl:template>
-<xsl:template match="*" mode="attributes">
-<xsl:for-each select="@*">
<xsl:text/>
-<span>
-<xsl:attribute name="class">
<xsl:value-of select="$prefix"/>
</xsl:attribute>
<xsl:value-of select="name()"/>
</span>
-<span>
-<xsl:attribute name="class">
<xsl:value-of select="$prefix"/>
</xsl:attribute>
<xsl:text/>
</span>
-<span>
-<xsl:attribute name="class">
<xsl:value-of select="$prefix"/>
</xsl:attribute>
<xsl:value-of select="."/>
</span>
-<span>
-<xsl:attribute name="class">
<xsl:value-of select="$prefix"/>
</xsl:attribute>
<xsl:text/>
</span>
</xsl:for-each>
</xsl:template>
-<xsl:template name="lt">
-<span>
-<xsl:attribute name="class">
<xsl:value-of select="$prefix"/>
</xsl:attribute>
<xsl:text/>
</span>
</xsl:template>
-<xsl:template name="gt">
-<span>
-<xsl:attribute name="class">
<xsl:value-of select="$prefix"/>
</xsl:attribute>
<xsl:text/>
</span>
</xsl:template>
-<xsl:template name="slash">
-<span>
-<xsl:attribute name="class">
<xsl:value-of select="$prefix"/>
</xsl:attribute>
<xsl:text/>
</span>
</xsl:template>
-<xsl:template match="*" mode="nodename">
-<span>
-<xsl:attribute name="class">
<xsl:value-of select="$prefix"/>
</xsl:attribute>
<xsl:value-of select="name()"/>
</span>
</xsl:template>
</xsl:transform>
原创粉丝点击