首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >寻求有关使用XSLT转换XML内容的建议

寻求有关使用XSLT转换XML内容的建议
EN

Stack Overflow用户
提问于 2014-05-30 16:21:22
回答 1查看 101关注 0票数 2

我正在使用XSLT1.0将更复杂的xml模式转换为更简化的输出。我现在不关注输出数据的准确性,而是主要关注xslt处理和xpath技术。

查看下面的源xml,我需要访问所有的'rzOutbound/body/portfolios/portfolio'节点。我可以进行XSLT处理,然后提取@id属性、<exposure>值等。这没有问题。

但是,在底部还有另一个xml部分,我需要参考--即,

代码语言:javascript
复制
<combinations> 

因此,对于我访问的每个<portfolio>节点,我需要在combinations/portfolio中查找@id属性值,并验证portfolio/key1portfolio/key2的组合。

例如,如果我遇到这种组合,我知道我在处理"VM“(或财务上的变化幅度):

代码语言:javascript
复制
  <key1 id='DummyCounterpartyIRS'></key1>
  <key2 id='MTM'></key2>

而在本例中,我处理的是"IM“(初始保证金):

代码语言:javascript
复制
 <portfolio id='35'>
   <key1 id='DummyCounterpartyIRS'></key1>
   <key2 id='InternalVaR_IRS_CCH'></key2>
 </portfolio>

那么,我是在'rzOutbound/body/portfolios/portfolio'节点级别开始模板迭代,还是在combinations/portfolio节点级别开始迭代?这是我最挣扎的地方,怎么做呢?

下面是我正在试验的一些XSLT代码,从xml源代码的底部开始,使用<combinations>节点部分:

