2013-05-12 38 views
0

我有一個xml,我試圖添加一個新元素,然後在特定條件下分配值。 一切工作正常。 但是,它似乎將新元素添加到父節點。 有人可以幫我找出問題。XSLT將新元素添加到當前節點

以下是完整的XSLT。但是我對最後一個模板有問題。

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
    xmlns:ns0="http://somenamespace"> 

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 

    <xsl:template match="node() | @*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node() | @*" /> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="ns0:Cedent/ns0:Party/ns0:Id[. = '']"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*" /> 
     <xsl:apply-templates select="../../following-sibling::ns0:Broker[1]/ns0:Party/ns0:Id/node()" /> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="ns0:Cedent/ns0:Party/ns0:Id/@Agency[. = '']"> 
    <xsl:attribute name="Agency">DUNS_dun_and_bradstreet</xsl:attribute> 
    </xsl:template> 

    <xsl:template match="ns0:Reinsurer[not(ns0:Party/ns0:Id != '') and not(ns0:Reinsurer/ns0:Party/ns0:Id/@Agency[. != ''])]" /> 

    <xsl:template match="ns0:Reinsurer/ns0:Contact[not(ns0:PersonName !='')]" /> 

    <xsl:template match="ns0:Reinsurer/ns0:Contact/*[not(node())]" /> 

    <xsl:template match="ns0:Broker/ns0:Contact/ns0:Telephone[not(node())]" /> 

    <xsl:template match="ns0:ServiceProvider[. = '6']" /> 

    <xsl:template match="ns0:ServiceProvider[not(ns0:Party/ns0:Id != '') and not(ns0:Reinsurer/ns0:Party/ns0:Id/@Agency[. != ''])]" /> 

    <xsl:template match="ns0:Contract/ns0:ContractGroupName[not(node())]" /> 

    <xsl:template match="ns0:Endorsement[ns0:Placing/ns0:PlacingStage = 'endorsement']" /> 

    <xsl:template match="ns0:Endorsement/ns0:EndorsementReference[not(node())]" /> 

    <xsl:template match="ns0:Endorsement/ns0:EndorsementName[not(node())]" /> 

    <xsl:template match="ns0:Endorsement/ns0:Description[not(node())]" /> 

    <xsl:template match="ns0:Endorsement/ns0:EffectiveDate[not(node())]" /> 

    <xsl:template match="ns0:ContractSection/ns0:BrokerSharePercentage[not(node()) and ns0:ContractSection/ns0:BrokerSharePercentage/ns0:Rate > 0]" /> 

    <xsl:template match="ns0:ContractSection/ns0:RiskLocation[not(node()) or (ns0:PlacingTransactionFunction = 'request_for_line_or_binder' or ns0:PlacingTransactionFunction = 'signed_line_advice' or ns0:PlacingTransactionFunction = 'quotation_request' or ns0:PlacingTransactionFunction = 'endorsement_request')]" /> 

    <xsl:template match="ns0:ContractSection/ns0:RiskLocation/ns0:Location/ns0:Supraentity[not(node())]" /> 

    <xsl:template match="ns0:ContractSection/ns0:RiskLocation/ns0:Location/ns0:Country[not(node())]" /> 

    <xsl:template match="ns0:ContractSection/ns0:PerilsIncluded[not(ns0:Peril/ns0:PerilType !='')]" /> 

    <xsl:template match="ns0:ContractSection/ns0:Brokerage[descendant::ns0:Rate = '' and not(ns0:ContractSection/ns0:BrokeragePercentage/ns0:Rate > 0)]" /> 

    <xsl:template match="ns0:ContractSection/ns0:ContractMarket/ns0:Reinsurer[descendant::ns0:Id = '' and not(ns0:Party/ns0:Id/@Agency[. != ''])]" /> 

    <xsl:template match="ns0:ContractSection/ns0:ContractMarket/ns0:ReinsurerContractReference[not(node())]" /> 

    <xsl:template match="ns0:ContractSection/ns0:ContractMarket/ns0:ReinsurerQuoteMaximumSharePercentage[descendant::ns0:Rate = '']" /> 

    <xsl:template match="ns0:ContractSection/ns0:ContractMarket/ns0:ReinsurerWrittenPercentage[descendant::ns0:Rate = '' and ns0:PlacingTransactionFunction = 'signed_line_advice']" /> 

    <xsl:template match="ns0:Placing"> 

    <xsl:variable name="percentage-rate" select="ns0:ContractSection/ns0:OrderPercentage/ns0:Rate"/> 

    <xsl:copy> 
     <ns0:ContractSection> 
     <ns0:LinesPercentageOfOrderIndicator> 
     <xsl:value-of select="ns0:PlacingTransactionFunction = 'signed_line_advice' and $percentage-rate > 100"/> 
     </ns0:LinesPercentageOfOrderIndicator> 
     <xsl:apply-templates select="ns0:ContractSection/ns0:ContractMarket"/> 
     </ns0:ContractSection> 
    </xsl:copy> 

    </xsl:template> 
