XSLT向当前节点添加新元素

时间:2013-05-12 22:42:58

标签: xslt element add parent

我有一个xml,我正在尝试添加一个新元素,然后在某些条件下分配值。 一切都很好。 但是,它似乎将新元素添加到父节点。 有人可以帮我解决问题。

以下是完整的 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>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输出:

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

我遇到的问题是<ns0:LinesPercentageOfOrderIndicator>节点 它应该属于<ns0:ContractSection>而不是<ns0:ContractSection>的父节点<ns0:Placing>

1 个答案:

答案 0 :(得分:1)

这个样式表似乎可以满足您的需求,但有些观点尚不清楚。

请注意,布尔表达式的字符串值为truefalse,因此您可以使用value-of代替choose / { {1}} / when就像我在这里所做的那样。我还将订单百分比率的值拉出到一个变量中,这样表达式就不会太笨拙了。

由于新的otherwise元素同时依赖于LinesPercentageOfOrderIndicatorPlacingTransactionFunction,我认为这就是输出中缺少这两个原始元素的原因,所以我就这样编码了。但我不清楚为什么OrderPercentage/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:Reinsurer子元素。 (你不清楚ns0:ServiceProvider可以有任何孩子的数据,但显然我不能确定。)