2017-06-20 58 views
1

我需要刪除的命名空間,並在下面的響應過濾掉所有GetShardHoldersResponseRecords並刪除命名空間:執行多個XSLT轉換,每個修改前一個轉換的結果

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://schemas.blabla.com/service/ShardServiceResponse/1.0" xmlns:ns1="http://schemas.blabla.com/TYPELIB/1.0"> 
    <soapenv:Header/> 
    <soapenv:Body> 
    <GetShardHoldersResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tl="http://schemas.blabla.com/TYPELIB/1.0" xmlns:ns1="http://schemas.blabla.com/service/ShardServiceResponse/1.0"> 
     <ns1:Header> 
      <tl:MsgId>myId</tl:MsgId> 
     </ns1:Header> 
     <ns1:GetShardHoldersResponseRecord> 
      <ns1:ShardHolder> 
       <tl:ShardHolderNo>5443137000478556</tl:ShardHolderNo> 
       <tl:Shard> 
        <tl:ShardNo>5443132000143888</tl:ShardNo> 
       </tl:Shard> 
       <tl:Shard> 
        <tl:ShardNo>5443132000143889</tl:ShardNo> 
       </tl:Shard> 
      </ns1:ShardHolder> 
     </ns1:GetShardHoldersResponseRecord> 
     </GetShardHoldersResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

我想要的結果僅包含responseRecords並與命名空間中刪除:

<GetShardHoldersResponseRecord> 
      <ShardHolder> 
       <ShardHolderNo>5443137000478556</ShardHolderNo> 
       <Shard> 
        <ShardNo>5443132000143888</ShardNo> 
       </Shard> 
       <Shard> 
        <ShardNo>5443132000143889</ShardNo> 
       </Shard> 
      </ShardHolder> 
     </GetShardHoldersResponseRecord> 
     </GetShardHoldersResponse> 

要刪除的命名空間,我知道我們可以:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:tl="http://schemas.blabla.com/TYPELIB/1.0" xmlns:ns1="http://schemas.blabla.com/service/ShardServiceResponse/1.0" > 

<xsl:output omit-xml-declaration="yes" indent="yes"/> 



<!-- remove namespaces --> 
<xsl:template match="*"> 
      <xsl:element name="{local-name()}" > 
     <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

</xsl:stylesheet> 

要篩選出GetShardHoldersResponse,我們可以使用這樣的:

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

但我似乎不能後對方兩個變換相結合。 如何將兩個變形像這樣彼此結合起來?

我一直用這個來測試XSL:

http://www.utilities-online.info/xsltransformation/#.WUjXjxOGNN1

回答

1

這應該做你想要什麼:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns1="http://schemas.blabla.com/service/ShardServiceResponse/1.0"> 

    <!-- 
    Omit these elements from the output, but apply the templates that match 
    the child elements of the current element. 
    --> 
    <xsl:template match="soapenv:Envelope | soapenv:Body"> 
    <xsl:apply-templates/> 
    </xsl:template> 

    <!-- Omit these elements and their children from the output. --> 
    <xsl:template match="soapenv:Header | ns1:Header"/> 

    <!-- 
    For every other element, create a new element with the same local name 
    in the empty namespace. Then apply the templates that match the child 
    nodes of the current element. 
    --> 
    <xsl:template match="*"> 
    <xsl:element name="{local-name()}"> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:element> 
    </xsl:template> 

    <!-- 
    For every attribute node `@*` and text node `text()`, copy them into the 
    output as is. 
    --> 
    <xsl:template match="@* | text()"> 
    <xsl:copy/> 
    </xsl:template> 

</xsl:stylesheet>