代码语言:javascript
复制
 <?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">
 <xsl:output encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/*">
      <collection>
         <xsl:apply-templates select="combinations/portfolio"/>
      </collection>
    </xsl:template>

    <xsl:template match="combinations/portfolio">
         <-- if <key1> and <key2> @id attrib values are acceptable, then apply the 
              next template and select rzOutbound/body/portfolios/portfolio node 
              where the @id is a match -->
         <xsl:apply-templates select="/razorOutbound/body/portfolios/portfolio[@id=thisID]" />
    </xsl:template>

    <xsl:template match="contributions/tradeContribution">
       <extIA>
          <Legal_ID>
            <xsl:value-of select="ancestor::portfolio/@id"/>                
          </Legal_ID>
          <Amount>
            <xsl:value-of select="ancestor::portfolio/exposure"/>                
          </Amount>
    </xsl:template> 

 </xsl:stylesheet>

我的源xml是:

代码语言:javascript
复制
 <rzOutbound>
<body>
    <portfolios>
        <portfolio id='39'>
            <exposure>4233391.352528125</exposure>
            <contributions>
                <tradeContribution tradeId='CDS-RRT-002' dealId='CDS-RRT-002' desc='CRSW' contribution='2732540.4858249128'>
                    <hideTrade>false</hideTrade>                        
                </tradeContribution>
                <tradeContribution tradeId='CDS-RRT-001' dealId='CDS-RRT-001' desc='CRSW' contribution='1528725.384451054'>
                    <hideTrade>false</hideTrade>                        
                </tradeContribution>
            </contributions>
            <nodeAnalysis id='ES 99.7'>
                <method>EXPECTED_SHORTFALL</method>
            </nodeAnalysis>
        </portfolio>
        <portfolio id='36'>
            <exposure>1243694.4436083585</exposure>
            <contributions>
                <tradeContribution tradeId='IRS-RRT-001' dealId='IRS-RRT-001' desc='IRSW-FIX-FLOAT' contribution='743437.76086249948'>
                    <hideTrade>false</hideTrade>
                </tradeContribution>
                <tradeContribution tradeId='IRS-RRT-002' dealId='IRS-RRT-002' desc='IRSW-FIX-FLOAT' contribution='500256.682745859'>
                    <hideTrade>false</hideTrade>
                </tradeContribution>
            </contributions>
            <nodeAnalysis id='ES 99.7'>
                <method>EXPECTED_SHORTFALL</method>
            </nodeAnalysis>
        </portfolio>
        <portfolio id='35'>
            <exposure>20150788.00987801</exposure>
            <contributions>
                <tradeContribution tradeId='IRS-RRT-001' dealId='IRS-RRT-001' desc='IRSW-FIX-FLOAT' contribution='12045637.843246162'>
                    <hideTrade>false</hideTrade>                        
                </tradeContribution>
                <tradeContribution tradeId='IRS-RRT-002' dealId='IRS-RRT-002' desc='IRSW-FIX-FLOAT' contribution='8105150.1666318476'>
                    <hideTrade>false</hideTrade>                    
                </tradeContribution>
            </contributions>
            <nodeAnalysis id='HSVaR 5D 100 ES'>
                <method>INTERPOLATED_EXPECTED_SHORTFALL</method>                    
                <percentile>100</percentile>
            </nodeAnalysis>
        </portfolio>
        <portfolio id='38'>             
            <exposure>5869883.4547659159</exposure>
            <contributions>
                <tradeContribution tradeId='CDS-RRT-002' dealId='CDS-RRT-002' desc='CRSW' contribution='3871823.3074953556'>
                    <hideTrade>false</hideTrade>                        
                </tradeContribution>
                <tradeContribution tradeId='CDS-RRT-001' dealId='CDS-RRT-001' desc='CRSW' contribution='1998060.1472705603'>
                    <hideTrade>false</hideTrade>                        
                </tradeContribution>
            </contributions>
            <nodeAnalysis id='HSVaR 5D 100 ES'>
                <method>INTERPOLATED_EXPECTED_SHORTFALL</method>                    
                <percentile>100</percentile>
            </nodeAnalysis>
        </portfolio>
        <portfolio id='34'>         
            <exposure>93678157.587009192</exposure>
            <contributions>
                <tradeContribution tradeId='IRS-RRT-001' dealId='IRS-RRT-001' desc='IRSW-FIX-FLOAT' contribution='50713348.483467519'>
                    <hideTrade>false</hideTrade>                        
                </tradeContribution>
                <tradeContribution tradeId='IRS-RRT-002' dealId='IRS-RRT-002' desc='IRSW-FIX-FLOAT' contribution='42964809.103541672'>
                    <hideTrade>false</hideTrade>                        
                </tradeContribution>
            </contributions>
            <nodeAnalysis id='MTM'>
                <method>SCENARIO</method>
                <exposure>true</exposure>                   
            </nodeAnalysis>
        </portfolio>
        <portfolio id='37'>
            <exposure>-507553339.63408583</exposure>
            <contributions>
                <tradeContribution tradeId='CDS-RRT-001' dealId='CDS-RRT-001' desc='CRSW' contribution='-207732702.4400686'>
                    <hideTrade>false</hideTrade>                        
                </tradeContribution>
                <tradeContribution tradeId='CDS-RRT-002' dealId='CDS-RRT-002' desc='CRSW' contribution='-299820637.19401723'>
                    <hideTrade>false</hideTrade>                        
                </tradeContribution>
            </contributions>
            <nodeAnalysis id='MTM'>
                <method>SCENARIO</method>
                <exposure>true</exposure>                   
            </nodeAnalysis>
        </portfolio>
    </portfolios>
</body>
<combinations>
    <portfolio id='34'>
        <key1 id='DummyCounterpartyIRS'></key1>
        <key2 id='MTM'></key2>
    </portfolio>
    <portfolio id='35'>
        <key1 id='DummyCounterpartyIRS'></key1>
        <key2 id='InternalVaR_IRS_CCH'></key2>
    </portfolio>
    <portfolio id='38'>
        <key1 id='DummyCounterpartyCDS'></key1>
        <key2 id='InternalVaR_IRS_CCH'></key2>
    </portfolio>
    <portfolio id='39'>
        <key1 id='DummyCounterpartyCDS'></key1>
        <key2 id='InternalVaR_CDS_CCH'></key2>
    </portfolio>
    <portfolio id='36'>
        <key1 id='DummyCounterpartyIRS'></key1>
        <key2 id='InternalVaR_CDS_CCH'></key2>
    </portfolio>
    <portfolio id='37'>
        <key1 id='DummyCounterpartyCDS'></key1>
        <key2 id='MTM'></key2>
    </portfolio>
</combinations>
 </rzOutbound>

下面是一个xml输出示例(我现在不关注输出数据的准确性,而是主要关注xslt处理和xpath技术):

代码语言:javascript
复制
<collection>
<extIA>
  <AgreementID>39</AgreementID>
  <Legal_ID>39</Legal_ID>
  <Product/>
  <Amount>4233391.352528125</Amount>
  <Currency>USD</Currency>
  <ValuationDate>2012-05-15</ValuationDate>
  <externalSystem>IM</externalSystem>
</extIA>
<extIA>
  <AgreementID>39</AgreementID>
  <Legal_ID>39</Legal_ID>
  <Product/>
  <Amount>4233391.352528125</Amount>
  <Currency>USD</Currency>
  <ValuationDate>2012-05-15</ValuationDate>
</extIA>
<extIA>
  <AgreementID>36</AgreementID>
  <Legal_ID>36</Legal_ID>
  <Product/>
  <Amount>1243694.4436083585</Amount>
  <Currency>USD</Currency>
  <ValuationDate>2012-05-15</ValuationDate>
</extIA>
<extIA>
  <AgreementID>36</AgreementID>
  <Legal_ID>36</Legal_ID>
  <Product/>
  <Amount>1243694.4436083585</Amount>
  <Currency>USD</Currency>
  <ValuationDate>2012-05-15</ValuationDate>
</extIA>
<extIA>
  <AgreementID>34</AgreementID>
  <Legal_ID>34</Legal_ID>
  <Product/>
  <Amount>93678157.587009192</Amount>
  <Currency>USD</Currency>
  <ValuationDate>2012-05-15</ValuationDate>
</extIA>
<extIA>
  <AgreementID>34</AgreementID>
  <Legal_ID>34</Legal_ID>
  <Product/>
  <Amount>93678157.587009192</Amount>
  <Currency>USD</Currency>
  <ValuationDate>2012-05-15</ValuationDate>
</extIA>
 <extIA>
  <AgreementID>37</AgreementID>
  <Legal_ID>37</Legal_ID>
  <Product/>
  <Amount>-507553339.63408583</Amount>
  <Currency>USD</Currency>
  <ValuationDate>2012-05-15</ValuationDate>
 </extIA>
 <extIA>
  <AgreementID>37</AgreementID>
  <Legal_ID>37</Legal_ID>
  <Product/>
  <Amount>-507553339.63408583</Amount>
  <Currency>USD</Currency>
  <ValuationDate>2012-05-15</ValuationDate>
</extIA>
 </collection>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-30 17:51:52

那么,我是在‘rzOutbound/body/portfolio/portfolio’节点级别开始我的模板迭代,还是在组合/组合节点级别开始迭代?

如果--就像看起来一样-- XML的两个分支都包含相同的组合,那么您选择哪一个并不重要。通过使用portfolio id作为键,您始终可以从另一个分支中的相应组合中获取数据。

例如,如果我遇到这种组合,我知道我在处理"VM“(或财务上的变化幅度): 而在本例中,我处理的是"IM“(初始保证金):

这部分还不清楚。我看不出你从给出的文件中知道任何这样的事情。如果您希望样式表也“知道”这些事情,则必须在样式表本身中或通过链接到外部XML文档来提供这些信息。只有这样,样式表才能决定"if <key1> and <key2> @id attrib values are acceptable“。

<--如果和@id attrib值是可接受的,那么应用下一个模板并选择rzOutbound/body/portfolio节点,其中@id是匹配的->

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23959120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档