2016-05-30 9 views
1

カスタムxmlにSOAPメッセージをコピーしようとしています(Envelope、Header、BodyからSoap名前空間を削除し、ResponseHeader要素から名前空間接頭辞を削除しています。そして、ResponseData要素xsltを使用してxmlnからsoap属性を削除する

入力:。

<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:Name>NAME</ns:Name> 
</ns0:ResponseHeader> 
</soap:Header> 
<soap:Body> 
<ns2:ResponseData> 
<ns2:Employee /> 
<ns2:MessageList> 
<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:Employee> 
</ns2:ResponseData> 
</soap:Body> 
</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> 
<ResponseHeader> 
<ns:Env>Dev</ns:Env> 
<ns:Version>1</ns:Version> 
<ns:Server> 
<ns:Name>NAME</ns:Name> 
</ResponseHeader> 
</Header> 
<Body> 
<ns2:ResponseData> 
<ns2:Employee /> 
<ns2:MessageList> 
<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:Employee> 
</ns2:ResponseData> 
</Body> 
</Envelope> 

マイXSLT:

<xsl:stylesheet version="2.0" extension-element-prefixes="dp" exclude-result-prefixes="soap ns3 snss ns0 ns1 ns4 #default" 
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" 
> 
    <xsl:output method="xml" version="1.0" indent="no"/> 
    <xsl:template match="node()|@*" exclude-result-prefixes="#all"> 

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

<xsl:template match="soap:*" exclude-result-prefixes="#all"> 
    <xsl:element name="{local-name()}"> 
    <xsl:copy-of select="namespace::*[not(. = namespace-uri(..))]"/> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:element> 
</xsl:template> 
    <xsl:template match="tns:ResponseData" exclude-result-prefixes="#all"> 
<ResponseHeader> 
    <xsl:copy-of select="namespace::*[not(. = namespace-uri(..))]"/> 
    <xsl:apply-templates select="node()|@*"/> 
    </ResponseHeader> 
</xsl:template> 

</xsl:stylesheet> 

取得出力:石鹸属性:

<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> 
<ResponseHeader xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<ns:Env>Dev</ns:Env> 
<ns:Version>1</ns:Version> 
<ns:Server> 
<ns:Name>NAME</ns:Name> 
</ResponseHeader> 
</Header> 
<Body> 
<ns2:ResponseData xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<ns2:Employee /> 
<ns2:MessageList> 
<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:Employee> 
</ns2:ResponseData> 
</Body> 
</Envelope> 

場合のxmlnsを取り除くためにどのように助けてください?

+0

**整形された**入出力を投稿してください。 –

答えて

0

この溶液は、XSLTのすべてのバージョンで動作: 1.0、2.0及び3.0(ともはるかに短い):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:x="http:test/201/4"> 
<xsl:output omit-xml-declaration="yes"/> 

    <xsl:template match="*" priority="1"> 
    <xsl:element name="{name()}" namespace="{namespace-uri()}"> 
     <xsl:copy-of select="namespace::*[not(name()='soap')]"/> 
     <xsl:apply-templates/> 
    </xsl:element> 
    </xsl:template> 

    <xsl:template match="soap:* | x:ResponseHeader" priority="2"> 
    <xsl:element name="{local-name()}"> 
     <xsl:copy-of select="namespace::*[not(name()='soap')]"/> 
     <xsl:apply-templates/> 
    </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

この変換は、(整形式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:Name>NAME</ns:Name> 
     </ns0:ResponseHeader> 
    </soap:Header> 
    <soap:Body> 
     <ns2:ResponseData> 
     <ns2:Employee> 
      <ns2:MessageList> 
       <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> 

希望、正しい結果が製造される:

<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> 
     <ResponseHeader> 
      <ns:Env>Dev</ns:Env> 
      <ns:Version>1</ns:Version> 
      <ns:Server/> 
      <ns:Name>NAME</ns:Name> 
     </ResponseHeader> 
    </Header> 
    <Body> 
     <ns2:ResponseData> 
      <ns2:Employee> 
       <ns2:MessageList> 
        <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> 
+1

'* [local-name()= 'ResponseHeader']' =悪い習慣。名前空間は無視しないで使用する必要があります。 –

+0

@ michael.hor257k、これは一般的に賢明なフィードバックです。私は 'ResponseHeader'が常にその名前空間にあるとは確信していませんが、これが文字通り結果の要素が名前空間にないという要件を実装した理由です。言い換えれば、この表現には独自のユースケースがあり、私は現在の問題がこのようなユースケースを提示していると信じています。私があなたのフィードバックを反映させることがソリューションの一般性を低下させると信じているという事実にかかわらず、私はそれだけでした。他のコメント? –

0

あなたの入力も出力も整形式のXMLではありません。あなたの "XSLT"はXSLT文書でもありません!以下の整形式XML入力にこれを適用すると2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ns0="http:test/201/4" 
exclude-result-prefixes="soap"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="*"> 
    <xsl:copy copy-namespaces="no"> 
     <xsl:apply-templates/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="soap:* | ns0:ResponseHeader"> 
    <xsl:element name="{local-name()}"> 
     <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="/soap:Envelope"> 
    <Envelope> 
     <xsl:copy-of select="namespace::*"/> 
     <xsl:apply-templates/> 
    </Envelope> 
</xsl:template> 

</xsl:stylesheet> 

XSLT:

<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:Name>NAME</ns:Name> 
     </ns0:ResponseHeader> 
    </soap:Header> 
    <soap:Body> 
     <ns2:ResponseData> 
     <ns2:Employee> 
      <ns2:MessageList> 
       <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> 

が返されます:

AFAICTは、あなたがやりたいです

<?xml version="1.0" encoding="UTF-8"?> 
<Envelope xmlns:ns0="http:test/201/4" xmlns:ns="http:test/201/2" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:m0="http:test/201/3" 
      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
      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"> 
    <Header> 
     <ResponseHeader> 
     <ns:Env>Dev</ns:Env> 
     <ns:Version>1</ns:Version> 
     <ns:Server/> 
     <ns:Name>NAME</ns:Name> 
     </ResponseHeader> 
    </Header> 
    <Body> 
     <ns2:ResponseData> 
     <ns2:Employee> 
      <ns2:MessageList> 
       <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> 

最終的なテンプレート/soap:Envelopeは、美容上の理由から含まれています。削除すると、結果は次のようになります。

これは以前の結果と意味的に同じです。

+0

ありがとうございます。本当に助かります。私は入力を少し変更しました。同じ出力が期待されます: – Mike

+0

私の最初の質問に答えました。私は入力と答えを待つために小さな変更を加えました。 – Mike

+0

@マイケル私は何の変化も見ません。いずれにせよ、それが小さな変化であれば、自分で調整することができます。それ以外の場合は新しい質問を投稿してください。 –

関連する問題