XSLT如何从div和td标签中删除样式

时间:2015-06-22 10:41:12

标签: xml xslt

我是XSLT的新手。从div,td或li标签中删除样式属性时卡住了。

输入XML:

<?xml version="1.0" encoding="UTF-8"?>
    <div xmlns="http://www.w3.org/1999/xhtml">
                <table style="BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; WIDTH: 606px; BORDER-COLLAPSE: collapse; WORD-WRAP: break-word; TABLE-LAYOUT: fixed; BORDER-TOP: medium none; BORDER-RIGHT: medium none"
                       class="MsoNormalTable msoUcTable"                    tabIndex="-1" border="1" cellPadding="0" width="606">
                   <colgroup>
                      <col style="WIDTH: 189px"/>
                      <col style="WIDTH: 417px"/>
                   </colgroup>
                   <tbody>
                      <tr>
                         <td bgColor="#cccccc"
                             style="BORDER-BOTTOM: black 1pt solid; BORDER-LEFT: black 1pt solid; BORDER-TOP: black 1pt solid; BORDER-RIGHT: black 1pt solid">
                            <div style="MARGIN-TOP: 0in; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; MARGIN-BOTTOM: 0pt; FONT-SIZE: 12pt">
                               <span style="FONT-SIZE: 10pt">
                                  <strong>
                                     <font face="Arial"> 1st data</font>
                                  </strong>
                               </span>
                               <span style="FONT-SIZE: 1pt"/>
                            </div>
                         </td>
                         <td bgColor="#cccccc"
                             style="BORDER-BOTTOM: black 1pt solid; BORDER-LEFT: black 1pt solid; BORDER-TOP: black 1pt solid; BORDER-RIGHT: black 1pt solid">
                            <div style="MARGIN-TOP: 0in; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; MARGIN-BOTTOM: 0pt; FONT-SIZE: 12pt">
                               <span style="FONT-SIZE: 10pt">
                                  <strong>
                                     <font face="Arial">2nd data</font>
                                  </strong>
                               </span>
                               <span style="FONT-SIZE: 1pt"/>
                            </div>
                         </td>
                      </tr>
                   </tbody>
                   <tbody>
                      <tr>
                         <td style="BORDER-BOTTOM: black 1pt solid; BORDER-LEFT: black 1pt solid; BORDER-TOP: black 1pt solid; BORDER-RIGHT: black 1pt solid">
                            <div style="MARGIN-TOP: 0in; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; MARGIN-BOTTOM: 0pt; FONT-SIZE: 12pt">
                               <span style="FONT-SIZE: 10pt">
                                  <strong>
                                     <font face="Arial">3rd data</font>
                                  </strong>
                               </span>
                               <span style="FONT-SIZE: 1pt"/>
                            </div>
                         </td>
                         <td style="BORDER-BOTTOM: black 1pt solid; BORDER-LEFT: black 1pt solid; BORDER-TOP: black 1pt solid; BORDER-RIGHT: black 1pt solid">
                            <div style="MARGIN-TOP: 0in; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; MARGIN-BOTTOM: 0pt; FONT-SIZE: 12pt">
                               <span style="FONT-SIZE: 10pt">
                                  <font face="Arial">4th data.</font>
                               </span>
                            </div>
                         </td>
                      </tr>
                   </tbody>
                </table>
             </div>

并输出应为:

的Output.xml

<div xmlns="http://www.w3.org/1999/xhtml">
    <span>
        <table tabIndex="-1" class="MsoNormalTable msoUcTable" style="BORDER-TOP-STYLE: none; WORD-WRAP: break-word; WIDTH: 453.75pt; BORDER-COLLAPSE: collapse; TABLE-LAYOUT: fixed; BORDER-BOTTOM-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none" borderColor="#000000" cellPadding="0" width="606" border="1">
            <colgroup>
                <col style="WIDTH: 163px"/>
                <col style="WIDTH: 442px"/>
            </colgroup>
            <tbody>
                <tr>
                    <td>
                        <font size="2" face="Arial">
                            <div>
                                <font size="2" face="Arial">
                                    <strong>1st data </strong>
                                </font>
                            </div>
                        </font>
                    </td>
                    <td>
                        <font size="2" face="Arial">
                            <div>
                                <font size="2" face="Arial">
                                    <strong>2nd data </strong>
                                </font>
                            </div>
                        </font>
                    </td>
                </tr>
                <tr>
                    <td>
                        <font size="2" face="Arial">
                            <div>
                                <font size="2" face="Arial">
                                    <strong>3rd data</strong>
                                </font>
                            </div>
                        </font>
                    </td>
                    <td>
                        <font size="2" face="Arial">
                            <div>
                                <font size="2" face="Arial">4th data </font>
                            </div>
                        </font>
                    </td>
                </tr>
            </tbody>
        </table>
    </span>
</div> 

我正在使用XSL,现在需要从div,li,span和其他标记中删除style属性:<xsl:template match=" div | li | span | th | col | td "> <xsl:element name="{local-name()}" namespace="http://www.w3.org/1999/xhtml"> <xsl:copy-of select="@*" /> <xsl:apply-templates select="@*|node()"/> </xsl:element> </xsl:template>

提前致谢。 高塔姆

1 个答案:

答案 0 :(得分:1)

要删除一些以身份转换模板

开头的节点
<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

然后为要删除的节点添加一个空模板:

<xsl:template xmlns:xhtml="http://www.w3.org/1999/xhtml" 
   match="xhtml:div/@style | 
          xhtml:li/@style | 
          xhtml:td/@style | 
          xhtml:span/@style"/>