简体   繁体   English

将名称空间和前缀添加到xml

[英]add namespace and prefix to xml

I am using camel to route messages to a Webservice. 我正在使用骆驼将消息路由到Web服务。 The Messages are like XML but without namespaces/prefixes. 消息类似于XML,但没有名称空间/前缀。 The problem now is that the Webservice expects the XML but with the appropriate namespaces for each element. 现在的问题是,Web服务需要XML,但每个元素都具有适当的命名空间。 So as an example: 举个例子:

<a>
  <b>value_b</b>
  <c>value_c</c>
</a>

is what im getting in, but what needs to be sent out should look like this 我正在进入,但需要发送的内容应如下所示

<a  xmlns:n1="http://yadda-ns1.com" xmlns:n2="http://yadda-ns2.com">
  <ns1:b>value_b</ns1:b>
  <ns2:c>value_c</ns2:c>
</a>

if it was the same namespace on all elements i would have just used an xslt to add it. 如果它是所有元素上的相同名称空间,我将只使用xslt来添加它。 But its mostly 2 or 3 different namespaces. 但是它主要是2或3个不同的名称空间。

Now is it possible to add the namespaces in my camel route? 现在可以在我的骆驼路线中添加名称空间了吗? I had the idea to use jaxb to marshal from the "incomplete" XML to the "complete" one (with XML), would this work? 我的想法是使用jaxb将“不完整”的XML编组为“完整”的XML(使用XML),这行得通吗? I was trying this but did not succeed yet. 我正在尝试此操作,但尚未成功。

Or does someone have a different idea? 还是有人有不同的想法? What i also have in my project is the XSDs and JAXB Annotated Classes so these can also be used and the messages are identical apart from the missing namespace. 我在项目中还拥有XSD和JAXB注释类,因此它们也可以使用,并且消息与缺少的命名空间相同。

Best Regards 最好的祝福

Thomas 托马斯

You could transform the XML with a stylesheet such as the one below to modify the elements to be bound to the appropriate namespaces: 您可以使用下面的样式表来转换XML,以修改要绑定到适当名称空间的元素:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:ns1="http:yadayada-ns1.com"
    xmlns:ns2="http:yadayada-ns2.com">
    <xsl:output indent="yes"/>

    <!-- identity template that copies content(unless more specific templates match) -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- Make the "a" elements in the ns1 namespace -->
    <xsl:template match="a">
        <xsl:element name="ns1:{local-name()}">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>

    <!-- Make the "b" and "c" elements in the ns2 namespace -->
    <xsl:template match="b|c">
        <xsl:element name="ns2:{local-name()}">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM