2013-02-19 28 views
0

我有這個源:XSLT移動空間聲明從小孩到父元素

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soapenv:Body> 
    <sc:GetFdrRewardsResponse xmlns:sc="http://somecompany.com/soa/cardaccount1.1/schema" 
    xmlns:sc_1="http://somecompany.com/soa/common1.1/schema" 
    xmlns:sc_2="http://somecompany.com/soa/common/schema" 
    xmlns:sc_3="http://somecompany.com/soa/cardaccount/schema"> 
     <sc_1:ResponseCode>0000</sc_1:ResponseCode> 
     <sc_1:ResponseMessage>Successful Execution</sc_1:ResponseMessage> 
     <sc_1:CorrelationId>1234</sc_1:CorrelationId> 
     <sc:FdrRewards> 
     <sc_2:BankNumber>0175</sc_2:BankNumber> 
     <sc:IsRewardsMember>true</sc:IsRewardsMember> 
     <sc:IsEligibleToRedeem>true</sc:IsEligibleToRedeem> 
     <sc_3:AmazingRewardsBalance>10442.00</sc_3:AmazingRewardsBalance> 
     <sc_3:CashBackSavingsBalance>0.00</sc_3:CashBackSavingsBalance> 
     </sc:FdrRewards> 
    </sc:GetFdrRewardsResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

而且需要這樣的結果:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soapenv:Body> 
    <sc_3:GetFdrRewardsResponse 
     xmlns:sc_1="http://somecompany.com/soa/common1.1/schema" 
     xmlns:sc_2="http://somecompany.com/soa/common/schema" 
     xmlns:sc_3="http://somecompany.com/soa/cardaccount/schema" > 
     <sc_1:ResponseCode>0000</sc_1:ResponseCode> 
     <sc_1:ResponseMessage >Successful Execution</sc_1:ResponseMessage> 
     <sc_1:CorrelationId >1234</sc_1:CorrelationId> 
     <sc_3:FdrRewards> 
     <sc_2:BankNumber >0175</sc_2:BankNumber> 
     <sc_3:AmazingRewardsBalance>10442.00</sc_3:AmazingRewardsBalance> 
     <sc_3:CashBackSavingsBalance>0.00</sc_3:CashBackSavingsBalance> 
     </sc_3:FdrRewards> 
    </sc_3:GetFdrRewardsResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

我使用這個變換:

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:sc="http://somecompany.com/soa/cardaccount1.1/schema" 
    xmlns:sc_1="http://somecompany.com/soa/common1.1/schema" 
    xmlns:sc_2="http://somecompany.com/soa/common/schema" 
    xmlns:sc_3="http://somecompany.com/soa/cardaccount/schema" 
    > 

    <xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes"/> 

    <xsl:strip-space elements="*"/> 

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

    <xsl:template match="sc:IsRewardsMember"/> 
    <xsl:template match="sc:IsEligibleToRedeem"/> 

    <xsl:template match="sc:*"> 
    <xsl:element name="sc_3:{local-name()}"> 
     <xsl:apply-templates/> 
    </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

哪幾乎奏效。它將得到的名稱空間聲明放在每個子元素上,而不是在父元素中聲明一次。

任何想法?一個完整的XSLT文件將是非常美妙.....

+0

注意,你不只是「移動空間聲明」,要更改的元素的名稱(該名稱包含本地名加上命名空間URI)。如果您想要在實際不需要的元素上生成名稱空間聲明,那麼在XSLT 2.0中使用xsl:namespace指令;在1.0中使用xsl:copy將名稱空間節點從輸入中的某處複製到需要的地方。 – 2013-02-20 09:33:07

回答

0

當我改變你的XSLT以下幾點:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sc="http://somecompany.com/soa/cardaccount1.1/schema" xmlns:sc_1="http://somecompany.com/soa/common1.1/schema" xmlns:sc_2="http://somecompany.com/soa/common/schema" xmlns:sc_3="http://somecompany.com/soa/cardaccount/schema"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

    <xsl:template match="sc:IsRewardsMember"/> 
    <xsl:template match="sc:IsEligibleToRedeem"/> 

    <xsl:template match="sc:GetFdrRewardsResponse"> 
     <sc_3:GetFdrRewardsResponse> 
      <xsl:apply-templates select="node()|@*"/> 
     </sc_3:GetFdrRewardsResponse> 
    </xsl:template> 
</xsl:stylesheet> 

所需的輸出幾乎是你想要的東西,但還是下面的命名空間是在輸出:

xmlns:sc="http://somecompany.com/soa/cardaccount1.1/schema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

但它不應該傷害任何人

+0

只需添加一個'排除輸出前綴=「SC XSI」'來擺脫他們。 – 2013-02-19 20:25:03

+0

這很好用!謝謝。我無法調整,因爲我沒有足夠的分數。 – 2013-02-19 21:52:37

+0

添加排除輸出前綴屬性促使子元素具有SC的命名空間聲明: 2013-02-19 22:37:22