Pergunta

I have a xml and I am trying to add a new element then assign the value on certain conditions. Everything is working fine. However, it seems to adding the new element to the parent node. Can someone help me figure out the issue.

Below is the full XSLT. But I am having problem with the last template.

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

Source 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>test@ri3k.com</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>jenny.edwards@aonbenfield.com</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 output:

  <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>jenny.edwards@aonbenfield.com</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>

The problem I'm having is with <ns0:LinesPercentageOfOrderIndicator> node It should fall under <ns0:ContractSection> instead it is coming under parent node of <ns0:ContractSection> which is <ns0:Placing>

Foi útil?

Solução

This stylesheet seems to do what you need, although some points are unclear.

Note that the string value of a boolean expression is either true or false, so since that's what you want you can just use value-of instead of choose / when / otherwise as I have done here. I've also pulled out the value of the order percentage rate into a variable so that the expression isn't too ungainly.

Since the new LinesPercentageOfOrderIndicator element depends on both PlacingTransactionFunction and OrderPercentage/Rate I assume that's why both these original elements are missing from the output, so I've coded it that way. But I'm not clear why the ReinsurerContractReference element isn't in your desired output, and if that's a mistake then all you need to do is to remove the final template that matches just this element and it will be reinstated.

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

output

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

Update

In addition to what you already have, you need a template for OrderPercentage that adds the new LinesPercentageOfOrderIndicator immediately after copying itself.

This should do the trick.

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

I note that you have numerous ways of checking whether an element is empty. There is

  • not(node()), which is "has no children"

  • element = '' which evaluates the element as a string

  • not(element != '') which is the same, but more confusing!

For the latter two to work you need to strip any insignificant spaces from the text nodes, either using <xsl:strip-space elements="*"/> at the top of the stylesheet, or by using normalize-space on the value in the test. But it is neatest to use the fact that an expression evaluates to false if it is the empty string, so you can just write not(element) to test if it is empty.

In addition you have these

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

and

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

which look like they should be

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

and

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

because it is nice to lose the double negatives, and there is no ns0:Reinsurer child of either a ns0:Reinsurer element or a ns0:ServiceProvider element. (It isn't clear from you data that ns0:ServiceProvider can have any children at all, but obviously I can't be sure.)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top