【问题标题】:How to remove namespace from xml如何从 xml 中删除命名空间
【发布时间】:2012-08-30 16:43:20
【问题描述】:

我有一个以下格式的 XML

<Body xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://schemas.xmlsoap.org/soap/envelope/">
  <TransactionAcknowledgement xmlns="">
    <TransactionId>HELLO </TransactionId>
    <UserId>MC</UserId>
    <SendingPartyType>SE</SendingPartyType>
  </TransactionAcknowledgement>
</Body>

我想为它使用 XQuery 或 XPath 表达式。

现在我只想删除

xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"

来自 xml 的命名空间。

有什么方法可以实现吗?

谢谢

【问题讨论】:

  • XPath 是一种基于 XML 文档 (a) 的 查询 语言,因此,对 XPath 表达式的求值永远不会修改表达式所针对的源 XML 文档被评估。您需要的是 XML 转换。最适合 XML 转换的语言是 XSLT(也可以使用 XQuery,但由于缺少模板匹配,因此非常笨拙)。您对 XSLT 解决方案感兴趣吗?

标签: xpath xquery osb


【解决方案1】:

尝试使用functx:change-element-ns-deep:

let $xml := <Body xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://schemas.xmlsoap.org/soap/envelope/">
  <TransactionAcknowledgement xmlns="">
    <TransactionId>HELLO </TransactionId>
    <UserId>MC</UserId>
    <SendingPartyType>SE</SendingPartyType>
  </TransactionAcknowledgement>
</Body>

return functx:change-element-ns-deep($xml, "http://schemas.xmlsoap.org/soap/envelope/", "")

但是正如 Dimitre Novatchev 所说,这个函数不会改变源 xml 的命名空间,它会创建一个新的 XML。

【讨论】: