使用XSLT访问子节点并检索元素值



使用下面的输入XML,我的目标是首先选择portfolios/portfolio节点,然后选择<tradeContribution>子节点——特别是contributions/tradeContribution

这些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>

试试这样做:

<?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>

,
注意outBound与razorOutbound

相关内容

  • 没有找到相关文章

最新更新