我对 XSLT 比较陌生,但想要执行我认为是元素之间相对简单的匹配以获得另一个元素。
这是 XML 的一个片段。版本 = 1.0,输出为文本(将 xml 转换为文本)。
<Accounts>
<Account>
<Id>273228MD301</Id>
<EPIProductCode>IPP4D3</EPIProductCode>
<Name>Mr John Smith</Name>
<Status>Open</Status>
<Owners>
<Owner>
<Id>273228M</Id>
</Owner>
</Owners>
<Advisers>
<Adviser>
<Id>286666</Id>
<PrimaryAdviser>true</PrimaryAdviser>
</Adviser>
</Advisers>
<Delete>false</Delete>
<LastModified>2012-06-08T15:19:19</LastModified>
</Account>
<Account>
<Id>273228MD399</Id>
<EPIProductCode>IPAAA</EPIProductCode>
<Name>Sir Leslie Patterson</Name>
<Status>Open</Status>
<Owners>
<Owner>
<Id>2732299</Id>
</Owner>
</Owners>
<Advisers>
<Adviser>
<Id>286666</Id>
<PrimaryAdviser>true</PrimaryAdviser>
</Adviser>
</Advisers>
<Delete>false</Delete>
<LastModified>2012-06-08T15:19:19</LastModified>
</Account>
<Account>
<Id>273228MD999</Id>
<EPIProductCode>IPYYY</EPIProductCode>
<Name>Dame Edna</Name>
<Status>Open</Status>
<Owners>
<Owner>
<Id>27322YY</Id>
</Owner>
</Owners>
<Advisers>
<Adviser>
<Id>286666</Id>
<PrimaryAdviser>true</PrimaryAdviser>
</Adviser>
</Advisers>
<Delete>false</Delete>
<LastModified>2012-06-08T15:19:19</LastModified>
</Account>
</Accounts>
<InvestmentHoldingBalances>
<HoldingBalance>
<AccountId>273228MD399</AccountId>
<InvestmentCode>TEST123</InvestmentCode>
<Exchange>FND</Exchange>
<UnitBalance>
<Settled Currency="AUD">0</Settled>
<Pending Currency="AUD">0</Pending>
<AsAtDate>2012-06-08T15:19:34</AsAtDate>
</UnitBalance>
<LastModified>2012-05-16T00:00:00</LastModified>
</HoldingBalance>
<HoldingBalance>
<AccountId>273228MD301</AccountId>
<InvestmentCode>0114AU</InvestmentCode>
<Exchange>FND</Exchange>
<UnitBalance>
<Settled Currency="AUD">0</Settled>
<Pending Currency="AUD">0</Pending>
<AsAtDate>2012-06-08T15:19:34</AsAtDate>
</UnitBalance>
<LastModified>2012-05-16T00:00:00</LastModified>
</HoldingBalance>
<HoldingBalance>
<AccountId>273228MD301</AccountId>
<InvestmentCode>0016AU</InvestmentCode>
<Exchange>FND</Exchange>
<UnitBalance>
<Settled Currency="AUD">0</Settled>
<Pending Currency="AUD">0</Pending>
<AsAtDate>2012-06-08T15:19:34</AsAtDate>
</UnitBalance>
<LastModified>2012-05-16T00:00:00</LastModified>
</HoldingBalance>
<HoldingBalance>
<AccountId>273228MD301</AccountId>
<InvestmentCode>0277AU</InvestmentCode>
<Exchange>FND</Exchange>
<UnitBalance>
<Settled Currency="AUD">0</Settled>
<Pending Currency="AUD">0</Pending>
<AsAtDate>2012-06-08T15:19:34</AsAtDate>
</UnitBalance>
<LastModified>2012-05-15T00:00:00</LastModified>
</HoldingBalance>
<HoldingBalance>
<AccountId>273228MD999</AccountId>
<InvestmentCode>TD0155</InvestmentCode>
<Exchange>FND</Exchange>
<UnitBalance>
<Settled Currency="AUD">0</Settled>
<Pending Currency="AUD">0</Pending>
<AsAtDate>2012-06-08T15:19:34</AsAtDate>
</UnitBalance>
<LastModified>2012-05-21T00:00:00</LastModified>
</HoldingBalance>
</InvestmentHoldingBalances>
我要做的是将节点//Accounts/Account/Id 与//InvestmentHoldingBalances/HoldingBalance/AccountId 匹配,当匹配时,获取属于该帐户ID 的相应//Owners/Owner/Id。当我进行匹配时,我得到的结果是所有行的第一个 //Owners/Owner/Id,而不是单个匹配的行。这是我的 xslt;
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes" />
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<!-- Create Header Record -->
<xsl:template match="xxxxxx">
<!-- Some other xslt here to extract header info -->
<xsl:apply-templates select="InvestmentHoldingBalances/HoldingBalance" />
</xsl:template>
<!-- Create HoldingBalance Records -->
<xsl:template match="HoldingBalance">
<xsl:value-of select="AccountId" />
<xsl:text>","</xsl:text>
<xsl:value-of select="InvestmentCode" />
<xsl:text>","</xsl:text>
<xsl:value-of select="Exchange" />
<xsl:text>","</xsl:text>
<xsl:if test="../../Accounts/Account/Id=AccountId">
<xsl:value-of select="../../Accounts/Account/Owners/Owner/Id" />
</xsl:if>
<xsl:text>"</xsl:text>
<xsl:text disable-output-escaping="yes"> </xsl:text>
</xsl:template>
</xsl:stylesheet>
输出的是每一行的相同Owner Id(即'273228M'最后一列),而不是根据Account Id匹配的Owner Id;
273228MD399","TEST123","FND","273228M"
273228MD301","0114AU","FND","273228M"
273228MD301","0016AU","FND","273228M"
273228MD301","0277AU","FND","273228M"
273228MD999","TD0155","FND","273228M"
我追求的结果是这样的;
273228MD399","TEST123","FND","2732299"
273228MD301","0114AU","FND","273228M"
273228MD301","0016AU","FND","273228M"
273228MD301","0277AU","FND","273228M"
273228MD999","TD0155","FND","27322YY"
感谢您的任何建议。