</xsl:stylesheet> 

源XML:

<ns0:Root xmlns:ns0="http://somenamespace"> 
    <ns0:Placing Sender="broker" Receiver="serviceprovider"> 
     <ns0:UUId>GUID</ns0:UUId> 
     <ns0:BrokerReference>2B3B8992-3185-48EE-A030-0F61EFF7C7EB</ns0:BrokerReference> 
     <ns0:ServiceProviderReference>16</ns0:ServiceProviderReference> 
     <ns0:PlacingStage>order</ns0:PlacingStage> 
     <ns0:PlacingTransactionFunction>signed_line_advice</ns0:PlacingTransactionFunction> 
     <ns0:TransactionReasonDescription></ns0:TransactionReasonDescription> 
     <ns0:Cedent> 
      <ns0:Party> 
       <ns0:Id Agency=""></ns0:Id> 
       <ns0:Name>Bahamas First General Insurance Co/Aon Benfield Canada</ns0:Name> 
      </ns0:Party> 
     </ns0:Cedent> 
     <ns0:Reinsurer> 
      <ns0:Party> 
       <ns0:Id Agency=""></ns0:Id> 
       <ns0:Name>RI3K</ns0:Name> 
      </ns0:Party> 
      <ns0:Contact> 
       <ns0:PersonName>test</ns0:PersonName> 
       <ns0:Telephone></ns0:Telephone> 
       <ns0:Email>[email protected]</ns0:Email> 
      </ns0:Contact> 
     </ns0:Reinsurer> 
     <ns0:Broker> 
      <ns0:Party> 
       <ns0:Id Agency="DUNS_dun_and_bradstreet">292320710</ns0:Id> 
       <ns0:Name>Aon Benfield UK</ns0:Name> 
      </ns0:Party> 
      <ns0:Contact> 
       <ns0:PersonName>Jenny Edwards</ns0:PersonName> 
       <ns0:Telephone>reinsurance_contract</ns0:Telephone> 
       <ns0:Email>[email protected]</ns0:Email> 
      </ns0:Contact> 
     </ns0:Broker> 
     <ns0:ServiceProvider> 
      <ns0:Party> 
       <ns0:Id Agency="DUNS_dun_and_bradstreet">239195295</ns0:Id> 
      </ns0:Party> 
     </ns0:ServiceProvider> 
     <ns0:Contract> 
      <ns0:ContractName>FINCO QUOTA SHARE TREATY</ns0:ContractName> 
      <ns0:ContractGroupName>BFG FINCO QUOTA SHARE TREATY</ns0:ContractGroupName> 
      <ns0:ContractType>0</ns0:ContractType> 
      <ns0:BrokerReference>B110813BDO1053</ns0:BrokerReference> 
      <ns0:BrokerGroupReference>200153436</ns0:BrokerGroupReference> 
      <ns0:BrokerRiskReference>13BDO1053</ns0:BrokerRiskReference> 
     </ns0:Contract> 
     <ns0:Endorsement> 
      <ns0:EndorsementReference>0</ns0:EndorsementReference> 
      <ns0:EndorsementName></ns0:EndorsementName> 
      <ns0:Description></ns0:Description> 
      <ns0:EffectiveDate></ns0:EffectiveDate> 
     </ns0:Endorsement> 
     <ns0:ContractSection ContractReportingLevel="section_level"> 
      <ns0:HighLevelReference>01</ns0:HighLevelReference> 
      <ns0:CoverType>quota_share</ns0:CoverType> 
      <ns0:ContractPeriod> 
       <ns0:StartDate DateIndicator="Jan 1 2013 12:00AM"></ns0:StartDate> 
       <ns0:EndDate DateIndicator="Dec 31 2013 12:00AM"></ns0:EndDate> 
      </ns0:ContractPeriod> 
      <ns0:UnderwritingYear>Jan </ns0:UnderwritingYear> 
      <ns0:ContractSectionClass> 
       <ns0:JvClassOfBusiness>wind_storm_unspecified</ns0:JvClassOfBusiness> 
       <ns0:ClassOfBusinessDescription>wind_storm_unspecified</ns0:ClassOfBusinessDescription> 
      </ns0:ContractSectionClass> 
      <ns0:RiskLocation> 
       <ns0:Address> 
        <ns0:Country></ns0:Country> 
       </ns0:Address> 
       <ns0:Location> 
        <ns0:Supraentity></ns0:Supraentity> 
        <ns0:Country>BS</ns0:Country> 
       </ns0:Location> 
      </ns0:RiskLocation> 
      <ns0:PerilsIncluded> 
       <ns0:Peril> 
        <ns0:PerilType></ns0:PerilType> 
       </ns0:Peril> 
      </ns0:PerilsIncluded> 
      <ns0:ContractAmountsCurrency> 
       <ns0:Ccy>GBP</ns0:Ccy> 
      </ns0:ContractAmountsCurrency> 
      <ns0:BrokerSharePercentage> 
       <ns0:Rate RateUnit="percentage">2.500000</ns0:Rate> 
      </ns0:BrokerSharePercentage> 
      <ns0:OrderPercentage> 
       <ns0:Rate RateUnit="percentage">41.000000</ns0:Rate> 
      </ns0:OrderPercentage> 
      <ns0:Brokerage> 
       <ns0:BrokeragePercentage> 
        <ns0:Rate RateUnit="percentage">2.500000</ns0:Rate> 
       </ns0:BrokeragePercentage> 
      </ns0:Brokerage> 
      <ns0:ContractMarket> 
       <ns0:Reinsurer> 
        <ns0:Party> 
         <ns0:Id Agency="DUNS_dun_and_bradstreet">239195295</ns0:Id> 
        </ns0:Party> 
       </ns0:Reinsurer> 
       <ns0:ParticipantFunction>leader</ns0:ParticipantFunction> 
       <ns0:ReinsurerContractReference></ns0:ReinsurerContractReference> 
       <ns0:ReinsurerQuoteMaximumSharePercentage> 
        <ns0:Rate RateUnit="percentage">100.000000</ns0:Rate> 
       </ns0:ReinsurerQuoteMaximumSharePercentage> 
       <ns0:ReinsurerWrittenPercentage> 
        <ns0:Rate RateUnit="percentage">100.000000</ns0:Rate> 
       </ns0:ReinsurerWrittenPercentage> 
      </ns0:ContractMarket> 
     </ns0:ContractSection> 
    </ns0:Placing> 
