将孙子节点合并到父容器中



我有一个这样的XML结构:

  <GetAccount_Output>
     <GetAccountResponse>
        <AccountCollection>
           <Account>
              <AccountId>1</AccountId>
           </Account>
        </AccountCollection>
     </GetAccountResponse>
     <GetAccountResponse>
        <AccountCollection>
           <Account>
              <AccountId>2</AccountId>
           </Account>
        </AccountCollection>
     </GetAccountResponse>
  </GetAccount_Output>

期望的输出是:

        <GetAccount_Output>
           <GetAccountResponse>
              <AccountCollection>
                 <Account>
                    <AccountId>1</AccountId>
                 </Account>
                 <Account>
                    <AccountId>2</AccountId>
                 </Account>
              </AccountCollection>
           </GetAccountResponse>
        </GetAccount_Output>    

我可以设法删除一个:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
    <xsl:template match="/*">
      <xsl:copy>
      <GetAccountResponse>
        <xsl:copy-of select="GetAccountResponse/*"/>
      </GetAccountResponse>
      <xsl:apply-templates select="*[name()!='GetAccountResponse']"/>
    </xsl:copy>
  </xsl:template>   
</xsl:stylesheet>

给出如下结果:

        <GetAccount_Output>
           <AccountCollection>
              <Account>
                 <AccountId>1</AccountId>
              </Account>
              <Account>
                 <AccountId>2</AccountId>
              </Account>
           </AccountCollection>
        </GetAccount_Output>

但是我不知道如何合并GetAccountResponseAccountCollection ?谁能帮我找到正确的解决方案?

可以这样看:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="GetAccount_Output">
    <GetAccount_Output>
        <GetAccountResponse>
            <AccountCollection>
                <xsl:apply-templates select="GetAccountResponse"/>
            </AccountCollection>
        </GetAccountResponse>
    </GetAccount_Output>
</xsl:template>
<xsl:template match="GetAccountResponse">
    <xsl:apply-templates select="AccountCollection/Account"/>
</xsl:template>
</xsl:stylesheet>

,
这是另一个:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
    <GetAccount_Output>
        <GetAccountResponse>
            <AccountCollection>
                <xsl:for-each select="GetAccount_Output/GetAccountResponse/AccountCollection/Account">
                    <xsl:copy-of select="."/>
                </xsl:for-each>
            </AccountCollection>
        </GetAccountResponse>
    </GetAccount_Output>
</xsl:template>
</xsl:stylesheet>

这里有一种稍微不同的方法:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"    version="1.0">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    <!-- Copy all nodes -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <!-- Ignore any GetAccountResponse except the first -->
    <xsl:template match="GetAccountResponse[position() != 1]"/>
    <!-- Include all Account in AccountCollection -->
    <xsl:template match="AccountCollection">
        <xsl:copy>
            <xsl:apply-templates select="//Account"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

最新更新