2011-11-14 38 views
2

我想太多瞭解析我的XML升技像一個數據集,並具備以下條件:XSLT聯合來組合數據集?

... 
     <InvestmentStrategy Id="Employee"> 
      <FundSplit FundName="Fund032" PctInvested="20.0000" /> 
      <FundSplit FundName="Fund034" PctInvested="20.0000" /> 
      <FundSplit FundName="Fund035" PctInvested="10.0000" /> 
      <FundSplit FundName="Fund042" PctInvested="20.0000" /> 
      <FundSplit FundName="Lifeasdstyle030" PctInvested="30.0000" /> 
     </InvestmentStrategy> 
     <InvestmentStrategy Id="Employer"> 
      <FundSplit FundName="Fund092" PctInvested="20.0000" /> 
      <FundSplit FundName="Fund094" PctInvested="20.0000" /> 
      <FundSplit FundName="Fund095" PctInvested="10.0000" /> 
      <FundSplit FundName="Fund092" PctInvested="20.0000" /> 
      <FundSplit FundName="Lifasdestyle030" PctInvested="30.0000" /> 
     </InvestmentStrategy> 

... 

     <InvestmentAccount Id="Sequence001" Type="Standard" InvestmentStrategyId="Employee" ParameterOverrideIds="AllocationRateOverride"> 
      <Investment FundName="Fund092" FundValue="7395.91" /> 
      <Investment FundName="Fund094" FundValue="7222.72" /> 
      <Investment FundName="Fund095" FundValue="3903.52" /> 
      <Investment FundName="Fund098" FundValue="11051.32" /> 
      <Investment FundName="Fund092" FundValue="6602.54" /> 
     </InvestmentAccount> 
     <InvestmentAccount Id="Sequence002" Type="Standard" InvestmentStrategyId="Employer" ParameterOverrideIds="AllocationRateOverride"> 
      <Investment FundName="Fund032" FundValue="4754.82" /> 
      <Investment FundName="Fund034" FundValue="4643.48" /> 
      <Investment FundName="Fund035" FundValue="2509.46" /> 
      <Investment FundName="Fund038" FundValue="7104.71" /> 
      <Investment FundName="Fund042" FundValue="4244.08" /> 
     </InvestmentAccount> 

我想要做的是能要「選擇」所有elemets其中InvestmentStrategy/@ ID等於to InvestmentAccount/@ InvestmentStrategyId

並循環組合數據節點。我曾嘗試沒有成功「unioning」 2,也像

任何線索?幫幫我?

回答

2

這種變換:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:key name="kacctByStratId" match="InvestmentAccount" 
    use="@InvestmentStrategyId"/> 

<xsl:template match="InvestmentStrategy"> 
    <strategyData> 
    <xsl:copy-of select="."/> 
    <xsl:copy-of select="key('kacctByStratId', @Id)"/> 
    </strategyData> 
</xsl:template> 
<xsl:template match="text()"/> 
</xsl:stylesheet> 

當所提供的XML施加(包裹在頂部元件,使之良好的文檔):

<t> 
     <InvestmentStrategy Id="Employee"> 
      <FundSplit FundName="Fund032" PctInvested="20.0000" /> 
      <FundSplit FundName="Fund034" PctInvested="20.0000" /> 
      <FundSplit FundName="Fund035" PctInvested="10.0000" /> 
      <FundSplit FundName="Fund042" PctInvested="20.0000" /> 
      <FundSplit FundName="Lifeasdstyle030" PctInvested="30.0000" /> 
     </InvestmentStrategy> 
     <InvestmentStrategy Id="Employer"> 
      <FundSplit FundName="Fund092" PctInvested="20.0000" /> 
      <FundSplit FundName="Fund094" PctInvested="20.0000" /> 
      <FundSplit FundName="Fund095" PctInvested="10.0000" /> 
      <FundSplit FundName="Fund092" PctInvested="20.0000" /> 
      <FundSplit FundName="Lifasdestyle030" PctInvested="30.0000" /> 
     </InvestmentStrategy> 

