【问题标题】:使用 XSLT 从整个 XML 文件中删除属性
【发布时间】:2012-07-29 19:59:59
【问题描述】:

在使用 XSLT 将其转换为其他 XML 时,我必须从整个 XML 文件中删除特定属性。无论何时发生,我都必须从整个文档中删除“onclick”事件。

输入文件:

<?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' 事件仍然保持原样是。 请帮我解决这个问题。 谢谢!

【问题讨论】:

    标签: xml xslt xhtml xslt-1.0


    【解决方案1】:

    这种转变

    <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>
    

    【讨论】:

    • 非常感谢您的详细回答。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多