2013-10-11 37 views
0

接收遞歸這樣的XMLXSLT:轉變遞歸和重複的Xml

<ResponseXml> 
    <AccountData> 
    <AccountInformation> 
    <AccountNumber>123465</AccountNumber> 
    <BankCode>456</BankCode> 
    <OwnerInformation> 
     <FirstName>Himanshu</FirstName> 
     <LastName>Yadav</LastName> 
    </OwnerInformation> 
    <AccountInformation> 
     <AccountNumber>78910</AccountNumber> 
     <BankCode>123</BankCode> 
     <OwnerInformation> 
     <FirstName>My</FirstName> 
     <LastName>Wife</LastName> 
     </OwnerInformation> 
    </AccountInformation> 
    </AccountInformation> 
    </AccountData> 
    </ResponseXml> 

它有一個傳統應用程序被格式化成:

<BillingInformation> 
<AccountNumber>123465</AccountNumber> 
<BankCode>456</BankCode> 
</BillingInformation> 
<ClientInfo> 
<FirstName>Himanshu</FirstName> 
<LastName>Yadav</LastName> 
</ClientInfo> 
<BillingInformation2> 
<AccountNumber>78910</AccountNumber> 
<BankCode>123</BankCode> 
</BillingInformation2> 
<ClientInfo> 
<FirstName>My</FirstName> 
<LastName>Wife</LastName> 
</ClientInfo> 

作爲新XSLT轉換我正在爲多個問題:

  1. 複製父項值時排除子元素。
  2. 然後複製新根元素下的排除子元素。

到目前爲止試過。
遞歸部分的部分解決方案。由於您使用的身份模板它不排除根元素<ResponseXml><AccountData>

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

    <xsl:template match="AccountInformation"> 
    <BillingInformation> 
     <xsl:apply-templates select="*[name()!='AccountInformation']"/> 
    </BillingInformation> 
    <xsl:apply-templates select="AccountInformation"/> 
    </xsl:template> 

    <xsl:template match="AccountInformation/AccountInformation"> 
    <BillingInformation2> 
     <xsl:apply-templates/> 
    </BillingInformation2> 
    </xsl:template> 
+1

請顯示您到目前爲止所嘗試的內容。 –

+0

@JimGarrison我已經添加了我的部分解決方案來處理xml的遞歸部分。但我不知道排除每個孩子''元素。 –

+0

您是否認爲我以正確的方式解決了這個問題? –

回答

0

,那麼你必須覆蓋,對於要行動上的任何元素的模板。在這種情況下,如果您只需要刪除ResponseXmlAccountData元素,那麼您只需爲它們創建一個空模板。

<xsl:template match="ResponseXml | AccountData"> 
    <xsl:apply-templates/> 
</xsl:template> 

將上面的行添加到您的XSL,然後它不會輸出這兩個元素。

+0

謝謝。任何關於複製子元素的想法? –

+1

@HimanshuYadav我錯過了這個不會輸出子元素的事實。我已經更新了答案,以顯示如何刪除元素並繼續處理子元素。 –