</ns0:Root> 

XML輸出:

<ns0:Root xmlns:ns0="http://somenamespace"> 
    <ns0:Placing Sender="broker" Receiver="serviceprovider"> 
    <ns0:UUId>GUID</ns0:UUId> 
    <ns0:BrokerReference>2B3B8992-3185-48EE-A030-0F61EFF7C7EB</ns0:BrokerReference> 
    <ns0:ServiceProviderReference>16</ns0:ServiceProviderReference> 
    <ns0:PlacingStage>order</ns0:PlacingStage> 
    <ns0:PlacingTransactionFunction>signed_line_advice</ns0:PlacingTransactionFunction> 
    <ns0:TransactionReasonDescription /> 
    <ns0:Cedent> 
     <ns0:Party> 
     <ns0:Id Agency="DUNS_dun_and_bradstreet">292320710</ns0:Id> 
     <ns0:Name>Bahamas First General Insurance Co/Aon Benfield Canada</ns0:Name> 
     </ns0:Party> 
    </ns0:Cedent> 
    <ns0:Broker> 
     <ns0:Party> 
     <ns0:Id Agency="DUNS_dun_and_bradstreet">292320710</ns0:Id> 
     <ns0:Name>Aon Benfield UK</ns0:Name> 
     </ns0:Party> 
     <ns0:Contact> 
     <ns0:PersonName>Jenny Edwards</ns0:PersonName> 
     <ns0:Telephone>reinsurance_contract</ns0:Telephone> 
     <ns0:Email>[email protected]</ns0:Email> 
     </ns0:Contact> 
    </ns0:Broker> 
    <ns0:ServiceProvider> 
     <ns0:Party> 
     <ns0:Id Agency="DUNS_dun_and_bradstreet">239195295</ns0:Id> 
     </ns0:Party> 
    </ns0:ServiceProvider> 
    <ns0:Contract> 
     <ns0:ContractName>FINCO QUOTA SHARE TREATY</ns0:ContractName> 
     <ns0:ContractGroupName>BFG FINCO QUOTA SHARE TREATY</ns0:ContractGroupName> 
     <ns0:ContractType>0</ns0:ContractType> 
     <ns0:BrokerReference>B110813BDO1053</ns0:BrokerReference> 
     <ns0:BrokerGroupReference>200153436</ns0:BrokerGroupReference> 
     <ns0:BrokerRiskReference>13BDO1053</ns0:BrokerRiskReference> 
    </ns0:Contract> 
    <ns0:Endorsement> 
     <ns0:EndorsementReference>0</ns0:EndorsementReference> 
    </ns0:Endorsement> 
    <ns0:ContractSection ContractReportingLevel="section_level"> 
     <ns0:HighLevelReference>01</ns0:HighLevelReference> 
     <ns0:CoverType>quota_share</ns0:CoverType> 
     <ns0:ContractPeriod> 
     <ns0:StartDate DateIndicator="Jan 1 2013 12:00AM" /> 
     <ns0:EndDate DateIndicator="Dec 31 2013 12:00AM" /> 
     </ns0:ContractPeriod> 
     <ns0:UnderwritingYear>Jan </ns0:UnderwritingYear> 
     <ns0:ContractSectionClass> 
     <ns0:JvClassOfBusiness>wind_storm_unspecified</ns0:JvClassOfBusiness> 
     <ns0:ClassOfBusinessDescription>wind_storm_unspecified</ns0:ClassOfBusinessDescription> 
     </ns0:ContractSectionClass> 
     <ns0:RiskLocation> 
     <ns0:Address> 
      <ns0:Country /> 
     </ns0:Address> 
     <ns0:Location> 
      <ns0:Country>BS</ns0:Country> 
     </ns0:Location> 
     </ns0:RiskLocation> 
     <ns0:ContractAmountsCurrency> 
     <ns0:Ccy>GBP</ns0:Ccy> 
     </ns0:ContractAmountsCurrency> 
     <ns0:BrokerSharePercentage> 
     <ns0:Rate RateUnit="percentage">2.500000</ns0:Rate> 
     </ns0:BrokerSharePercentage> 
     <ns0:OrderPercentage> 
     <ns0:Rate RateUnit="percentage">41.000000</ns0:Rate> 
     </ns0:OrderPercentage> 
     <ns0:LinesPercentageOfOrderIndicator>false</ns0:LinesPercentageOfOrderIndicator> 
     <ns0:Brokerage> 
     <ns0:BrokeragePercentage> 
      <ns0:Rate RateUnit="percentage">2.500000</ns0:Rate> 
     </ns0:BrokeragePercentage> 
     </ns0:Brokerage> 
     <ns0:ContractMarket> 
     <ns0:Reinsurer> 
      <ns0:Party> 
      <ns0:Id Agency="DUNS_dun_and_bradstreet">239195295</ns0:Id> 
      </ns0:Party> 
     </ns0:Reinsurer> 
     <ns0:ParticipantFunction>leader</ns0:ParticipantFunction> 
     <ns0:ReinsurerQuoteMaximumSharePercentage> 
      <ns0:Rate RateUnit="percentage">100.000000</ns0:Rate> 
     </ns0:ReinsurerQuoteMaximumSharePercentage> 
     <ns0:ReinsurerWrittenPercentage> 
      <ns0:Rate RateUnit="percentage">100.000000</ns0:Rate> 
     </ns0:ReinsurerWrittenPercentage> 
     </ns0:ContractMarket> 
    </ns0:ContractSection> 
    </ns0:Placing> 
