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

时间:2014-05-15 16:39:02

标签: xslt-1.0

使用以下输入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>

1 个答案:

答案 0 :(得分: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>

-
注意outBound vs. razorOutbound