在xsl中放入空检查

时间:2015-02-26 06:23:54

标签: xslt xpath xslt-1.0

我有以下输入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我想读取tradeid方案参数的值9786042-2现在我想在交易id方案paremeter中放一个值不是exixts的检查然后null是dispalyed因此,我已经提出了xsl以下,但仍然是对于其中没有tradeid方案参数值的cses,在这些情况下不会更新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>

1 个答案:

答案 0 :(得分:1)

表达式something != ' '将产生

  • true如果任何 something不等于' '(单个空格)
  • false如果something所有等于' '
  • false如果没有something s

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

如果我理解正确,你想输出tradeIdScheme不是空白或空格的tradeId,或者输出null如果没有这样的tradeId。

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

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