【问题标题】:Removing Signature from xml从 xml 中删除签名
【发布时间】:2014-08-11 14:52:22
【问题描述】:

我想从我的 xml 文件中丢弃签名元素。所以我使用 xslt 从我的 xml 文件中过滤一些元素和标签。我将 xslt 与 python 一起使用。 Xslt 如下所示:

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

问题是当我保存结果(更新的)xml 文件时,我在 xslt 规则中定义的所有元素和标签都将被丢弃,除了保留的“签名”元素。有没有办法从 xml 文件中丢弃这个签名?

【问题讨论】:

  • 请发布您的源 XML - 连同您获得的 XML 输出与您的预期相比。否则我们将无法为您提供帮助。谢谢!

标签: python xml xslt xml-signature


【解决方案1】:

如果你的Signature 元素有命名空间,例如:

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

然后您需要调整您的 XSLT 以使其与命名空间相匹配:

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

【讨论】:

  • 好主意,但它不起作用..我添加了你提到的命名空间。
猜你喜欢
  • 2016-07-31
  • 1970-01-01
  • 2021-04-26
  • 2018-12-21
  • 2023-03-06
  • 2019-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多