以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XSL/XSLT/XSL-FO/CSS 』  (http://bbs.xml.org.cn/list.asp?boardid=8)
----  [原创]我的一个xslt,请大家分享,不要吐我  (http://bbs.xml.org.cn/dispbbs.asp?boardid=8&rootid=&id=21064)


--  作者:bjshengyu333
--  发布时间:8/11/2005 2:59:00 PM

--  [原创]我的一个xslt,请大家分享,不要吐我
<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet xmlns:cps="xalan://com.emy.cps.misc.CpsUtils" xmlns:java="http://xml.apache.org/xslt/java" xmlns:token="xalan://java.util.StringTokenizer" xmlns:vars="xalan://java.util.HashMap" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="java vars cps token" version="1.0">
    <xsl:output encoding="UTF-8" method="xml"/>
    <xsl:template match="html">
        <xsl:choose>
            <xsl:when test="$page_mode='check'">
                <xsl:apply-templates mode="check" select="/"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates mode="normal" select="/"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!--
       imput parameters:
     description:
     page_mode    
       page_url   
       page_category  
       page_url   
     page_<param>   其他参数,由上级页面传递过来,如果                                                           需要使用,则需要加上相关定义。
      例如,如果要传递title, 则需要加入:

    -->
    <xsl:param name="page_mode">normal</xsl:param>
    <xsl:param name="page_sid">cps</xsl:param>
    <xsl:param name="page_schema">news</xsl:param>
    <xsl:param name="page_supplier">CPS_SYSTEM</xsl:param>
    <xsl:param name="page_category">/CPS_SYSTEM</xsl:param>
    <xsl:param name="page_refpage">http://www/</xsl:param>
    <xsl:param name="page_url">http://www/</xsl:param>

    <xsl:param name="page_style_channel_name_pattern"/>
    <xsl:param name="page_style_channel_url_pattern"/>
    
    <xsl:param name="page_style_channel_subcategory_url_pattern"/>
    <xsl:param name="page_style_channel_nonsubcategory_url_pattern"/>
    <xsl:param name="page_style_channel_subcategory_name_modifier"/>
    <xsl:param name="page_style_title_url_pattern"/>
    <xsl:param name="page_style_title_modifier"/>
    <xsl:param name="page_style_title_allow_keywords"/>
    <xsl:param name="page_style_title_deny_keywords"/>


<!-- 最新加上的参数,正在测试阶段 -->
    <xsl:param name="page_style_channel_level" select="'token'"/>
    <xsl:param name="page_style_define_channel_self" select="'首页新闻'"/>
    <xsl:param name="page_style_subcategory_name_pattern"/>
    <xsl:param name="page_style_subcategory_url_pattern"/>

    <xsl:param name="page_style_channel_name_token"/>
    <xsl:param name="page_style_channel_url_token"/>
    <xsl:param name="page_style_base_url_top"/>
    <xsl:param name="page_style_base_url_foot"/>

  <xsl:param name="page_style_max_title_page_no" select="4"/>
    <xsl:param name="page_title_currently_page_no" select="1"/>
    <xsl:param name="page_style_title_next_page_url_pattern" select="'[p|P]age=(\d+)'"/>
    <!--
        STD MOD: <xsl:template match="html" mode="normal">
    -->
<!-- 这里选择页面的级数 -->
<xsl:template match="html" mode="normal">
   <webinfo>
   <xsl:if test="vars:put('_page_style_channel_level','')"/>
   <xsl:choose>
   <xsl:when test="$page_style_channel_level='token'">
         <xsl:if test="vars:put('_page_style_channel_level','subcategory')"/>
         <xsl:call-template name="define-category"/>
      </xsl:when>
 <xsl:when test="$page_style_channel_level='main'">
       <xsl:apply-templates mode="a-main" select="body"/>
 </xsl:when>
 <xsl:when test="$page_style_channel_level='subcategory'">
       <xsl:apply-templates mode="a-subcategory" select="body"/>
   </xsl:when>
 <xsl:otherwise>
 <!-- nothing -->
 </xsl:otherwise>
</xsl:choose>
<!--  判断是否取下一页 -->
      <xsl:choose>
       <xsl:when test="string-length($page_style_max_title_page_no)&lt;1">
            <xsl:apply-templates mode="next-page" select="body"/> 
       </xsl:when>
       <xsl:otherwise>
         <xsl:if test="not(number($page_style_max_title_page_no)&lt;number($page_title_currently_page_no)) and $page_style_title_next_page_url_pattern!=''">
               <xsl:apply-templates mode="next-page" select="body"/>
           </xsl:if>
       </xsl:otherwise>
      </xsl:choose>
     </webinfo>
</xsl:template>

<!-- 得到下一页,注意这里要把主帖id和页号传入参数,其他主帖的相关信息也应传入 -->
<xsl:template match="body" mode="next-page">
<!--   屏幕输出页数  -->
    <xsl:variable name="out" select="java:com.emy.cps.ClientTools.getLog('currently_page:::title ')"/>
    <xsl:variable name="useless" select="java:error($out, concat('=============',$page_title_currently_page_no))"/>
    <xsl:if test="vars:put('_keep_onlyone_pass','true')"/>
    <xsl:variable name="estimate_next_page_no" select="number($page_title_currently_page_no)+1"/>       
    <xsl:for-each select=".//a[@href]">
       <xsl:variable name="url" select="cps:concatUrl($page_url,string(@href))"/>
      <xsl:if test="cps:isMatchPattern($url,$page_style_title_next_page_url_pattern)">
          <xsl:variable name="keep_onlyone_pass" select="vars:get('_keep_onlyone_pass')"/>
   <xsl:variable name="next_page_no" select="cps:getPattern($url,$page_style_title_next_page_url_pattern,1)"/>
       <xsl:if test="$next_page_no=$estimate_next_page_no and $keep_onlyone_pass='true'">
          <xsl:if test="vars:put('_keep_onlyone_pass','false')"/>
   <infocategory>
    <xsl:attribute name="supplier">
                 <xsl:value-of select="$page_supplier"/>
              </xsl:attribute>
    <xsl:attribute name="category">
                 <xsl:value-of select="$page_category"/>
              </xsl:attribute>
    <xsl:attribute name="url">
                 <xsl:value-of select="$url"/>
              </xsl:attribute>
    <xsl:attribute name="title_currently_page_no">
                 <xsl:value-of select="number($next_page_no)"/>
              </xsl:attribute>
  </infocategory>
  </xsl:if>
 </xsl:if>
  </xsl:for-each>
</xsl:template>

<!-- 处理首页面 -->
<xsl:template match="body" mode="a-main">
  <xsl:if test="vars:put('_main_position','false')"/>
  <xsl:if test="$page_style_channel_url_pattern!='' or  $page_style_channel_name_pattern!=''">
  <xsl:for-each select=".//a[@href]">
    <xsl:variable name="url" select="cps:concatUrl($page_url, string(@href))"/>
    <xsl:variable name="text" select="normalize-space(.)"/>
     <xsl:if test="$url!='' and cps:isMatchPattern($url,$page_style_channel_url_pattern) and cps:isMatchPattern($text,$page_style_channel_name_pattern)">
      <xsl:if test="vars:put('_main_position','true')"/>
<!--  -->
      <xsl:variable name="category">
        <xsl:choose>
          <xsl:when test="cps:isMatchPattern($url, $page_style_channel_subcategory_url_pattern)">
           <xsl:choose>
             <xsl:when test="$page_style_channel_subcategory_name_modifier!=''">
              <xsl:variable name="modified_text" select="cps:getPattern($text, $page_style_channel_subcategory_name_modifier, 1)"/>
               <xsl:value-of select="concat($page_category, '/', $modified_text)"/>
            </xsl:when>
         <xsl:otherwise>
         <xsl:value-of select="concat($page_category, '/', $text)"/>
         </xsl:otherwise>
       </xsl:choose>
     </xsl:when>
     <xsl:when test="$page_style_channel_nonsubcategory_url_pattern!='' and cps:isMatchPattern($url, $page_style_channel_nonsubcategory_url_pattern)">
      <xsl:value-of select="$page_category"/>
       </xsl:when>
      <xsl:otherwise>
         <xsl:value-of select="$page_category"/>
     </xsl:otherwise>
   </xsl:choose>
  </xsl:variable>
  <xsl:if test="vars:put('_page_style_channel_level','subcategory')"/>
    <xsl:apply-templates mode="a-info-category" select=".">
         <xsl:with-param name="url" select="$url"/>
         <xsl:with-param name="category" select="$category"/>
      </xsl:apply-templates>
     </xsl:if>
   </xsl:for-each>
   </xsl:if>
<!--  没有匹配的情况 -->
  <xsl:if test="vars:get('_main_position')='false'">
    <xsl:for-each select=".//a">
       <xsl:variable name="url" select="cps:concatUrl($page_url, string(@href))"/>
         <xsl:if test="cps:isMatchPattern($url, $page_style_title_url_pattern)">
          <xsl:apply-templates mode="a-news-title-with-filter" select=".">
               <xsl:with-param name="category">
                    <xsl:choose>
                   <xsl:when test="$page_style_define_channel_self!=''">
                    <xsl:value-of select="concat($page_category,'/',$page_style_define_channel_self)"/>
                     </xsl:when>
                    <xsl:otherwise>
                         <xsl:value-of select="$page_category"/>
                     </xsl:otherwise>
                     </xsl:choose>
                  </xsl:with-param>
          </xsl:apply-templates>
         </xsl:if>
</xsl:for-each>
   </xsl:if>
</xsl:template>


<!-- 处理子页面 -->
<xsl:template match="body" mode="a-subcategory">
   <xsl:if test="vars:put('_position','false')"/>
   <xsl:if test="$page_style_subcategory_url_pattern!='' or  $page_style_subcategory_name_pattern!=''">
   <xsl:for-each select=".//a[@href]">
     <xsl:variable name="url" select="cps:concatUrl($page_url, string(@href))"/>
     <xsl:variable name="text" select="normalize-space(.)"/>
     <xsl:if test="$url!='' and cps:isMatchPattern($url,$page_style_subcategory_url_pattern) and cps:isMatchPattern($text,$page_style_subcategory_name_pattern)">
      <!--xsl:variable name="position" select="position()"/-->
      <xsl:if test="vars:put('_position','true')"/>
      <xsl:variable name="category" select="concat($page_category,'/',$text)"/>
      <xsl:if test="vars:put('_page_style_channel_level','subcategory')"/>
      <xsl:apply-templates mode="a-info-category" select=".">
         <xsl:with-param name="url" select="$url"/>
         <xsl:with-param name="category" select="$category"/>
       </xsl:apply-templates>
    </xsl:if>
  </xsl:for-each>
  </xsl:if>
<!-- 没有匹配的情况  -->
   <xsl:if test="vars:get('_position')='false'">
     <xsl:for-each select=".//a">
                 <xsl:variable name="url" select="cps:concatUrl($page_url, string(@href))"/>
          <xsl:if test="cps:isMatchPattern($url, $page_style_title_url_pattern)">
          <xsl:apply-templates mode="a-news-title-with-filter" select=".">
              <xsl:with-param name="category" select="$page_category"/>
          </xsl:apply-templates>
         </xsl:if>
      </xsl:for-each>
  </xsl:if>
</xsl:template>

    <!--
        STD MOD:  result::a-info-category
         
     -->
<xsl:template match="a" mode="a-info-category">
        <xsl:param name="category"/>
        <xsl:param name="url"/>
        <infocategory>
            <xsl:attribute name="supplier">
                <xsl:value-of select="$page_supplier"/>
            </xsl:attribute>
            <xsl:attribute name="category">
                <xsl:value-of select="$category"/>
            </xsl:attribute>
            <xsl:attribute name="url">
                <xsl:value-of select="$url"/>
            </xsl:attribute>
            <xsl:attribute name="style_channel_level">
                <xsl:value-of select="vars:get('_page_style_channel_level')"/>
            </xsl:attribute>
        </infocategory>
    </xsl:template>


<!--
     STD MOD:  result::define-category
         

-->
<xsl:template name="define-category">
    <xsl:variable name="name_token" select="token:new(string($page_style_channel_name_token),'|')"/>
    <xsl:variable name="url_token" select="token:new($page_style_channel_url_token,'|')"/>
     <xsl:variable name="token_count" select="number(token:countTokens($name_token))"/>
     <xsl:for-each select="cps:makeNodeLoop($token_count)/*">
         <xsl:variable name="temp_name" select="token:nextToken($name_token)"/>
         <xsl:variable name="temp_url" select="token:nextToken($url_token)"/>
         <xsl:variable name="category" select="$temp_name"/>
         <xsl:variable name="url" select="concat($page_style_base_url_top,$temp_url,$page_style_base_url_foot)"/>
          <infocategory>
            <xsl:attribute name="supplier">
                <xsl:value-of select="$page_supplier"/>
            </xsl:attribute>
            <xsl:attribute name="category">
                <xsl:value-of select="concat($page_category,'/',$category)"/>
            </xsl:attribute>
            <xsl:attribute name="url">
                <xsl:value-of select="$url"/>
            </xsl:attribute>
            <xsl:attribute name="style_channel_level">
                <xsl:value-of select="vars:get('_page_style_channel_level')"/>
            </xsl:attribute>
        </infocategory>
    </xsl:for-each>

<!--
       
-->
    </xsl:template>


    <!--
        STD MOD; result::a-news-title-with-filter
        1.对标题进行修改,参数:page_style_title_modifier
                2.其他参数:           page_style_title_allow_keywords
                                                            page_style_title_deny_keywords
    -->
    <xsl:template match="a" mode="a-news-title-with-filter">
     <xsl:param name="category"/>
        <xsl:variable name="text" select="normalize-space(.)"/>
        <xsl:variable name="text" select="cps:removePattern($text,'null\s*')"/>
        <xsl:variable name="title">
            <xsl:choose>
                <xsl:when test="$page_style_title_modifier!='' and cps:isMatchPattern($text,$page_style_title_modifier)">
                    <xsl:value-of select="cps:getPattern($text, $page_style_title_modifier, 1)"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$text"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="url" select="cps:concatUrl($page_url, @href)"/>
        <xsl:if test="$title!='null' and string-length($title)&gt;=2">
            <xsl:choose>
                <xsl:when test="$page_style_title_allow_keywords!=''">
                    <xsl:choose>
                        <xsl:when test="cps:isMatchPattern($title, $page_style_title_allow_keywords)">
                            <infocategory _type="news">
                                <xsl:attribute name="_schema">
                                    <xsl:value-of select="$page_schema"/>
                                </xsl:attribute>
                                <xsl:attribute name="supplier">
                                    <xsl:value-of select="$page_supplier"/>
                                </xsl:attribute>
                                <xsl:attribute name="category">
                                    <xsl:value-of select="$category"/>
                                </xsl:attribute>
                                <xsl:attribute name="title">
                                    <xsl:value-of select="$title"/>
                                </xsl:attribute>
                                <xsl:attribute name="url">
                                    <xsl:value-of select="$url"/>
                                </xsl:attribute>
                            </infocategory>
                        </xsl:when>
                    </xsl:choose>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:if test="$page_style_title_deny_keywords='' or not(cps:isMatchPattern($title, $page_style_title_deny_keywords))">
                        <infocategory _type="news">
                            <xsl:attribute name="_schema">
                                <xsl:value-of select="$page_schema"/>
                            </xsl:attribute>
                            <xsl:attribute name="supplier">
                                <xsl:value-of select="$page_supplier"/>
                            </xsl:attribute>
                            <xsl:attribute name="category">
                                <xsl:value-of select="$category"/>
                            </xsl:attribute>
                            <xsl:attribute name="title">
                                <xsl:value-of select="$title"/>
                            </xsl:attribute>
                            <xsl:attribute name="url">
                                <xsl:value-of select="$url"/>
                            </xsl:attribute>
                        </infocategory>
                    </xsl:if>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>


--  作者:孤独
--  发布时间:8/11/2005 8:50:00 PM

--  
必须以来java类库哦...
支持一个.
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
114.746ms