0

我有一个这样的 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?谁能将我推向正确的解决方案?

4

2 回答 2

3

这是看待它的一种方法:

<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>
于 2014-07-02T16:18:17.660 回答
0

这是一种稍微不同的方法:

<?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>
于 2014-07-02T16:22:42.170 回答