2016-06-09 42 views
0

我有以下XML:复制输入XML输出并删除不需要的命名空间出来的

<soap:Envelope 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ns="http:test/201/2" 
xmlns:m0="http:test/201/3" 
xmlns:ns0="http:test/201/4" 
xmlns:ns2="http:test/201/5" 
xmlns:ns1="http:test/201/6" 
xmlns:ns3="http:test/201/7" 
xmlns:ns6="http:test/201/8" 
xmlns:ns4="http:test/201/9" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<soap:Header> 
<ns0:ResponseHeader> 
<ns:Env>Dev</ns:Env> 
<ns:Version>1</ns:Version> 
<ns:Server></ns:Server> 
<ns:Name>NAME</ns:Name> 
</ns0:ResponseHeader> 
</soap:Header> 
<soap:Body> 
<ns2:ResponseData> 
<ns2:Employee > 
<ns2:MessageList xsi:type="ns3:Info"> 
<ns2:Message> 
<ns4:Type>new</ns4:Type> 
<ns4:Code>2</ns4:Code> 
<ns4:Source>Emp</ns4:Source> 
<ns4:Description>new hire</ns4:Description> 
</ns2:Message> 
</ns2:MessageList> 
<ns2:MessageList xsi:type="ns3:Info"> 
<ns2:Message> 
<ns4:Type>new</ns4:Type> 
<ns4:Code>1</ns4:Code> 
<ns4:Source>contract</ns4:Source> 
<ns4:Description>new hire</ns4:Description> 
</ns2:Message> 
</ns2:MessageList> 
</ns2:Employee> 
</ns2:ResponseData> 
</soap:Body> 
</soap:Envelope> 

我的要求是,拷贝完整的输入XML元素和属性来输出XML包括除的xmlns所有命名空间:SOAP =” http://schemas.xmlsoap.org/soap/envelope/”。 所以欲望输出是:

我的要求是,复制完整的输入xml元素和属性以输出xml包括除xmlns之外的所有命名空间:soap =“http://schemas.xmlsoap.org/soap/envelope/”。 愿意的话输出是:

<Envelope 
xmlns:ns="http:test/201/2" 
xmlns:m0="http:test/201/3" 
xmlns:ns0="http:test/201/4" 
xmlns:ns2="http:test/201/5" 
xmlns:ns1="http:test/201/6" 
xmlns:ns3="http:test/201/7" 
xmlns:ns6="http:test/201/8" 
xmlns:ns4="http:test/201/9" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<Header> 
<ns0:ResponseHeader> 
<ns:Env>Dev</ns:Env> 
<ns:Version>1</ns:Version> 
<ns:Server></ns:Server> 
<ns:Name>NAME</ns:Name> 
</ns0:ResponseHeader> 
</Header> 
<Body> 
<ns2:ResponseData> 
<ns2:Employee > 
<ns2:MessageList xsi:type="ns3:Info"> 
<ns2:Message> 
<ns4:Type>new</ns4:Type> 
<ns4:Code>2</ns4:Code> 
<ns4:Source>Emp</ns4:Source> 
<ns4:Description>new hire</ns4:Description> 
</ns2:Message> 
</ns2:MessageList> 
<ns2:MessageList xsi:type="ns3:Info"> 
<ns2:Message> 
<ns4:Type>new</ns4:Type> 
<ns4:Code>1</ns4:Code> 
<ns4:Source>contract</ns4:Source> 
<ns4:Description>new hire</ns4:Description> 
</ns2:Message> 
</ns2:MessageList> 
</ns2:Employee> 
</ns2:ResponseData> 
</Body> 
</Envelope> 
+0

你的输出包含'肥皂:Envelope'所以它需要有一些'的xmlns:SOAP = “...”'声明。 –

+0

我的不好。肥皂前缀元素将被本地名称替换。所以这就是为什么它不是必需的。所以最终的愿望输出是。 – Mike

+0

请编辑您的问题的代码示例以反映所需的输出。看来你也已经用一个可接受的解决方案发布了一个类似的问题,这个问题不同吗?如果是这样,为什么? –

回答

0

如果你想改变所有soap:foo元素foo元素没有命名空间,并删除soap命名空间,然后使用

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    exclude-result-prefixes="xs soap" 
    version="2.0"> 

    <xsl:template match="@* | node()"> 
     <xsl:copy copy-namespaces="no"> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="soap:*"> 
     <xsl:element name="{local-name()}"> 
      <xsl:copy-of select="namespace::*[not(. = namespace-uri(current()))]"/> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:element> 
    </xsl:template> 

