2012-07-31 36 views
2

我必須從整個XML文件中刪除特定屬性,同時使用XSLT將其轉換爲其他XML。我必須從整個文檔中刪除'onclick'事件,只要它發生。使用XSLT從整個XML文件中刪除屬性

輸入文件:

<?xml version="1.0" encoding="utf-8"?> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
    </head> 
    <body> 
     <div class="iDev"> 
     <div class="q"> 
      T <input type="radio" name="o0" id="t0" onclick="getFeedback()"/> 
     </div> 
     <div class="q"> 
      T <input type="radio" name="o1" id="t1" onclick="getFeedback()" /> 
     </div> 
     </div> 
    </body> 
    </html> 

我的XSLT: 我嘗試以下方法來消除這種屬性(身份模板後):在某些情況下,它去掉了

<xsl:template match="xhtml:body//xhtml:input/@onclick /> 

'onclick'事件,但當我通過一個模板更改'name'和'id'屬性的值並在輸入標籤內添加一個屬性時,這個'onclick'事件保持原樣。 請幫我解決這個問題。 感謝你!

回答

3

該轉化

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:x="http://www.w3.org/1999/xhtml" 
xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="x"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="@onclick"/> 

<xsl:template match="x:input"> 
    <xsl:element name="input" namespace="http://www.w3.org/1999/xhtml"> 
    <xsl:attribute name="name"> 
    <xsl:value-of select="concat('n',substring(@name,2))"/> 
    </xsl:attribute> 
    <xsl:attribute name="id"> 
    <xsl:value-of select="concat('i',substring(@id,2))"/> 
    </xsl:attribute> 
    <xsl:attribute name="someNew">newVal</xsl:attribute> 
    <xsl:apply-templates select= 
    "@*[not(name()='name' or name()='id')] | node()"/> 
    </xsl:element> 
</xsl:template> 
</xsl:stylesheet> 

當所提供的XML文檔應用:

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
    </head> 
    <body> 
     <div class="iDev"> 
      <div class="q">   T 
       <input type="radio" name="o0" id="t0" onclick="getFeedback()"/> 
      </div> 
      <div class="q">   T 
       <input type="radio" name="o1" id="t1" onclick="getFeedback()" /> 
      </div> 
     </div> 
    </body> 
</html> 

改變nameid屬性,並增加了一個新的屬性到各個input元件。它還「刪除」onclick屬性

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-type" content="text/html; charset=utf-8"/> 
    </head> 
    <body> 
     <div class="iDev"> 
     <div class="q">   T 
       <input name="n0" id="i0" someNew="newVal" type="radio"/> 
     </div> 
     <div class="q">   T 
       <input name="n1" id="i1" someNew="newVal" type="radio"/> 
     </div> 
     </div> 
    </body> 
</html> 
+0

非常感謝您的詳細解答。 :) – RahulD 2012-08-01 05:22:00

+0

@rahuldwivedi:不客氣。 – 2012-08-01 05:32:05