</ns0:Root> 

我遇到的問題是與節點 它應屬於<ns0:ContractSection>下,代替它的是來到的父節點下這是<ns0:Placing>

+1

請編輯問題並提供一個完整的(但很小的)源XML文檔。還提供一個完整的轉換(如果可能,小)。在目前的狀態下,這不是一個真正的問題。 – 2013-05-12 23:17:13

+0

@DimitreNovatchev好的。對於疏忽感到抱歉。我已經添加了樣式表和一個示例xml。讓我知道。謝謝 – JohnXsl 2013-05-13 00:40:34

+0

請澄清:這是*「XML輸出」*你*要求*,或者它是你目前得到的需要修復? – Borodin 2013-05-13 15:31:11

回答

1

這個樣式表似乎做你需要什麼,雖然有些點不清楚。

需要注意的是布爾表達式的字符串值是truefalse,使因爲這是你想要的,你可以只使用value-of代替choose/when/otherwise因爲我在這裏做。我也將訂單百分比的價值提取到變量中,以使表達式不太難看。

由於新LinesPercentageOfOrderIndicator元素取決於兩個PlacingTransactionFunctionOrderPercentage/Rate我認爲,這就是爲什麼這兩個原始元素的輸出不見了,所以我編寫這種方式。但我不清楚爲什麼ReinsurerContractReference元素不在您想要的輸出中,並且如果這是一個錯誤,那麼您只需刪除與此元素相匹配的最終模板並將其恢復。

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:ns0="http://somenamespace" 
    exclude-result-prefixes="msxsl"> 

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

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 

    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="ns0:Placing"> 

    <xsl:variable name="percentage-rate" select="ns0:ContractSection/ns0:OrderPercentage/ns0:Rate"/> 

    <xsl:copy> 
     <ns0:ContractSection> 
     <ns0:LinesPercentageOfOrderIndicator> 
      <xsl:value-of select="ns0:PlacingTransactionFunction = 'signed_line_advice' and $percentage-rate > 100"/> 
     </ns0:LinesPercentageOfOrderIndicator> 
     <xsl:apply-templates select="ns0:ContractSection/ns0:ContractMarket"/> 
     </ns0:ContractSection> 
    </xsl:copy> 

    </xsl:template> 

    <xsl:template match="ns0:ReinsurerContractReference"/> 