</xsl:stylesheet> 

应该足够了,假设你的根元素是soap命名空间并在范围内包含所有名称空间声明。

当我在帖子中应用上面XSLT将输入样本

<?xml version="1.0" encoding="UTF-8"?> 
<soap:Envelope 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns="http:test/201/2" 
    xmlns:m0="http:test/201/3" 
    xmlns:ns0="http:test/201/4" 
    xmlns:ns2="http:test/201/5" 
    xmlns:ns1="http:test/201/6" 
    xmlns:ns3="http:test/201/7" 
    xmlns:ns6="http:test/201/8" 
    xmlns:ns4="http:test/201/9" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soap:Header> 
     <ns0:ResponseHeader> 
      <ns:Env>Dev</ns:Env> 
      <ns:Version>1</ns:Version> 
      <ns:Server></ns:Server> 
      <ns:Name>NAME</ns:Name> 
     </ns0:ResponseHeader> 
    </soap:Header> 
    <soap:Body> 
     <ns2:ResponseData> 
      <ns2:Employee > 
       <ns2:MessageList xsi:type="ns3:Info"> 
        <ns2:Message> 
         <ns4:Type>new</ns4:Type> 
         <ns4:Code>2</ns4:Code> 
         <ns4:Source>Emp</ns4:Source> 
         <ns4:Description>new hire</ns4:Description> 
        </ns2:Message> 
       </ns2:MessageList> 
       <ns2:MessageList xsi:type="ns3:Info"> 
        <ns2:Message> 
         <ns4:Type>new</ns4:Type> 
         <ns4:Code>1</ns4:Code> 
         <ns4:Source>contract</ns4:Source> 
         <ns4:Description>new hire</ns4:Description> 
        </ns2:Message> 
       </ns2:MessageList> 
      </ns2:Employee> 
     </ns2:ResponseData> 
    </soap:Body> 
</soap:Envelope> 

然后撒克逊9.6创建结果

<?xml version="1.0" encoding="UTF-8"?><Envelope xmlns:ns="http:test/201/2" xmlns:m0="http:test/201/3" xmlns:ns0="http:test/201/4" xmlns:ns2="http:test/201/5" xmlns:ns1="http:test/201/6" xmlns:ns3="http:test/201/7" xmlns:ns6="http:test/201/8" xmlns:ns4="http:test/201/9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <Header> 
     <ns0:ResponseHeader> 
      <ns:Env>Dev</ns:Env> 
      <ns:Version>1</ns:Version> 
      <ns:Server/> 
      <ns:Name>NAME</ns:Name> 
     </ns0:ResponseHeader> 
    </Header> 
    <Body> 
     <ns2:ResponseData> 
      <ns2:Employee> 
       <ns2:MessageList xsi:type="ns3:Info"> 
        <ns2:Message> 
         <ns4:Type>new</ns4:Type> 
         <ns4:Code>2</ns4:Code> 
         <ns4:Source>Emp</ns4:Source> 
         <ns4:Description>new hire</ns4:Description> 
        </ns2:Message> 
       </ns2:MessageList> 
       <ns2:MessageList xsi:type="ns3:Info"> 
        <ns2:Message> 
         <ns4:Type>new</ns4:Type> 
         <ns4:Code>1</ns4:Code> 
         <ns4:Source>contract</ns4:Source> 
         <ns4:Description>new hire</ns4:Description> 
        </ns2:Message> 
       </ns2:MessageList> 
      </ns2:Employee> 
     </ns2:ResponseData> 
    </Body> 
</Envelope> 
+0

以上xslt工作良好,期望有soap名称空间属性复制soap前缀元素的子元素。

> – Mike

+0

我已经测试了针对您发布的输入示例的代码,并将结果包含在答案中,没有'xmlns:soap'声明,因此您必须详细告诉我们您使用的XSLT处理器如果你真的还有问题。 –

+0

我在您发布的datapower aboue xslt代码中使用。 – Mike

0

的DataPower只实现XSLT 1.0,而不是2.0。

您可以通过复制控制你的命名空间:

<xsl:copy> 
    <xsl:element name="ns:Element" namespace="http://www.xml.com/ns"> 
</xsl:copy> 
相关问题