以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XSL/XSLT/XSL-FO/CSS 』  (http://bbs.xml.org.cn/list.asp?boardid=8)
----  求助~关于HTML标题数字化怎样用XSLT表达?  (http://bbs.xml.org.cn/dispbbs.asp?boardid=8&rootid=&id=25239)


--  作者:glueckschwein
--  发布时间:12/10/2005 6:43:00 PM

--  求助~关于HTML标题数字化怎样用XSLT表达?
题目是写出一个XSLT,对任意输入的一个XHTML文件,都能输出一个于原文件相符的新XHTML文件来,但在文件里元素H1到H6是层次数据化的。
输入是:
           <h1>Titel 1</h1>
           <h2>Titel 2</h1>
           <h3>Titel 3</h1>
           <h3>Titel 4</h1>
           <h1>Titel 5</h1>

输出是:
          <h1>1 Titel 1</h1>
          <h2>1.1 Titel 2</h1>
          <h3>1.1.1 Titel 3</h1>
          <h3>1.1.2 Titel 4</h1>
          <h1>2 Titel 5</h1>
请高人帮偶看看,应该怎样写出这个XSLT,多谢啦~~~~~


--  作者:98900969r
--  发布时间:12/11/2005 3:21:00 PM

--  
这道题目超难,可以拿来作为比拼。得有奖励才行,呵呵。你设个奖吧,偶试试。
--  作者:glueckschwein
--  发布时间:12/12/2005 2:41:00 AM

--  
呵呵, 我也认为很难~~可我不是斑竹,怎么给奖励哈~~只能顶你了~
--  作者:98900969r
--  发布时间:12/12/2005 2:39:00 PM

--  
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xh="http://www.w3.org/1999/xhtml">
 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

 <xsl:template match="*">
  <xsl:copy>
   <xsl:apply-templates select="@*"/>
   <xsl:apply-templates/>
  </xsl:copy>
 </xsl:template>
 
 <xsl:template match="@* | text() | processing-instruction() | comment()">
  <xsl:copy/>
 </xsl:template>
 
 <xsl:template match="text() [parent::xh:h1 or parent::xh:h2 or parent::xh:h3 or parent::xh:h4 or parent::xh:h5 or parent::xh:h6]">
  <xsl:for-each select="parent::*">
   <xsl:variable name="n0" select="number( substring-after(local-name(.), 'h') )"/>
   <xsl:variable name="n" select="$n0"/>
   <xsl:call-template name="hn">
    <xsl:with-param name="n0" select="$n0"/>
    <xsl:with-param name="n" select="$n"/>
    <xsl:with-param name="s0" select="''"/>
   </xsl:call-template>
  </xsl:for-each>
  <xsl:apply-templates/>
 </xsl:template>
 
 <xsl:template name="hn">
  <xsl:param name="n0"/>
  <xsl:param name="n"/>
  <xsl:param name="s0"/>
  <xsl:choose>
   <xsl:when test="$n>=1">
    <xsl:variable name="hp">
     <xsl:variable name="hn_1_t" select="string(preceding-sibling::* [local-name(.)=concat('h', number($n)-1)] [last()])"/>
     <xsl:choose>
      <xsl:when test="$n=1">
       <xsl:choose>
        <xsl:when test="$n!=$n0">
         <xsl:value-of select="count(preceding-sibling::* [local-name(.)=concat('h', $n)])"/>
        </xsl:when>
        <xsl:otherwise>
         <xsl:value-of select="count(preceding-sibling::* [local-name(.)=concat('h', $n)] )+1"/>
        </xsl:otherwise>
       </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
       <xsl:choose>
        <xsl:when test="$n!=$n0">
         <xsl:value-of select="count(preceding-sibling::* [local-name(.)=concat('h', $n) and preceding-sibling::* [local-name(.)=concat('h', number($n)-1) and .=$hn_1_t ] ] )"/>
        </xsl:when>
        <xsl:otherwise>
         <xsl:value-of select="count(preceding-sibling::* [local-name(.)=concat('h', $n) and preceding-sibling::* [local-name(.)=concat('h', number($n)-1) and .=$hn_1_t ] ] )+1"/>
        </xsl:otherwise>
       </xsl:choose>
      </xsl:otherwise>
     </xsl:choose>
    </xsl:variable>
    <xsl:variable name="s">
     <xsl:choose>
      <xsl:when test="$n=$n0">
       <xsl:value-of select="concat($s0, $hp, ' ', .)"/>
      </xsl:when>
      <xsl:otherwise>
       <xsl:value-of select="concat($hp, '.', $s0 )"/>
      </xsl:otherwise>
     </xsl:choose>
    </xsl:variable>
    <xsl:call-template name="hn">
     <xsl:with-param name="n0" select="$n0"/>
     <xsl:with-param name="n" select="number($n)-1"/>
     <xsl:with-param name="s0" select="$s"/>
    </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$s0"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

[此贴子已经被作者于2005-12-12 19:22:22编辑过]

--  作者:glueckschwein
--  发布时间:12/13/2005 3:54:00 AM

--  
感谢斑竹支持~~!!!
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
125.000ms