</xsl:stylesheet> 

輸出

<ns0:Root xmlns:ns0="http://somenamespace"> 
    <ns0:Placing> 
     <ns0:ContractSection> 
     <ns0:LinesPercentageOfOrderIndicator>false</ns0:LinesPercentageOfOrderIndicator> 
     <ns0:ContractMarket> 
      <ns0:Reinsurer> 
       <ns0:Party> 
        <ns0:Id Agency="DUNS_dun_and_bradstreet">239195295</ns0:Id> 
       </ns0:Party> 
      </ns0:Reinsurer> 
      <ns0:ParticipantFunction>leader</ns0:ParticipantFunction> 
      <ns0:ReinsurerContractReference/> 
      <ns0:ReinsurerQuoteMaximumSharePercentage> 
       <ns0:Rate RateUnit="percentage">100.000000</ns0:Rate> 
      </ns0:ReinsurerQuoteMaximumSharePercentage> 
      <ns0:ReinsurerWrittenPercentage> 
       <ns0:Rate RateUnit="percentage">100.000000</ns0:Rate> 
      </ns0:ReinsurerWrittenPercentage> 
     </ns0:ContractMarket> 
     </ns0:ContractSection> 
    </ns0:Placing> 
</ns0:Root> 

更新

除了你已經擁有的東西,你需要一個模板OrderPercentage是將自身複製後立即添加新LinesPercentageOfOrderIndicator

