我對XSLT比較新,但想執行我認爲是元素之間相對簡單的匹配以獲得另一個元素。XSLT匹配2個元素得到另一個
這是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>
什麼我搭售做的是匹配的節點//會計/會計/身份證以// InvestmentHoldingBalances/HoldingBalance/ACCOUNTID,當有一個匹配得到相應的業主// /業主/標識屬於到該帳戶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>
的輸出是相同的所有者ID對於每行(即「273228M」的最後一列),而不是匹配所有者ID根據帳戶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"
感謝您的任何建議。
伊恩謝謝你的明確解釋。您的解決方案完美運作 – user2356926 2013-05-08 02:03:51
@ user2356926很高興幫助,歡迎來到Stack Overflow。請注意,當您獲得解決問題的答案時,通過單擊答案左側的刻度線來接受答案是一個不錯的主意,這樣其他人就可以一目瞭然地看到問題已解決。 – 2013-05-08 07:18:45
完成。再次感謝。 – user2356926 2013-05-16 06:23:13