使用XSLT选择多个子节点

时间:2014-05-15 22:06:30

标签: xslt-1.0

我在以下输入XML中准确选择了我想要的内容时遇到了一些麻烦。 我需要基本上选择keys/key,其中有多个<key>子节点。

例如,这是一个示例输入XML文件:

  <Outbound environment="" request="selectPortfolios">
<body matchedRecordCount="4">       
    <portfolioSummary portfolioId="36">
        <portfolio id="36">
            <currency>USD</currency>
            <keys>
                <key displayValue="DummyCounterpartyIRS" sequenceNumber="100" sequenceValue="DummyCounterpartyIRS" type="Counterparty" value="DummyCounterpartyIRS"/>
                <key displayValue="Internal VaR IRS LCH" sequenceNumber="2000" sequenceValue="80" type="ExpType" value="InternalVaR_IRS_LCH"/>
            </keys>
            <status>Unapproved</status>
        </portfolio>
        <exposureProfile>
            <node date="2014-05-06">
                <tag>HSVaR 5D 100 ES</tag>
                <exposure>492692</exposure>
            </node>
        </exposureProfile>
    </portfolioSummary>
    <portfolioSummary portfolioId="37">     
        <portfolio id="37">
            <currency>USD</currency>
            <keys>
                <key displayValue="DummyCounterpartyIRS" sequenceNumber="100" sequenceValue="DummyCounterpartyIRS" type="Counterparty" value="DummyCounterpartyIRS"/>
                <key displayValue="Internal VaR CDS LCH" sequenceNumber="2000" sequenceValue="81" type="ExpType" value="InternalVaR_CDS_LCH"/>
            </keys>
            <status>Unapproved</status>
        </portfolio>
        <exposureProfile>
            <node date="2014-05-09">
                <tag>Node Scenario 4</tag>
                <exposure>248057</exposure>
            </node>
            <node date="2014-05-10">
                <tag>Node Scenario 5</tag>
                <exposure>373130</exposure>
            </node>
            <node date="2014-05-11">
                <tag>EXPECTED_SHORTFALL 99.7</tag>
                <exposure>373130</exposure>
            </node>
        </exposureProfile>
    </portfolioSummary>
    <portfolioSummary portfolioId="71">
        <readOnly>false</readOnly>
        <portfolio id="71">
            <currency>USD</currency>
            <keys>
                <key displayValue="DummyCounterpartyCDS" sequenceNumber="100" sequenceValue="DummyCounterpartyCDS" type="Counterparty" value="DummyCounterpartyCDS"/>
                <key displayValue="Internal VaR IRS LCH" sequenceNumber="2000" sequenceValue="80" type="ExpType" value="InternalVaR_IRS_LCH"/>
            </keys>         
            <status>Unapproved</status>
        </portfolio>
        <exposureProfile>
            <node date="2014-05-06">
                <tag>HSVaR 5D 100 ES</tag>
                <exposure>58</exposure>
            </node>
        </exposureProfile>
    </portfolioSummary>
    <portfolioSummary portfolioId="72">
        <readOnly>false</readOnly>
        <portfolio id="72">
            <currency>USD</currency>
            <keys>
                <key displayValue="DummyCounterpartyCDS" sequenceNumber="100" sequenceValue="DummyCounterpartyCDS" type="Counterparty" value="DummyCounterpartyCDS"/>
                <key displayValue="Internal VaR CDS LCH" sequenceNumber="2000" sequenceValue="81" type="ExpType" value="InternalVaR_CDS_LCH"/>
            </keys>
            <status>Unapproved</status>
        </portfolio>
        <exposureProfile>
            <node date="2014-05-09">
                <tag>Node Scenario 4</tag>
                <exposure>9</exposure>
            </node>
            <node date="2014-05-10">
                <tag>Node Scenario 5</tag>
                <exposure>12</exposure>
            </node>
            <node date="2014-05-11">
                <tag>EXPECTED_SHORTFALL 99.7</tag>
                <exposure>12</exposure>
            </node>
        </exposureProfile>
    </portfolioSummary>
</body>
</Outbound>

我想要的特定<portfolioSummary>节点基于<key>属性:

1)第一个XSLT模板:    1a)其中属性类型=&#34;交易对手&#34;和价值=&#34; DummyCounterpartyIRS&#34;和    1b)其中属性类型=&#34; ExpType&#34; AND value =&#34; InternalVaR_IRS_LCH&#34;

2)第二个XSLT模板:     2a)其中属性类型=&#34;交易对手&#34;值=&#34; DummyCounterpartyCDS&#34;和     2b)其中属性类型=&#34; ExpType&#34; AND value =&#34; InternalVaR_CDS_LCH&#34;

最终应该为portfolioId =&#34; 36&#34;提取两个<portfolioSummary>个节点。和portfolioId =&#34; 72&#34;

这是我拥有的,但它没有正确选择:

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

<!-- Pull portfolioSummary nodes for HSVaR -->
<xsl:template match="/*">
    <collection>
        <xsl:apply-templates select="/Outbound/body/portfolioSummary[descendant::portfolio/keys/key[@type='Counterparty' and @value='DummyCounterpartyIRS']]"/>
        <xsl:apply-templates select="/Outbound/body/portfolioSummary[descendant::portfolio/keys/key[@type='Counterparty' and @value='DummyCounterpartyCDS']]"/>

    </collection>
</xsl:template>
<xsl:template match="*">
    <xsl:choose>
        <xsl:when test="descendant::portfolio/keys/key[@type='ExpType' and @value='InternalVaR_IRS_LCH']">
        <extIA>
            <AGREEMENTID><xsl:value-of select="@portfolioId"></xsl:value-of></AGREEMENTID>
            <legal_id><xsl:value-of select="portfolio/keys/key[@type='Counterparty']/@displayValue"/></legal_id>
            <PRODUCT><xsl:value-of select="portfolio/keys/key[@type='Counterparty']/@displayValue"/></PRODUCT>               
            <AMOUNT><xsl:value-of select="exposureProfile/node[tag/text()[contains(.,$hsVar5D)]]/exposure"/></AMOUNT>
            <CURRENCY><xsl:value-of select="bandStructure/currency"/></CURRENCY>
            <ValuationDate>2012-05-15</ValuationDate>
            <externalSystem>MY EXT SYSTEM</externalSystem>
        </extIA>
        </xsl:when>
        <xsl:when test="descendant::portfolio/keys/key[@type='ExpType' and @value='InternalVaR_CDS_LCH']">
            <extIA>
                <AGREEMENTID><xsl:value-of select="@portfolioId"></xsl:value-of></AGREEMENTID>
                <legal_id><xsl:value-of select="portfolio/keys/key[@type='Counterparty']/@displayValue"/></legal_id>
                <PRODUCT><xsl:value-of select="portfolio/keys/key[@type='ExpType']/@displayValue"/></PRODUCT>
                                    <AMOUNT><xsl:value-of select="exposureProfile/node[tag/text()[contains(.,$hsVar5D)]]/exposure"/></AMOUNT>
                <CURRENCY><xsl:value-of select="bandStructure/currency"/></CURRENCY>
                <ValuationDate>2012-05-15</ValuationDate>
                <externalSystem>MY EXT SYSTEM</externalSystem>
            </extIA>
        </xsl:when>
        <xsl:otherwise></xsl:otherwise>
    </xsl:choose>

</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

第一个模板中的<xsl:apply-templates>未选择任何内容。您正在匹配/*并选择绝对位置路径,该路径以不存在的元素开头:razorOutbound

将其更改为:

<xsl:template match="/">
    <collection>
        <xsl:apply-templates select="Outbound/body/portfolioSummary[descendant::portfolio/keys/key[@type='Counterparty' and @value='DummyCounterpartyIRS']]"/>
        <xsl:apply-templates select="Outbound/body/portfolioSummary[descendant::portfolio/keys/key[@type='Counterparty' and @value='DummyCounterpartyCDS']]"/>
    </collection>
</xsl:template>

对于选择,选择四个投资组合,而不是两个。第一个表达:

//body/portfolioSummary[portfolio/keys/key[@type='Counterparty' and @value='DummyCounterpartyIRS']]

将匹配两个投资组合(36和37),另一个将匹配71和72. *模板将为每对调用两次,并且始终匹配一个或其他xsl:when。如果您只想选择其中一个,则必须为其添加一些其他限制。