... 

     <InvestmentAccount Id="Sequence001" Type="Standard" InvestmentStrategyId="Employee" ParameterOverrideIds="AllocationRateOverride"> 
      <Investment FundName="Fund092" FundValue="7395.91" /> 
      <Investment FundName="Fund094" FundValue="7222.72" /> 
      <Investment FundName="Fund095" FundValue="3903.52" /> 
      <Investment FundName="Fund098" FundValue="11051.32" /> 
      <Investment FundName="Fund092" FundValue="6602.54" /> 
     </InvestmentAccount> 
     <InvestmentAccount Id="Sequence002" Type="Standard" InvestmentStrategyId="Employer" ParameterOverrideIds="AllocationRateOverride"> 
      <Investment FundName="Fund032" FundValue="4754.82" /> 
      <Investment FundName="Fund034" FundValue="4643.48" /> 
      <Investment FundName="Fund035" FundValue="2509.46" /> 
      <Investment FundName="Fund038" FundValue="7104.71" /> 
      <Investment FundName="Fund042" FundValue="4244.08" /> 
     </InvestmentAccount> 

</t> 

產生希望「組合數據」結果

<strategyData> 
    <InvestmentStrategy Id="Employee"> 
     <FundSplit FundName="Fund032" PctInvested="20.0000" /> 
     <FundSplit FundName="Fund034" PctInvested="20.0000" /> 
     <FundSplit FundName="Fund035" PctInvested="10.0000" /> 
     <FundSplit FundName="Fund042" PctInvested="20.0000" /> 
     <FundSplit FundName="Lifeasdstyle030" PctInvested="30.0000" /> 
    </InvestmentStrategy> 
    <InvestmentAccount Id="Sequence001" Type="Standard" InvestmentStrategyId="Employee" ParameterOverrideIds="AllocationRateOverride"> 
     <Investment FundName="Fund092" FundValue="7395.91" /> 
     <Investment FundName="Fund094" FundValue="7222.72" /> 
     <Investment FundName="Fund095" FundValue="3903.52" /> 
     <Investment FundName="Fund098" FundValue="11051.32" /> 
     <Investment FundName="Fund092" FundValue="6602.54" /> 
    </InvestmentAccount> 
</strategyData> 
<strategyData> 
    <InvestmentStrategy Id="Employer"> 
     <FundSplit FundName="Fund092" PctInvested="20.0000" /> 
     <FundSplit FundName="Fund094" PctInvested="20.0000" /> 
     <FundSplit FundName="Fund095" PctInvested="10.0000" /> 
     <FundSplit FundName="Fund092" PctInvested="20.0000" /> 
     <FundSplit FundName="Lifasdestyle030" PctInvested="30.0000" /> 
    </InvestmentStrategy> 
    <InvestmentAccount Id="Sequence002" Type="Standard" InvestmentStrategyId="Employer" ParameterOverrideIds="AllocationRateOverride"> 
     <Investment FundName="Fund032" FundValue="4754.82" /> 
     <Investment FundName="Fund034" FundValue="4643.48" /> 
     <Investment FundName="Fund035" FundValue="2509.46" /> 
     <Investment FundName="Fund038" FundValue="7104.71" /> 
     <Investment FundName="Fund042" FundValue="4244.08" /> 
    </InvestmentAccount> 
</strategyData> 
+0

感謝這與我想要的非常接近(採取孤立它是完美的)現在我必須將此代碼放入現有模板中,該模板已經使用模板匹配和各種循環等來構建XML數據集。任何建議?這可以工作沒有模板,因爲我相信他們不能嵌套? 的 ... ... .. –

+0

我想我用'for-each'破解它

+0

@JonH:是的,這是可能的,你找到了一種方法。我當然更願意把''改爲''。你能否考慮接受這個答案?你也可以考慮接受我的答案,這個答案恰好比目前接受的答案好,還有另一個問題。 –