這應該可以做到。

<xsl:template match="ns0:OrderPercentage"> 
    <xsl:copy> 
    <xsl:apply-templates/> 
    </xsl:copy> 
    <ns0:LinesPercentageOfOrderIndicator> 
    <xsl:value-of select="ancestor::ns0:Placing/ns0:PlacingTransactionFunction = 'signed_line_advice' and ns0:Rate > 100"/> 
    </ns0:LinesPercentageOfOrderIndicator> 
</xsl:template> 

我注意到你有很多方法來檢查一個元素是否爲空。有

  • not(node()),這是 「沒有孩子」

  • element = ''其分析元素作爲一個字符串

  • not(element != '')這是相同的,但更多的混亂!

,後兩者的工作需要從文本節點中去掉任何微不足道的空間,或者使用在<xsl:strip-space elements="*"/>樣式表的頂部,或者通過對在測試中使用的值normalize-space。但如果表達式的值爲空字符串,則表達式計算結果爲false,因此您可以只寫not(element)來測試它是否爲空。

另外你有這些

ns0:Reinsurer[not(ns0:Party/ns0:Id != '') and not(ns0:Reinsurer/ns0:Party/ns0:Id/@Agency[. != ''])] 

ns0:ServiceProvider[not(ns0:Party/ns0:Id != '') and not(ns0:Reinsurer/ns0:Party/ns0:Id/@Agency[. != ''])] 

看起來像他們應該

ns0:ServiceProvider[not(ns0:Party/ns0:Id or ns0:Party/ns0:Id/@Agency)] 

ns0:ServiceProvider[not(ns0:Party/ns0:Id or ns0:Party/ns0:Id/@Agency)] 

,因爲丟失雙重底片是很好的,並且ns0:Reinsurer元素或ns0:ServiceProvider元素的子元素都沒有ns0:Reinsurer元素。 (目前還不清楚你的數據,ns0:ServiceProvider可以有任何孩子,但顯然我不能確定。)

+0

我試圖插入,但沒有工作。因爲我沒有放置完整版本的XSLT和XML。因此,請清楚我已經上傳完整的XSLT和XML的困惑。 – JohnXsl 2013-05-13 03:58:54

+1

@JohnXsl:好的我已經添加到我的解決方案中,爲您提供一個簡單的附加模板,以滿足您的要求。 – Borodin 2013-05-13 17:22:09

+0

感謝您的解決方案和您的建議。對不起,我忙於其他的東西,並沒有得到機會實施你的建議。我會整合你的建議,並會讓你知道。謝謝 – JohnXsl 2013-05-14 04:17:22