2013-10-11 38 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我错过了这个不会输出子元素的事实。我已经更新了答案,以显示如何删除元素并继续处理子元素。 –