Search code examples
pythonxmlxsltxml-signature

Removing Signature from xml


I would like to discard signature element from my xml files. So I use xslt for filtering some elements and tags from my xml files. I use xslt with python. Xslt looks like the following:

xslt_root = etree.XML('''\
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

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


                <xsl:template match="TimeStamp"/>
                <xsl:template match="@timeStamp"/>
                <xsl:template match="TimeStamps"/>
                <xsl:template match="Signature"/>

                </xsl:stylesheet>
                ''')

The problem is that when I save the result(updated) xml files, all elements and tags which I have defined in the xslt rule will be discarded except "Signature" element which remains. Is there a possible way to discard this signature from xml file?


Solution

  • If your Signature element has a namespace, for example:

      <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">...</Signature>
    

    Then you'll need to adapt your XSLT to match it with the namespace:

    <xsl:stylesheet version="1.0"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:s="http://www.w3.org/2000/09/xmldsig#"> <!-- CHANGE #1 -->
    
      <xsl:template match="node() | @*">
        <xsl:copy>
          <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="TimeStamp"/>
      <xsl:template match="@timeStamp"/>
      <xsl:template match="TimeStamps"/>
      <xsl:template match="s:Signature"/>  <!-- CHANGE #2 -->
    
    </xsl:stylesheet>