当柳上原的风吹向天际的时候...

真正的快乐来源于创造

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks
1.使用Substring进行字符串截取
输入:
<date>122811</date>

输出:
<?xml version="1.0" encoding="UTF-8"?>
<date>12-28-2011</date>

代码:
<?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="iso-8859-1" indent="yes"/>
    
    
<xsl:template match="/date">    
        
<date>
        
<xsl:value-of select="concat(substring(.,1,2),'-',substring(.,3,2),'-20',substring(.,5,2))"/>    
        
</date>
    
</xsl:template>    
    
</xsl:stylesheet>


2.使用substring-before和substring-after进行字符串截取
输入:
<lists><list><class><phonelist>123-456-789 623-436-189 923-056-329</phonelist></class></list><list2><class><phonelist>523-556-589 623-636-689 923-956-929</phonelist></class></list2></lists>

输出:
<?xml version="1.0" encoding="UTF-8"?>
<lists>
<list>
<class>
<phone>123-456-789</phone>
<phone>623-436-189</phone>
<phone>923-056-329</phone>
</class>
</list>
<list2>
<class>
<phone>523-556-589</phone>
<phone>623-636-689</phone>
<phone>923-956-929</phone>
</class>
</list2>
</lists>

代码:
<?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="iso-8859-1" indent="yes"/>
    
<xsl:template match="/">
        
<lists>
            
<list><class>
            
<xsl:for-each select="lists/list/class/phonelist">
                
<phone><xsl:value-of select="substring-before(normalize-space(.),' ')"/></phone>
                
<phone><xsl:value-of select="substring-before(substring-after(normalize-space(.),' '),' ')"/></phone>
                
<phone><xsl:value-of select="substring-after(substring-after(normalize-space(.),' '),' ')"/></phone>
            
</xsl:for-each>
            
</class></list>
            
            
<list2><class>
            
<xsl:for-each select="lists/list2/class/phonelist">
                
<phone><xsl:value-of select="substring-before(normalize-space(.),' ')"/></phone>
                
<phone><xsl:value-of select="substring-before(substring-after(normalize-space(.),' '),' ')"/></phone>
                
<phone><xsl:value-of select="substring-after(substring-after(normalize-space(.),' '),' ')"/></phone>
            
</xsl:for-each>
            
</class></list2>
        
</lists>
    
</xsl:template>    
</xsl:stylesheet>

3.使用XPath选择不同节点
输入:
<lists><A><A1>A1</A1><A2>A2</A2></A><B><B1>B1</B1></B></lists>

输出:
<?xml version="1.0" encoding="UTF-8"?>
<lists>
<AB>
<A1>A1</A1>
<B1>B1</B1>
<A2>A2</A2>
</AB>
</lists>

代码:
<?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="iso-8859-1" indent="yes"/>
    
<xsl:template match="/">
        
<lists>
            
<AB>
                
<A1><xsl:value-of select="/lists/A/A1"/></A1>
                
<B1><xsl:value-of select="/lists/B/B1"/></B1>
                
<A2><xsl:value-of select="/lists/A/A2"/></A2>
            
</AB>
        
</lists>
    
</xsl:template>    
</xsl:stylesheet>

4.取得兄弟节点。
.输入:
<list><group><seat id="A1"><price>1111.00</price></seat><seat id="A2"><price>2222.00</price></seat><seat id="A3"><price>3333.00</price></seat><seat id="A4"><price>4444.00</price></seat></group></list>

输出:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<seat id="A1" Next="A2">
<price>1111.00</price>
</seat>
<seat id="A2" Privious="A1" Next="A3">
<price>2222.00</price>
</seat>
<seat id="A3" Privious="A2" Next="A4">
<price>3333.00</price>
</seat>
<seat id="A4" Privious="A3">
<price>4444.00</price>
</seat>
</list>

代码:
<?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="iso-8859-1" indent="yes"/>

    
<xsl:template match="/">
        
<xsl:variable name="seatCount" select="count(list/group/seat)"/>
        
<list>
        
<xsl:for-each select="list/group/seat">
            
<seat>
                
<xsl:choose>
                    
<xsl:when test="position()= 1">
                        
<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
                        
<xsl:attribute name="Next"><xsl:value-of select="following-sibling::seat[1]/attribute::id"/></xsl:attribute>
                        
<price><xsl:value-of select="price"/></price>
                    
</xsl:when>                                       
                    
                    
                    
<xsl:when test="position()= $seatCount">
                        
<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
                        
<xsl:attribute name="Privious"><xsl:value-of select="preceding-sibling::seat[1]/attribute::id"/></xsl:attribute>
                        
<price><xsl:value-of select="price"/></price>
                    
</xsl:when>
                    
                    
<xsl:otherwise>
                        
<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>                        
                        
<xsl:attribute name="Privious"><xsl:value-of select="preceding-sibling::seat[1]/attribute::id"/></xsl:attribute>
                        
<xsl:attribute name="Next"><xsl:value-of select="following-sibling::seat[1]/attribute::id"/></xsl:attribute>
                        
<price><xsl:value-of select="price"/></price>
                    
</xsl:otherwise>
                
</xsl:choose>
            
</seat>
        
</xsl:for-each>
        
</list>
    
</xsl:template>    
</xsl:stylesheet>
5.使用string-length得到字符串长度并用xsl:if判断。
输入:
<list><group><seat><id>11</id><price/></seat><seat><id></id><price>2222.00</price></seat><seat><id/><price>3333.00</price></seat><seat><id>44</id><price></price></seat><seat><id>55</id><price>5555.00</price></seat></group></list>

输出:
<?xml version="1.0" encoding="UTF-8"?>
<seat>
<id>11</id>
</seat>
<seat>
<price>2222.00</price>
</seat>
<seat>
<price>3333.00</price>
</seat>
<seat>
<id>44</id>
</seat>
<seat>
<id>55</id>
<price>5555.00</price>
</seat>

代码:
<?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="iso-8859-1" indent="yes"/>

    
<xsl:template match="/">
        
<xsl:variable name="seatCount" select="count(list/group/seat)"/>
    
        
<xsl:for-each select="list/group/seat">
            
<seat>
                
<xsl:if test="string-length(id) &gt; 0">
                    
<id><xsl:value-of select="id"/></id>
                
</xsl:if>
                
<xsl:if test="string-length(price) &gt; 0">
                    
<price><xsl:value-of select="price"/></price>
                
</xsl:if>
            
</seat>
        
</xsl:for-each>
    
</xsl:template>    
</xsl:stylesheet>


posted on 2011-12-14 09:49 何杨 阅读(379) 评论(0)  编辑  收藏 所属分类: WMB

只有注册用户登录后才能发表评论。


网站导航: