在xsl中放入null检查



我有下面的输入xml,由于我现在正在使用xalan-api进行xsl转换,下面是xml

<?xml version="1.0" encoding="UTF-8"?>
<Report xmlns:fpml="http://www.fpml.org/FpML-5/confirmation" xmlns="http://www.eurexchange.com/EurexIRSFullInventoryReport" name="CB202 Full Inventory Report">
  <reportNameGrp>
    <CM>
      <acctTypGrp name="A1">
        <ProductType name="OIS">
          <currTypCod value="EUR">
            <rateIndex name="EONIA">
              <rateIndexTenor name="1D">
                <idxSource>EONIA</idxSource>
                <CCPTradeId id="612822">
                  <fpml:dataDocument fpmlVersion="5-0">
                    <fpml:trade>
                      <fpml:tradeHeader>
                        <fpml:partyTradeIdentifier>
                          <fpml:partyReference href="CPTY_612822"></fpml:partyReference>
                          <fpml:tradeId tradeIdScheme=""></fpml:tradeId>
                        </fpml:partyTradeIdentifier>
                        <fpml:partyTradeIdentifier>
                          <fpml:partyReference href="PO_612822"></fpml:partyReference>
                          <fpml:accountReference href="ACC_612822"></fpml:accountReference>
                          <fpml:tradeId tradeIdScheme="">9786042-2</fpml:tradeId>
                          <fpml:versionedTradeId>
                            <fpml:tradeId tradeIdScheme="">612822</fpml:tradeId>
                            <fpml:version>1</fpml:version>
                          </fpml:versionedTradeId>
                        </fpml:partyTradeIdentifier>
                        <fpml:tradeDate>2013-05-21</fpml:tradeDate>
                        <fpml:clearedDate>2013-11-13</fpml:clearedDate>
                      </fpml:tradeHeader>
                      <fpml:swap>
                        <fpml:swapStream id="fixedLeg_612822">
                          <fpml:payerPartyReference href="CPTY_612822"></fpml:payerPartyReference>
                          <fpml:receiverPartyReference href="PO_612822"></fpml:receiverPartyReference>
                          <fpml:calculationPeriodDates id="fixedLegCalcPeriodDates_612822">
                            <fpml:effectiveDate>
                              <fpml:unadjustedDate>2013-05-23</fpml:unadjustedDate>
                              <fpml:dateAdjustments>
                                <fpml:businessDayConvention>NONE</fpml:businessDayConvention>
                              </fpml:dateAdjustments>
                            </fpml:effectiveDate>
                            <fpml:terminationDate>
                              <fpml:unadjustedDate>2015-05-23</fpml:unadjustedDate>
                              <fpml:dateAdjustments>
                                <fpml:businessDayConvention>MODFOLLOWING</fpml:businessDayConvention>
                                <fpml:businessCenters>
                                  <fpml:businessCenter>EUTA</fpml:businessCenter>
                                </fpml:businessCenters>
                              </fpml:dateAdjustments>
                            </fpml:terminationDate>
                            <fpml:calculationPeriodDatesAdjustments>
                              <fpml:businessDayConvention>MODFOLLOWING</fpml:businessDayConvention>
                              <fpml:businessCenters>
                                <fpml:businessCenter>EUTA</fpml:businessCenter>
                              </fpml:businessCenters>
                            </fpml:calculationPeriodDatesAdjustments>
                            <fpml:calculationPeriodFrequency>
                              <fpml:periodMultiplier>1</fpml:periodMultiplier>
                              <fpml:period>Y</fpml:period>
                              <fpml:rollConvention>23</fpml:rollConvention>
                            </fpml:calculationPeriodFrequency>
                          </fpml:calculationPeriodDates>
                          <fpml:paymentDates>
                            <fpml:calculationPeriodDatesReference href="fixedLegCalcPeriodDates_612822"></fpml:calculationPeriodDatesReference>
                            <fpml:paymentFrequency>
                              <fpml:periodMultiplier>1</fpml:periodMultiplier>
                              <fpml:period>Y</fpml:period>
                            </fpml:paymentFrequency>
                            <fpml:payRelativeTo>CalculationPeriodEndDate</fpml:payRelativeTo>
                            <fpml:paymentDaysOffset>
                              <fpml:periodMultiplier>1</fpml:periodMultiplier>
                              <fpml:period>D</fpml:period>
                              <fpml:dayType>Business</fpml:dayType>
                            </fpml:paymentDaysOffset>
                            <fpml:paymentDatesAdjustments>
                              <fpml:businessDayConvention>MODFOLLOWING</fpml:businessDayConvention>
                              <fpml:businessCenters>
                                <fpml:businessCenter>EUTA</fpml:businessCenter>
                              </fpml:businessCenters>
                            </fpml:paymentDatesAdjustments>
                          </fpml:paymentDates>
                          <fpml:calculationPeriodAmount>
                            <fpml:calculation>
                              <fpml:notionalSchedule>
                                <fpml:notionalStepSchedule>
                                  <fpml:initialValue>100000000.00</fpml:initialValue>
                                  <fpml:currency>EUR</fpml:currency>
                                </fpml:notionalStepSchedule>
                              </fpml:notionalSchedule>
                              <fpml:fixedRateSchedule>
                                <fpml:initialValue>0.01200000</fpml:initialValue>
                              </fpml:fixedRateSchedule>
                              <fpml:dayCountFraction>ACT/360</fpml:dayCountFraction>
                            </fpml:calculation>
                          </fpml:calculationPeriodAmount>
                        </fpml:swapStream>
                        <fpml:swapStream id="floatingLeg_612822">
                          <fpml:payerPartyReference href="PO_612822"></fpml:payerPartyReference>
                          <fpml:receiverPartyReference href="CPTY_612822"></fpml:receiverPartyReference>
                          <fpml:calculationPeriodDates id="floatingLegCalcPeriodDates_612822">
                            <fpml:effectiveDate>
                              <fpml:unadjustedDate>2013-05-23</fpml:unadjustedDate>
                              <fpml:dateAdjustments>
                                <fpml:businessDayConvention>NONE</fpml:businessDayConvention>
                              </fpml:dateAdjustments>
                            </fpml:effectiveDate>
                            <fpml:terminationDate>
                              <fpml:unadjustedDate>2015-05-23</fpml:unadjustedDate>
                              <fpml:dateAdjustments>
                                <fpml:businessDayConvention>MODFOLLOWING</fpml:businessDayConvention>
                                <fpml:businessCenters>
                                  <fpml:businessCenter>EUTA</fpml:businessCenter>
                                </fpml:businessCenters>
                              </fpml:dateAdjustments>
                            </fpml:terminationDate>
                            <fpml:calculationPeriodDatesAdjustments>
                              <fpml:businessDayConvention>MODFOLLOWING</fpml:businessDayConvention>
                              <fpml:businessCenters>
                                <fpml:businessCenter>EUTA</fpml:businessCenter>
                              </fpml:businessCenters>
                            </fpml:calculationPeriodDatesAdjustments>
                            <fpml:calculationPeriodFrequency>
                              <fpml:periodMultiplier>1</fpml:periodMultiplier>
                              <fpml:period>Y</fpml:period>
                              <fpml:rollConvention>23</fpml:rollConvention>
                            </fpml:calculationPeriodFrequency>
                          </fpml:calculationPeriodDates>
                          <fpml:paymentDates>
                            <fpml:calculationPeriodDatesReference href="floatingLegCalcPeriodDates_612822"></fpml:calculationPeriodDatesReference>
                            <fpml:paymentFrequency>
                              <fpml:periodMultiplier>1</fpml:periodMultiplier>
                              <fpml:period>Y</fpml:period>
                            </fpml:paymentFrequency>
                            <fpml:payRelativeTo>CalculationPeriodEndDate</fpml:payRelativeTo>
                            <fpml:paymentDaysOffset>
                              <fpml:periodMultiplier>1</fpml:periodMultiplier>
                              <fpml:period>D</fpml:period>
                              <fpml:dayType>Business</fpml:dayType>
                            </fpml:paymentDaysOffset>
                            <fpml:paymentDatesAdjustments>
                              <fpml:businessDayConvention>MODFOLLOWING</fpml:businessDayConvention>
                              <fpml:businessCenters>
                                <fpml:businessCenter>EUTA</fpml:businessCenter>
                              </fpml:businessCenters>
                            </fpml:paymentDatesAdjustments>
                          </fpml:paymentDates>
                          <fpml:resetDates id="floatingLegresetDates_612822">
                            <fpml:calculationPeriodDatesReference href="floatingLegCalcPeriodDates_612822"></fpml:calculationPeriodDatesReference>
                            <fpml:resetRelativeTo>CalculationPeriodEndDate</fpml:resetRelativeTo>
                            <fpml:fixingDates>
                              <fpml:periodMultiplier>0</fpml:periodMultiplier>
                              <fpml:period>D</fpml:period>
                              <fpml:businessDayConvention>PRECEDING</fpml:businessDayConvention>
                              <fpml:businessCenters>
                                <fpml:businessCenter>EUTA</fpml:businessCenter>
                              </fpml:businessCenters>
                              <fpml:dateRelativeTo href="floatingLegresetDates_612822"></fpml:dateRelativeTo>
                            </fpml:fixingDates>
                            <fpml:resetFrequency>
                              <fpml:periodMultiplier>1</fpml:periodMultiplier>
                              <fpml:period>Y</fpml:period>
                            </fpml:resetFrequency>
                            <fpml:resetDatesAdjustments>
                              <fpml:businessDayConvention>MODFOLLOWING</fpml:businessDayConvention>
                              <fpml:businessCenters>
                                <fpml:businessCenter>EUTA</fpml:businessCenter>
                              </fpml:businessCenters>
                            </fpml:resetDatesAdjustments>
                          </fpml:resetDates>
                          <fpml:calculationPeriodAmount>
                            <fpml:calculation>
                              <fpml:notionalSchedule>
                                <fpml:notionalStepSchedule>
                                  <fpml:initialValue>100000000.00</fpml:initialValue>
                                  <fpml:currency>EUR</fpml:currency>
                                </fpml:notionalStepSchedule>
                              </fpml:notionalSchedule>
                              <fpml:floatingRateCalculation>
                                <fpml:floatingRateIndex>EUR-EONIA-OIS-COMPOUND</fpml:floatingRateIndex>
                              </fpml:floatingRateCalculation>
                              <fpml:dayCountFraction>ACT/360</fpml:dayCountFraction>
                            </fpml:calculation>
                          </fpml:calculationPeriodAmount>
                        </fpml:swapStream>
                      </fpml:swap>
                      <fpml:documentation>
                        <fpml:masterAgreement>
                          <fpml:masterAgreementType masterAgreementTypeScheme="">ISDA</fpml:masterAgreementType>
                        </fpml:masterAgreement>
                        <fpml:contractualDefinitions>ISDA2006</fpml:contractualDefinitions>
                      </fpml:documentation>
                    </fpml:trade>
                    <fpml:party id="CPTY_612822">
                      <fpml:partyId>EUREX</fpml:partyId>
                      <fpml:partyName>EUREX</fpml:partyName>
                    </fpml:party>
                    <fpml:party id="PO_612822">
                      <fpml:partyId>BOSLO</fpml:partyId>
                      <fpml:partyName>plc.</fpml:partyName>
                    </fpml:party>
                    <fpml:account id="ACC_612822">
                      <fpml:accountId>BOSLO_A1</fpml:accountId>
                      <fpml:accountBeneficiary href="PO_612822"></fpml:accountBeneficiary>
                    </fpml:account>
                  </fpml:dataDocument>
                  <novDateTime>2013-11-13 17:50:44.773</novDateTime>
                </CCPTradeId>
              </rateIndexTenor>
            </rateIndex>
          </currTypCod>
        </ProductType>
      </acctTypGrp>
    </CM>
  </reportNameGrp>
</Report>

正如你所看到的,从上面的xml中,我正在字符串中读取9786042-2的tradid-scheme参数的值,现在我想检查一下,如果值不存在于tradid-cheme参数中,那么null就会被显示出来,因此我想出了下面的xsl,但对于tradid-scheme paramtere值不存在的cse,在这些情况下null不会更新,请建议如何为这些情况填充null。

<!-- Main template starts -->
<xsl:template match="/eur:Report">
  <EurexMessage>
    <EurexFlows>
      <xsl:apply-templates select=".//eur:rateIndexTenor" />
      <!-- select="eur:reportNameGrp/eur:CM/eur:acctTypGrp/eur:ProductType/eur:currTypCod/eur:rateIndex/eur:rateIndexTenor" 
                    /> -->
    </EurexFlows>
  </EurexMessage>
</xsl:template>

<!-- sub template starts -->
<xsl:template match="eur:rateIndexTenor">
  <xsl:for-each select="eur:CCPTradeId">
    <EurexMessageObject>
      <!-- ####***** contact id is nul not displayd ****##### ----->
      <ContractID>
        <xsl:choose>
          <xsl:when test="fpml:dataDocument/*/*/fpml:partyTradeIdentifier/fpml:tradeId/@tradeIdScheme !=' '">
            <xsl:value-of
                select="fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="'null'" />
          </xsl:otherwise>
        </xsl:choose>
      </ContractID>
    </EurexMessageObject>
  </xsl:for-each>
</xsl:template>

表达式something != ' '将产生

  • true如果存在任何不等于' '(单个空间)的something
  • false如果所有something等于' '
  • 如果没有something,则为false

在您的情况下,两个something都等于''(空字符串)。由于这两者都不等于' ',因此这里的结果是true

如果我理解正确的话,您希望在tradeIdScheme不是空白或空白的情况下输出tradeId,或者如果没有这样的tradeId则输出null

如果是这样的话,应该这样做:

<ContractID>
  <xsl:variable name="foundTradeId"
      select="fpml:dataDocument/*/*/fpml:partyTradeIdentifier/fpml:tradeId
                                         [normalize-space(@tradeIdScheme)]" />
  <xsl:value-of select="$foundTradeId" />
  <xsl:if test="not($foundTradeId)">null</xsl:if>
</ContractID>

相关内容

  • 没有找到相关文章