使用XSLT选择多个子节点



我在下面的输入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)其中属性类型="交易对手",值="DummyCounterpartyIRS"andb) where attribute type="ExpType" AND value="InternalVaR_IRS_LCH"

2)第二个XSLT模板:2a)其中属性类型="交易对手"值="DummyCounterpartyCDS"ANDb) where attributes type="ExpType" AND value="InternalVaR_CDS_LCH"

它应该最终拉两个<portfolioSummary>节点为两个portfolioId="36"和portfolioId="72"

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

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

第一个模板中的<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。如果您只想选择其中的一个,则必须添加一些其他限制。

相关内容

  • 没有找到相关文章

最新更新