我正在将一个 XML 转换为 wso2esb 中的另一个 XML(使用 saxon)。在那里我有以下输入示例:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<root>
<MyElement>content</MyElement>
</root>
</soapenv:Body>
</soapenv:Envelope>
我的问题是现在我的新内容
<NEW>
不应获得任何命名空间。但是我在 XSLT 之后得到以下输出:<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<root>
<NEW xmlns="http://ws.apache.org/ns/synapse">content</NEW>
</root>
</soapenv:Body>
</soapenv:Envelope>
我不要
xmlns="http://ws.apache.org/ns/synapse"
新元素中的声明 .当我使用 oXygen 进行测试时,它确实有效,但是当我在 wso2esb 中运行它时,我得到了我猜的默认命名空间“http://ws.apache.org/ns/synapse”。所以我尝试了 3 种不同的方法来创建新元素,并且
<xsl:stylesheet xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:avintis="http://www.avintis.com/esb" extension-element-prefixes="avintis" version="2.0" exclude-result-prefixes="#all" xpath-default-namespace="">
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/soapenv:Envelope|soapenv:Body">
<xsl:copy>
<xsl:apply-templates select="*|text()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<!-- FIRST METHOD - THE METHOD I want to use! this will receive the xmlns="http://ws.apache.org/ns/synapse" -->
<NEW>
<xsl:value-of select="MyElement"/>
</NEW>
<!-- Second methodworks - but I need to add xmlns="" to every element and there are a lot-->
<NEW xmlns="">
<xsl:value-of select="MyElement"/>
</NEW>
<!-- Third method works: But not very readable - I would prefer the first method -->
<xsl:element name="NEW">
<xsl:value-of select="MyElement"/>
</xsl:element>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
使用第一种创建元素的方法时,有没有办法删除此 xmlns 属性 ?
谢谢
最佳答案
问题不在于序列化输出中存在不需要的命名空间声明;问题是 NEW 元素位于错误的命名空间中。您需要考虑元素和属性的(扩展)名称是什么,命名空间声明将自行处理。
我强烈怀疑您正在执行的代码与您向我们展示的不同。我认为它必须在 <NEW>
的某些祖先上具有默认 namespace 声明 xmlns="http://ws.apache.org/ns/synapse"文字结果元素,可能在 xsl:stylesheet 元素本身上。如果不是这种情况,那么 wso2esb 在运行代码时会做一些非常奇怪的事情。
关于XSLT 创建没有 namespace 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22863045/