gpt4 book ai didi

python - 从 xml 中删除签名

转载 作者:行者123 更新时间:2023-12-01 05:04:53 26 4
gpt4 key购买 nike

我想从我的 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 规则中定义的所有元素和标签都将被丢弃,但保留的“Signature”元素除外。有没有可能从 xml 文件中丢弃此签名?

最佳答案

如果您的 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>

关于python - 从 xml 中删除签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25246184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com