Frage

I have the following XML format below, where my main repeating node is

    <portfolioSummary>...</portfolioSummary>

One of the descendent nodes is

    <keys>
        <key>...</key>
        <key>...</key> 
    </keys>

and as you can see, there are multiple <key> child nodes.

I would like to set up an XSLT template whereby I'm selecting all <portfolioSummary> nodes where keys/key[displayValue='HSVaR'] .

My XML input sample set is :

<?xml version="1.0" encoding="UTF-8"?>
 <outBound>
    <body>
 <portfolioSummary portfolioId="61">
      <portfolio id="42">
       <currency>USD</currency>
        <keys>
           <key displayValue="My Company Inc" sequenceValue="My Company Inc" sequenceNumber="30" value="My Company Inc" />
           <key displayValue="COUNTERPARTY IRS" sequenceValue="COUNTERPARTY IRS" sequenceNumber="50" value="COUNTERPARTY IRS" />
           <key displayValue="Historical VaR" sequenceNumber="330" value="HSVaR" />               
        </keys>
      </portfolio>
      <exposureProfile>
          <node date="2008-08-08">
             <tag>HSVaR 5D 99.7 ES</tag>
             <exposure>16250079</exposure>
          </node>
      </exposureProfile>
      <summary>
       <util>33000000</util>
      </summary>
      </portfolio>
   </portfolioSummary>
  </body>
</outBound>

My desired OUTPUT with sample is (where <extIA>..</extIA> may be repeated many times over) :

  <?xml version="1.0" encoding="UTF-8"?>
  <collection  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <extIA>
    <legal_id>My Company Inc</legal_id>
    <AMOUNT>16250079</AMOUNT>
    <CURRENCY>USD</CURRENCY>
    <ValuationDate>2008-08-08</ValuationDate>
    <externalSystem>myExternalSystem123</externalSystem>
  </extIA>
  <extIA>
    <legal_id>My Company Inc</legal_id>
    <AMOUNT>100000</AMOUNT>
    <externalSystem>myExternalSystem123</externalSystem>
  </extIA>
   </collection>

and my XSLT starter code is the following (just some starter ideas):

   <?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"       
        version="1.0"
        indent="yes">
  <xsl:template match="/*">
     <collection>
        <xsl:apply-templates select="/outbound/body/portfolioSummary"/>
     </collection>
</xsl:template>
<!-- Pull portfolioSummary nodes -->
<xsl:template match="portfolio/keys/key[@value='HSVAR']">
    <xsl:apply-templates/>
</xsl:template>
  <xsl:template match="*">
    <extIA>
        <legal_id><xsl:value-of select="@portfolioId"></xsl:value-of></legal_id>
        <PRODUCT><xsl:value-of select="summary"/></PRODUCT>
        <AMOUNT><xsl:value-of select="exposureProfile"></xsl:value-of></AMOUNT>
        <CURRENCY>USD</CURRENCY>
        <ValuationDate>2012-05-15</ValuationDate>
        <externalSystem>My External System</externalSystem>
    </extIA>
  </xsl:template>
</xsl:stylesheet>

Again, I would be needing to pull all of those <portfolioSummary> nodes, but ONLY where portfolio/keys/key[@value='HSVAR'] .

Your advice would be greatly appreciated.

War es hilfreich?

Lösung

I would like to take a guess here. Given the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<outBound>
    <body>
        <portfolioSummary portfolioId="42">
            <portfolio id="42">
                <currency>USD</currency>
                <keys>
                    <key displayValue="My Company Inc" sequenceValue="My Company Inc" sequenceNumber="30" value="My Company Inc" />
                    <key displayValue="COUNTERPARTY IRS" sequenceValue="COUNTERPARTY IRS" sequenceNumber="50" value="COUNTERPARTY IRS" />
                    <key displayValue="Historical VaR" sequenceNumber="330" value="HSVaR" />               
                </keys>
            <exposureProfile>
                <node date="2008-08-08">100000</node>
            </exposureProfile>
            <summary>
                <util>33000000</util>
            </summary>
            </portfolio>
        </portfolioSummary>
    </body>
</outBound>

and the following stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:strip-space elements="*"/>

    <xsl:output indent="yes"/>

    <xsl:template match="/">
        <collection>
            <xsl:apply-templates select="/outBound/body/portfolioSummary[descendant::key/@value[.='HSVaR']]"/>
        </collection>
    </xsl:template>

    <xsl:template match="portfolioSummary">
        <extIA>
            <legal_id><xsl:value-of select="@portfolioId"/></legal_id>
            <PRODUCT><xsl:value-of select="descendant::summary/util"/></PRODUCT>
            <AMOUNT><xsl:value-of select="descendant::exposureProfile/node"/></AMOUNT>
            <CURRENCY><xsl:value-of select="descendant::currency"/></CURRENCY>
            <ValuationDate><xsl:value-of select="descendant::exposureProfile/node/@date"/></ValuationDate>
            <externalSystem>RAZOR</externalSystem>
        </extIA>
    </xsl:template>
</xsl:stylesheet>

it outputs:

<?xml version="1.0" encoding="utf-8"?>
<collection>
    <extIA>
        <legal_id>42</legal_id>
        <PRODUCT>33000000</PRODUCT>
        <AMOUNT>100000</AMOUNT>
        <CURRENCY>USD</CURRENCY>
        <ValuationDate>2008-08-08</ValuationDate>
        <externalSystem>RAZOR</externalSystem>
    </extIA>
</collection>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top