XSL使用笔记

选择指定指定子节点

<xsl:value-of select="format-number(/xxx/AnnualIncomeDetail[position()=last()]/Sales, '#,###')"/>

1. 批量替换
批量替换FAP/Content/Document/Properties/body内容里边的</Ticker>info</Ticker>

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:variable name="body"><xsl:apply-templates select="FAP/Content/Document/Properties/body"/></xsl:variable>
    .....
</xsl:template>

<xsl:template match="FAP/Content/Document/Properties/body">
  
<xsl:apply-templates /> 
</xsl:template>
<xsl:template match="Ticker">
   
<xsl:text/>&lt;a href="http://quote.morningstar.com/Switch.html?ticker=<xsl:value-of select="."/>"&gt;<xsl:value-of select="."/>&lt;/a&gt;<xsl:text/>
</xsl:template> 
</xsl:stylesheet>

2. 把<![CDATA[ asdf<p> cdb </p> name's <ticker>ticker</ticker>...]]>格式化
由于字符中包括了'或",因此无法通过外部的JS来转化(无法把这些内容当成字符串发给JS)。
用没有引号的JS实现

<xsl:stylesheet xmlns:HTML="http://www.w3.org/Profiles/XHTML-transitional" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" method="html" media-type="text/html" indent="no" encoding="iso8859-1"/>
<xsl:template match="/">
<script language="Javascript"><xsl:comment><![CDATA[
function format(description)
{
    var re=/&lt;/g;
    var descriptStr=description.replace(re,"<");
    re=/&gt;/g;
    descriptStr=descriptStr.replace(re,">");
    re=/&amp;/g;
    descriptStr=descriptStr.replace(re,"&");
    re=/""/g;
    descriptStr=descriptStr.replace(re,'"');
    re=/&quot;/g;
    descriptStr=descriptStr.replace(re,"'");
    re=/&#039;/g;
    descriptStr=descriptStr.replace(re,"'");
   
    document.write(descriptStr);
}
]]></xsl:comment></SCRIPT>
<xsl:template match="Fund/Content/FundFamilyFiduciaryGrade/DirectorIndependence">
    
<xsl:apply-templates />
</xsl:template>
<xsl:template match="ticker">
  
<a><xsl:attribute name="href"><xsl:choose>
    
<xsl:when test="/Fund/Footer/HostServer[. != 'online.morningstar.com']">  
       http://quote.morningstar.com/Switch.html?ticker=
<xsl:value-of select="."/>
    
</xsl:when>
    
<xsl:otherwise>
       /quote/Switch.html?ticker=
<xsl:value-of select="."/>
    
</xsl:otherwise>
    
</xsl:choose>
    
</xsl:attribute><script>caseConvert('<xsl:value-of select="."/>')</script></a>
</xsl:template>
<xsl:template match="text()">
<script>format('<xsl:value-of select="." />')</script>
</xsl:template>
</xsl:stylesheet>
非JS方式
<xsl:stylesheet xmlns:HTML="http://www.w3.org/Profiles/XHTML-transitional" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" method="html" media-type="text/html" indent="no" encoding="iso8859-1"/>
<xsl:template match="/">
<xsl:template match="Fund/Content/FundFamilyFiduciaryGrade/DirectorIndependence">
    
<xsl:apply-templates />
</xsl:template>
<xsl:template match="ticker">
  
<a><xsl:attribute name="href"><xsl:choose>
    
<xsl:when test="/Fund/Footer/HostServer[. != 'online.morningstar.com']">  
       http://quote.morningstar.com/Switch.html?ticker=
<xsl:value-of select="."/>
    
</xsl:when>
    
<xsl:otherwise>
       /quote/Switch.html?ticker=
<xsl:value-of select="."/>
    
</xsl:otherwise>
    
</xsl:choose>
    
</xsl:attribute><script>caseConvert('<xsl:value-of select="."/>')</script></a>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:template>
</xsl:stylesheet>

 

3. 匹配替换

<xsl:template name="StringReplace">
  
<xsl:param name="SrcString"/>
  
<xsl:param name="FromString"/>
  
<xsl:param name="ToString"/>
  
<xsl:choose>
   
<xsl:when test="contains($SrcString,$FromString)">
    
<xsl:value-of select="substring-before($SrcString,$FromString)" disable-output-escaping="yes"/>
    
<xsl:value-of select="$ToString" disable-output-escaping="yes"/>
    
<xsl:call-template name="StringReplace">
     
<xsl:with-param name="SrcString" select="substring-after($SrcString,$FromString)"/>
     
<xsl:with-param name="FromString" select="$FromString"/>
     
<xsl:with-param name="ToString" select="$ToString"/>
    
</xsl:call-template>
   
</xsl:when>
   
<xsl:otherwise><xsl:value-of select="$SrcString" disable-output-escaping="yes"/></xsl:otherwise>
  
</xsl:choose>
 
</xsl:template>

Test

1.xsl
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:HTML="http://www.w3.org/Profiles/XHTML-transitional" xmlns:ms="urn:anything">
<xsl:output omit-xml-declaration="yes" method="html" media-type="text/html" indent="no" encoding="iso8859-1"/>
<xsl:template name="StringReplace">
  
<xsl:param name="SrcString"/>
  
<xsl:param name="FromString"/>
  
<xsl:param name="ToString"/>
  
<xsl:choose>
   
<xsl:when test="contains($SrcString,$FromString)">
    
<xsl:value-of select="substring-before($SrcString,$FromString)"/>
    
<xsl:value-of select="$ToString"/>
    
<xsl:call-template name="StringReplace">
     
<xsl:with-param name="SrcString" select="substring-after($SrcString,$FromString)"/>
     
<xsl:with-param name="FromString" select="$FromString"/>
     
<xsl:with-param name="ToString" select="$ToString"/>
    
</xsl:call-template>
   
</xsl:when>
   
<xsl:otherwise><xsl:value-of select="$SrcString"/></xsl:otherwise>
  
</xsl:choose>
 
</xsl:template>
<xsl:template match="/">
<xsl:variable name="Tmp">
<xsl:call-template name="StringReplace">
     
<xsl:with-param name="SrcString" select="/Fund/t1"/>
     
<xsl:with-param name="FromString" select="string('t123')"/>
     
<xsl:with-param name="ToString" select="string('')"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="StringReplace">
     
<xsl:with-param name="SrcString" select="$Tmp"/>
     
<xsl:with-param name="FromString" select="string('t456')"/>
     
<xsl:with-param name="ToString" select="string('')"/>
</xsl:call-template>
</xsl:template>

</xsl:stylesheet>
1.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="1.xsl"?>
<Fund><t1>
t456t123's 
&amp;t1200t456700
</t1></Fund>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值