XSLT 创建没有 namespace 的内容

标签 xslt namespaces wso2esb

我正在将一个 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/

相关文章:

php - xml 错误 xsl 样式表

xml - 使用 xslt 在现有 xml 文件中创建新元素

python - 在局部函数范围内使用的全局语句

logging - WSO2 ESB 中的跟踪日志文件

wso2 - 当实际端点关闭时如何避免 wso2 esb 中的端点暂停

html - Xslt 生成 Html <IMG> 标签。如何使用 XML 节点值作为 <IMG> 标签的 src 属性

c# - 从 XSLT 中的变量集输出回车

ruby-on-rails - 命名空间修改后表单损坏

C++ TR1 正则表达式不可用

WSO2 Greg 和 ESB 集成示例