2014-05-15 80 views
0

使用以下輸入XML,我的目標是最初選擇portfolios/portfolio節點,然後<tradeContribution>子節點 - 特別是contributions/tradeContribution使用XSLT訪問子節點並檢索元素值

這些tradeContribution節點將被轉換爲下面顯示的xml輸出中的<trade>節點。

低於我的XML輸出和XSLT代碼如下...

 <outBound> 
<requestReceivedTime>2014-05-15 15:20:42.279</requestReceivedTime> 
<responseSentTime>2014-05-15 15:20:42.338</responseSentTime> 
<body> 
    <portfolios> 
     <portfolio id="36" nodeDate="2014-05-06"> 
      <query> 
       <firstTerm> 
        <date>2014-05-06</date> 
       </firstTerm> 
       <lastTerm> 
        <date>2014-05-06</date> 
       </lastTerm> 
      </query> 
      <exposure>492691.50878619519</exposure> 
      <contributions> 
       <tradeContribution contextId="0" contribution="267624.242492124" dealId="IRSW-TRADE-00011" desc="IRSW-FIX-FLOAT" order="0" sysId="1" tradeId="IRSW-TRADE-00011"> 
        <hideTrade>false</hideTrade> 
        <readOnly>false</readOnly> 
       </tradeContribution> 
       <tradeContribution contextId="7" contribution="225067.26629407122" dealId="IRSW-TRADE-00020" desc="IRSW-FIX-FLOAT" order="1" sysId="2" tradeId="IRSW-TRADE-00020"> 
        <hideTrade>false</hideTrade> 
        <readOnly>false</readOnly> 
       </tradeContribution> 
      </contributions> 
      <nodeAnalysis id="HSVaR 5D 100 ES"> 
       <method>INTERPOLATED_EXPECTED_SHORTFALL</method> 
       <exposure>true</exposure> 
       <percentile>100</percentile> 
      </nodeAnalysis> 
     </portfolio> 
    </portfolios> 
    </body> 
</outBound> 

我想要的XML輸出:

<?xml version="1.0" ?> 
    <collection> 
    <trade> 
    <legal_id>36</legal_id> 
    <tradeRef>IRS-RRT-002</tradeRef> 
    <system>MY SYSTEM IRS</system> 
    <indepMtmValuation>111111</indepMtmValuation> 
    <indepMtmValuationCcy>USD</indepMtmValuationCcy> 
    <principalDealLevelUpfront>TRUE</principalDealLevelUpfront> 
    <principalDealLevelAmount>14</principalDealLevelAmount> 
    <principalDealLevelCurrency>USD</principalDealLevelCurrency> 
    <principalDealLevelType>Independent Amount</principalDealLevelType> 
    <operation>U</operation> 
    </trade>  
    <trade> 
    <legal_id>36</legal_id> 
    <system>MY SYSTEM CDS</system> 
    <tradeRef>CDS-RRT-008</tradeRef> 
    <mtmValuation>222222</mtmValuation> 
    <mtmValuationDate>2013-09-11</mtmValuationDate> 
    <mtmValuationLocalSysCcy>USD</mtmValuationLocalSysCcy> 
    <counterpartyDealLevelUpfront>TRUE</counterpartyDealLevelUpfront> 
    <counterpartyDealLevelAmount>15</counterpartyDealLevelAmount> 
    <counterpartyDealLevelCurrency>JPY</counterpartyDealLevelCurrency> 
    <counterpartyDealLevelType>Independent Amount</counterpartyDealLevelType>  
    </trade> 
</collection> 

的XSLT我至今如下;然而,我有一個問題:

1)試圖找到最好的方法來拉的屬性「id」,這是<portfolio id="36" nodeDate="2014-05-06">的一部分的值。 我需要輸出到<legal_id>,但在這裏是行不通的:

<legal_id><xsl:value-of select="../portfolio[@id]"/></legal_id> 

您的意見將幫助正確構建我的XSLT將不勝感激,以產生如上所示的期望XML輸出:

我XSLT代碼迄今:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" 
exclude-result-prefixes="xs xd" 
version="1.0"> 

<!-- Variable declaration --> 
<xsl:variable name="hsVar1D" select="'1D (99%)'"></xsl:variable> 
<xsl:variable name="hsVar5D" select="'HSVaR 5D 100 ES'"></xsl:variable> 

<xsl:template match="/*"> 
    <collection> 
     <xsl:apply-templates select="/outbound/body/portfolios/portfolio[descendant::nodeAnalysis[@id[contains(.,$hsVar5D)]]]" />    
    </collection> 
</xsl:template> 
<xsl:template match="*"> 
    <xsl:apply-templates select="contributions/tradeContribution"/> 
</xsl:template> 
<xsl:template match="contributions/tradeContribution"> 
    <trade> 
     <!-- QUESTION: For <legal_id> what is the best way to select the @id attrib from the ancestor node <portfolio> --> 
     <legal_id><xsl:value-of select="@id"/></legal_id> 
     <legal_id222><xsl:value-of select="../portfolio[@id]"/></legal_id222> 
     <tradeRef>IRS-RRT-002</tradeRef> 
     <system>MY SYSTEM IRS</system> 
     <indepMtmValuation>111111</indepMtmValuation> 
     <indepMtmValuationCcy>USD</indepMtmValuationCcy> 
     <principalDealLevelUpfront>TRUE</principalDealLevelUpfront> 
     <principalDealLevelAmount><xsl:value-of select="exposure"></xsl:value-of></principalDealLevelAmount> 
     <principalDealLevelCurrency>USD</principalDealLevelCurrency> 
     <principalDealLevelType>Independent Amount</principalDealLevelType> 
     <operation>U</operation> 
    </trade> 
</xsl:template> 

回答

1

試試這樣說:

<?xml version="1.0" encoding="UTF-8"?> 
<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"/> 

<!-- Variable declaration --> 
<xsl:variable name="hsVar1D" select="'1D (99%)'"></xsl:variable> 
<xsl:variable name="hsVar5D" select="'HSVaR 5D 100 ES'"></xsl:variable> 

<xsl:template match="/"> 
    <collection> 
     <xsl:apply-templates select="outBound/body/portfolios/portfolio[nodeAnalysis[contains(@id,$hsVar5D)]]/contributions/tradeContribution" />    
    </collection> 
</xsl:template> 

<xsl:template match="tradeContribution"> 
    <trade> 
     <legal_id> 
      <xsl:value-of select="../../../portfolio/@id"/> 
     </legal_id> 
     <tradeRef>IRS-RRT-002</tradeRef> 
     <system>RAZOR IRS</system> 
     <indepMtmValuation>111111</indepMtmValuation> 
     <indepMtmValuationCcy>USD</indepMtmValuationCcy> 
     <principalDealLevelUpfront>TRUE</principalDealLevelUpfront> 
     <principalDealLevelAmount> 
      <xsl:value-of select="../../nodeAnalysis/exposure"/> 
     </principalDealLevelAmount> 
     <principalDealLevelCurrency>USD</principalDealLevelCurrency> 
     <principalDealLevelType>Independent Amount</principalDealLevelType> 
     <operation>U</operation> 
    </trade> 
</xsl:template> 

</xsl:stylesheet> 

-
注出站與razorOutbound

+0

感謝,我會嘗試,現在。我也更新了帖子,以匹配''。我正在清理帖子的價值等。 –