XSLT版本1 .net编译器有限选项

huangapple go评论53阅读模式
英文:

XSLT Version 1 .net compiler limited options

问题

a. Here is a modified version of V1 without using the key function:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes" omit-xml-declaration="yes" method="xml"/>
    
    <xsl:template match="/">
        <PremiumSplit>
            <xsl:apply-templates select="//VehicleCategory"/>
        </PremiumSplit>
    </xsl:template>
    
    <xsl:template match="VehicleCategory">
        <xsl:variable name="category" select="."/>
        <xsl:if test="not(preceding::VehicleCategory[. = current()])">
            <xsl:if test="//Cargo/Exposures/Vehicle/CoverageProvided">
                <xsl:apply-templates select="//Cargo/Exposures/Vehicle/CoverageProvided">
                    <xsl:with-param name="category" select="$category"/>
                </xsl:apply-templates>
            </xsl:if>
            <xsl:if test="//AutoLiability/Exposures/Vehicle/CoverageProvided">
                <xsl:apply-templates select="//AutoLiability/Exposures/Vehicle/CoverageProvided">
                    <xsl:with-param name="category" select="$category"/>
                </xsl:apply-templates>
            </xsl:if>
        </xsl:if>
    </xsl:template>
    
    <xsl:template match="Cargo/Exposures/Vehicle/CoverageProvided">
        <xsl:param name="category"/>
        <xsl:variable name="coverageCode" select="CoverageCode"/>
        <xsl:variable name="categoryPremium" select="sum(//Cargo/Exposures/Vehicle[VehicleSequenceNo = current()/../../VehicleSequenceNo]/CoverageProvided[CoverageCode = $coverageCode]/Premium)"/>
        <xsl:variable name="totalPremium" select="sum(//Cargo/Exposures/Vehicle/CoverageProvided[CoverageCode = $coverageCode]/Premium)"/>
        <xsl:if test="(generate-id() = generate-id(//Cargo/Exposures/Vehicle/CoverageProvided[CoverageCode = $coverageCode][1])) and not(preceding::Vehicle[CoverageSection = 'Cargo' and CoverageCode = $coverageCode and VehicleCategory = $category]) and $categoryPremium != 0">
            <Vehicle>
                <CoverageSection>Cargo</CoverageSection>
                <xsl:copy-of select="$coverageCode"/>
                <CoveragePercent>
                    <xsl:value-of select="$totalPremium div $categoryPremium"/>
                </CoveragePercent>
                <xsl:copy-of select="$category"/>
            </Vehicle>
        </xsl:if>
    </xsl:template>
    
    <xsl:template match="AutoLiability/Exposures/Vehicle/CoverageProvided">
        <xsl:param name="category"/>
        <xsl:variable name="coverageCode" select="CoverageCode"/>
        <xsl:variable name="categoryPremium" select="sum(//AutoLiability/Exposures/Vehicle[VehicleSequenceNo = current()/../../VehicleSequenceNo]/CoverageProvided[CoverageCode = $coverageCode]/Premium)"/>
        <xsl:variable name="totalPremium" select="sum(//AutoLiability/Exposures/Vehicle/CoverageProvided[CoverageCode = $coverageCode]/Premium)"/>
        <xsl:if test="(generate-id() = generate-id(//AutoLiability/Exposures/Vehicle/CoverageProvided[CoverageCode = $coverageCode][1])) and not(preceding::Vehicle[CoverageSection = 'AutoLiability' and CoverageCode = $coverageCode and VehicleCategory = $category]) and $categoryPremium != 0">
            <Vehicle>
                <CoverageSection>AutoLiability</CoverageSection>
                <xsl:copy-of select="$coverageCode"/>
                <CoveragePercent>
                    <xsl:value-of select="$totalPremium div $categoryPremium"/>
                </CoveragePercent>
                <xsl:copy-of select="$category"/>
            </Vehicle>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

b. To fix V2, you need to modify the matching conditions to group coverage codes by category correctly. Here's a revised version of V2:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes" omit-xml-declaration="yes" method="xml"/>

    <xsl:template match="/">
        <PremiumSplit>
            <xsl:apply-templates select="//VehicleCategory"/>
        </PremiumSplit>
    </xsl:template>

    <xsl:template match="VehicleCategory">
        <xsl:variable name="category" select="."/>
        <xsl:if test="not(preceding::VehicleCategory[. = current()])">
            <AutoLiability>
                <xsl:apply-templates select="//AutoLiability/Exposures/Vehicle/CoverageProvided">
                    <xsl:with-param name="category" select="$category"/>
                </xsl:apply-templates>
            </AutoLiability>
            <Cargo>
                <xsl:apply-templates select="//Cargo/Exposures/Vehicle/CoverageProvided">
                    <xsl:with-param name="category" select="$category"/>
                </xsl:apply-templates>
            </Cargo>
        </xsl:if>
    </xsl:template>

    <xsl:template match="Cargo/Exposures/Vehicle/CoverageProvided">
        <xsl:param name="category"/>
        <xsl:variable name="coverageCode" select="CoverageCode"/>
        <xsl:variable name="totalPremium" select="sum(//Cargo/Exposures/Vehicle/CoverageProvided[CoverageCode = $coverageCode]/Premium)"/>
        <xsl:variable name="categoryPremium" select="sum(//Cargo/Exposures/Vehicle[../VehicleSequenceNo = current()/../VehicleSequenceNo]/CoverageProvided[CoverageCode = $coverageCode]/Premium)"/>
        <xsl:if test="(generate-id() = generate-id(//Cargo/Exposures/Vehicle/CoverageProvided[CoverageCode = $coverageCode][1])) and $categoryPremium != 0">
            <Vehicle>
                <CoverageSection>Cargo</CoverageSection>
                <xsl:copy-of select="$coverageCode"/>
                <CoveragePercent>
                    <xsl:value-of select="$totalPremium div $categoryPremium"/>
                </CoveragePercent>
                <xsl:copy-of select="$category"/>
            </Vehicle>
        </xsl:if>
    </xsl:template>

    <xsl:template match="AutoLiability/Exposures/Vehicle/CoverageProvided">
        <xsl:param name="category"/>
        <xsl:variable name="coverageCode" select="CoverageCode"/>
        <xsl:variable

<details>
<summary>英文:</summary>

write xslt code in xslt version 1 without using key function for given input xml (reason - .net compiler cannot accept key function or a variable under use attribute of key function)

    &lt;Root&gt;
    	&lt;ExternalFulfillmentRequest&gt;
    		&lt;Vehicles&gt;
    			&lt;Vehicle&gt;
    				&lt;VehicleSequenceNo&gt;1&lt;/VehicleSequenceNo&gt;
    				&lt;VehicleCategory&gt;Tractor&lt;/VehicleCategory&gt;
    			&lt;/Vehicle&gt;
    			&lt;Vehicle&gt;
    				&lt;VehicleSequenceNo&gt;2&lt;/VehicleSequenceNo&gt;
    				&lt;VehicleCategory&gt;Tractor&lt;/VehicleCategory&gt;
    			&lt;/Vehicle&gt;			
    			&lt;Vehicle&gt;
    				&lt;VehicleSequenceNo&gt;3&lt;/VehicleSequenceNo&gt;
    				&lt;VehicleCategory&gt;Trailer&lt;/VehicleCategory&gt;
    			&lt;/Vehicle&gt;
    		&lt;/Vehicles&gt;
    		&lt;Policies&gt;
    			&lt;Policy&gt;
    				&lt;PrimaryAuto&gt;
    					&lt;AutoLiability&gt;
    						&lt;Exposures&gt;
    							&lt;Vehicle&gt;
    								&lt;VehicleSequenceNo&gt;1&lt;/VehicleSequenceNo&gt;
    								&lt;CoverageProvided&gt;
    									&lt;CoverageCode&gt;L1&lt;/CoverageCode&gt;
    									&lt;Premium&gt;100&lt;/Premium&gt;
    								&lt;/CoverageProvided&gt;								
    							&lt;/Vehicle&gt;	
    							&lt;Vehicle&gt;
    								&lt;VehicleSequenceNo&gt;2&lt;/VehicleSequenceNo&gt;
    								&lt;CoverageProvided&gt;
    									&lt;CoverageCode&gt;L1&lt;/CoverageCode&gt;
    									&lt;Premium&gt;200&lt;/Premium&gt;
    								&lt;/CoverageProvided&gt;								
    							&lt;/Vehicle&gt;								
    							&lt;Vehicle&gt;
    								&lt;VehicleSequenceNo&gt;3&lt;/VehicleSequenceNo&gt;
    								&lt;CoverageProvided&gt;
    									&lt;CoverageCode&gt;L1&lt;/CoverageCode&gt;
    									&lt;Premium&gt;150&lt;/Premium&gt;
    								&lt;/CoverageProvided&gt;								
    							&lt;/Vehicle&gt;							
    						&lt;/Exposures&gt;
    					&lt;/AutoLiability&gt;
    					&lt;Cargo&gt;
    						&lt;Exposures&gt;
    							&lt;Vehicle&gt;
    								&lt;VehicleSequenceNo&gt;1&lt;/VehicleSequenceNo&gt;
    								&lt;CoverageProvided&gt;
    									&lt;CoverageCode&gt;L1&lt;/CoverageCode&gt;
    									&lt;Premium&gt;100&lt;/Premium&gt;
    								&lt;/CoverageProvided&gt;
    							&lt;/Vehicle&gt;
    							&lt;Vehicle&gt;
    								&lt;VehicleSequenceNo&gt;2&lt;/VehicleSequenceNo&gt;
    								&lt;CoverageProvided&gt;
    									&lt;CoverageCode&gt;L1&lt;/CoverageCode&gt;
    									&lt;Premium&gt;200&lt;/Premium&gt;
    								&lt;/CoverageProvided&gt;
    							&lt;/Vehicle&gt;								
    							&lt;Vehicle&gt;
    								&lt;VehicleSequenceNo&gt;3&lt;/VehicleSequenceNo&gt;
    								&lt;CoverageProvided&gt;
    									&lt;CoverageCode&gt;L1&lt;/CoverageCode&gt;
    									&lt;Premium&gt;150&lt;/Premium&gt;
    								&lt;/CoverageProvided&gt;
    							&lt;/Vehicle&gt;							
    						&lt;/Exposures&gt;
    					&lt;/Cargo&gt;
    				&lt;/PrimaryAuto&gt;
    			&lt;/Policy&gt;
    		&lt;/Policies&gt;
    	&lt;/ExternalFulfillmentRequest&gt;
    &lt;/Root&gt;

output should look as below

    &lt;PremiumSplit&gt;
    	&lt;AutoLiability&gt;
    	   &lt;Vehicle&gt;
    		  &lt;CoverageSection&gt;AutoLiability&lt;/CoverageSection&gt;
    		  &lt;CoverageCode&gt;L1&lt;/CoverageCode&gt;
    		  &lt;!-- L1 premium across CovergaeSections 100 + 200 + 150 = 450; L1 premium for VehicleCategory tractor 100 + 200 = 300; 450 / 300 = 1.5 --&gt;
    		  &lt;CoveragePercent&gt;1.5&lt;/CoveragePercent&gt;
    		  &lt;VehicleCategory&gt;Tractor&lt;/VehicleCategory&gt;
    	   &lt;/Vehicle&gt;
    	   &lt;Vehicle&gt;
    		  &lt;CoverageSection&gt;AutoLiability&lt;/CoverageSection&gt;
    		  &lt;CoverageCode&gt;L1&lt;/CoverageCode&gt;
    		  &lt;!-- L1 premium across CovergaeSections 100 + 200 + 150 = 450; L1 premium for VehicleCategory trailer 150; 450 / 150 = 3 --&gt;
    		  &lt;CoveragePercent&gt;3&lt;/CoveragePercent&gt;
    		  &lt;VehicleCategory&gt;Trailer&lt;/VehicleCategory&gt;
    	   &lt;/Vehicle&gt; 
    	&lt;/AutoLiability&gt;
    	&lt;Cargo&gt;
    		&lt;Vehicle&gt;
    			&lt;CoverageSection&gt;Cargo&lt;/CoverageSection&gt;
    			&lt;CoverageCode&gt;L1&lt;/CoverageCode&gt;
    			&lt;CoveragePercent&gt;1.5&lt;/CoveragePercent&gt;
    			&lt;VehicleCategory&gt;Tractor&lt;/VehicleCategory&gt;
    		&lt;/Vehicle&gt;
    		&lt;Vehicle&gt;
    			&lt;CoverageSection&gt;Cargo&lt;/CoverageSection&gt;
    			&lt;CoverageCode&gt;L1&lt;/CoverageCode&gt;
    			&lt;CoveragePercent&gt;3&lt;/CoveragePercent&gt;
    			&lt;VehicleCategory&gt;Trailer&lt;/VehicleCategory&gt;
    		&lt;/Vehicle&gt;
    	&lt;/Cargo&gt;
    &lt;/PremiumSplit&gt;

aslo xslt should perform below
1. Group covergaecode based on coverageCode, vehiclecategory per coverageSection
2. sum up premium per coverageCode
3. sum up premium per coverageCode and vehiclecategory per coverageSection
4. calculate CoveragePercent i.e. premium split percent based on point 2 and 3 mentioned above, for CoveragePercent avoid divde by 0 i.e. replace infinity with 0 else actual CoveragePercent
5. output the same coveragecode only once per vehiclecategory per coveragesection


I have written 2 versions 
V1 - works as expected in online xslt test tools . But compiler throws exception as .net do not support key function under use attribute if match attribute as well deined. i.e. these 2 keys are not acceptable for the compiler 
&lt;xsl:key name=&quot;al-coverage-per-category&quot;/&gt; and 
&lt;xsl:key name=&quot;cg-coverage-per-category&quot;/&gt;

V1 Code -

    &lt;xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; version=&quot;1.0&quot;&gt;
    	&lt;xsl:output indent=&quot;yes&quot; omit-xml-declaration=&quot;yes&quot; method=&quot;xml&quot;/&gt;
    	&lt;xsl:key name=&quot;vehicle&quot; match=&quot;Vehicles/Vehicle&quot; use=&quot;VehicleSequenceNo&quot;/&gt;
    	&lt;xsl:key name=&quot;al-coverage-per-code&quot; match=&quot;AutoLiability/Exposures/Vehicle/CoverageProvided&quot; use=&quot;CoverageCode&quot;/&gt;
    	&lt;xsl:key name=&quot;al-coverage-per-category&quot; match=&quot;AutoLiability/Exposures/Vehicle/CoverageProvided&quot; use=&quot;key(&#39;vehicle&#39;,../VehicleSequenceNo)/VehicleCategory&quot;/&gt;
    	&lt;xsl:key name=&quot;cg-coverage-per-code&quot; match=&quot;Cargo/Exposures/Vehicle/CoverageProvided&quot; use=&quot;CoverageCode&quot;/&gt;
    	&lt;xsl:key name=&quot;cg-coverage-per-category&quot; match=&quot;Cargo/Exposures/Vehicle/CoverageProvided&quot; use=&quot;key(&#39;vehicle&#39;,../VehicleSequenceNo)/VehicleCategory&quot;/&gt;
    	&lt;xsl:template match=&quot;/&quot;&gt;
    		&lt;PremiumSplit&gt;
    			&lt;xsl:apply-templates select=&quot;//VehicleCategory&quot;/&gt;
    		&lt;/PremiumSplit&gt;
    	&lt;/xsl:template&gt;
    	&lt;xsl:template match=&quot;VehicleCategory&quot;&gt;
    		&lt;xsl:if test=&quot;not(preceding::VehicleCategory[.=current()])&quot;&gt;
    			&lt;xsl:if test=&quot;//Cargo/Exposures/Vehicle/CoverageProvided&quot;&gt;
    				&lt;xsl:apply-templates select=&quot;//Cargo/Exposures/Vehicle/CoverageProvided&quot;&gt;
    					&lt;xsl:with-param name=&quot;category&quot; select=&quot;.&quot;/&gt;
    				&lt;/xsl:apply-templates&gt;
    			&lt;/xsl:if&gt;
    			&lt;xsl:if test=&quot;//AutoLiability/Exposures/Vehicle/CoverageProvided&quot;&gt;
    				&lt;xsl:apply-templates select=&quot;//AutoLiability/Exposures/Vehicle/CoverageProvided&quot;&gt;
    					&lt;xsl:with-param name=&quot;category&quot; select=&quot;.&quot;/&gt;
    				&lt;/xsl:apply-templates&gt;
    			&lt;/xsl:if&gt;
    		&lt;/xsl:if&gt;
    	&lt;/xsl:template&gt;
    	&lt;xsl:template match=&quot;Cargo/Exposures/Vehicle/CoverageProvided&quot;&gt;
    		&lt;xsl:param name=&quot;category&quot;/&gt;
    		&lt;xsl:if test=&quot;(generate-id() = generate-id(key(&#39;cg-coverage-per-code&#39;,CoverageCode)[1])) and not(preceding::Vehicle[CoverageSection=&#39;Cargo&#39; and CoverageCode=current()/CoverageCode and VehicleCategory=$category]) and key(&#39;cg-coverage-per-category&#39;,$category)[CoverageCode=current()/CoverageCode]&quot;&gt;
    			&lt;xsl:variable name=&quot;cg-total-premium-per-code&quot; select=&quot;sum(key(&#39;cg-coverage-per-code&#39;,CoverageCode)/Premium)&quot;/&gt;
    			&lt;xsl:variable name=&quot;cg-category-premium-per-code&quot; select=&quot;sum(key(&#39;cg-coverage-per-category&#39;,$category)[CoverageCode=current()/CoverageCode]/Premium)&quot;/&gt;
    			&lt;Vehicle&gt;
    				&lt;CoverageSection&gt;Cargo&lt;/CoverageSection&gt;
    				&lt;xsl:copy-of select=&quot;CoverageCode&quot;/&gt;
    				&lt;CoveragePercent&gt;
    					&lt;xsl:choose&gt;
    						&lt;xsl:when test=&quot;$cg-category-premium-per-code != 0&quot;&gt;
    							&lt;xsl:value-of select=&quot;$cg-total-premium-per-code div $cg-category-premium-per-code&quot;/&gt;
    						&lt;/xsl:when&gt;
    						&lt;xsl:otherwise&gt;
    							&lt;xsl:value-of select=&quot;0&quot;/&gt;
    						&lt;/xsl:otherwise&gt;
    					&lt;/xsl:choose&gt;
    				&lt;/CoveragePercent&gt;
    				&lt;xsl:copy-of select=&quot;$category&quot;/&gt;
    			&lt;/Vehicle&gt;
    		&lt;/xsl:if&gt;
    	&lt;/xsl:template&gt;
    	&lt;xsl:template match=&quot;AutoLiability/Exposures/Vehicle/CoverageProvided&quot;&gt;
    		&lt;xsl:param name=&quot;category&quot;/&gt;
    		&lt;xsl:if test=&quot;(generate-id() = generate-id(key(&#39;al-coverage-per-code&#39;,CoverageCode)[1])) and not(preceding::Vehicle[CoverageSection=&#39;AutoLiability&#39; and CoverageCode=current()/CoverageCode and VehicleCategory=$category]) and key(&#39;al-coverage-per-category&#39;,$category)[CoverageCode=current()/CoverageCode]&quot;&gt;
    			&lt;xsl:variable name=&quot;al-total-premium-per-code&quot; select=&quot;sum(key(&#39;al-coverage-per-code&#39;,CoverageCode)/Premium)&quot;/&gt;
    			&lt;xsl:variable name=&quot;al-category-premium-per-code&quot; select=&quot;sum(key(&#39;al-coverage-per-category&#39;,$category)[CoverageCode=current()/CoverageCode]/Premium)&quot;/&gt;
    			&lt;Vehicle&gt;
    				&lt;CoverageSection&gt;AutoLiability&lt;/CoverageSection&gt;
    				&lt;xsl:copy-of select=&quot;CoverageCode&quot;/&gt;
    				&lt;CoveragePercent&gt;
    					&lt;xsl:choose&gt;
    						&lt;xsl:when test=&quot;$al-category-premium-per-code != 0&quot;&gt;
    							&lt;xsl:value-of select=&quot;$al-total-premium-per-code div $al-category-premium-per-code&quot;/&gt;
    						&lt;/xsl:when&gt;
    						&lt;xsl:otherwise&gt;
    							&lt;xsl:value-of select=&quot;0&quot;/&gt;
    						&lt;/xsl:otherwise&gt;
    					&lt;/xsl:choose&gt;
    				&lt;/CoveragePercent&gt;
    				&lt;xsl:copy-of select=&quot;$category&quot;/&gt;
    			&lt;/Vehicle&gt;
    		&lt;/xsl:if&gt;
    	&lt;/xsl:template&gt;
    &lt;/xsl:stylesheet&gt;

where as V2 code - without using Key function,it do not generate desired output. Current it add same coveragecode for all vehicles defined. But expectation is to have only one coveragecode per vehiclecategoryin output. Please refer sample output pasted.

    &lt;xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; version=&quot;1.0&quot;&gt;
        &lt;xsl:output indent=&quot;yes&quot; omit-xml-declaration=&quot;yes&quot; method=&quot;xml&quot;/&gt;
        &lt;xsl:template match=&quot;/&quot;&gt;
            &lt;PremiumSplit&gt;
                &lt;AutoLiability&gt;
                    &lt;xsl:apply-templates select=&quot;//AutoLiability/Exposures/Vehicle/CoverageProvided&quot;/&gt;
                &lt;/AutoLiability&gt;
                &lt;Cargo&gt;
                    &lt;xsl:apply-templates select=&quot;//Cargo/Exposures/Vehicle/CoverageProvided&quot;/&gt;
                &lt;/Cargo&gt;
            &lt;/PremiumSplit&gt;
        &lt;/xsl:template&gt;
        &lt;xsl:template match=&quot;AutoLiability/Exposures/Vehicle/CoverageProvided&quot;&gt;
            &lt;xsl:variable name=&quot;vehicleSeqNo&quot; select=&quot;../VehicleSequenceNo&quot;/&gt;
            &lt;xsl:variable name=&quot;coverageCode&quot; select=&quot;CoverageCode&quot;/&gt;
            &lt;xsl:variable name=&quot;vehicleCategory&quot; select=&quot;//Vehicles/Vehicle[VehicleSequenceNo=$vehicleSeqNo]/VehicleCategory&quot;/&gt;
            &lt;xsl:variable name=&quot;totalPremiumPerCode&quot; select=&quot;sum(//AutoLiability/Exposures/Vehicle/CoverageProvided[CoverageCode=$coverageCode]/Premium)&quot;/&gt;
            &lt;xsl:variable name=&quot;categoryPremiumPerCode&quot; select=&quot;sum(//AutoLiability/Exposures/Vehicle[VehicleSequenceNo=$vehicleSeqNo]/CoverageProvided[CoverageCode=$coverageCode]/Premium)&quot;/&gt;
            &lt;Vehicle&gt;
                &lt;CoverageSection&gt;AutoLiability&lt;/CoverageSection&gt;
                &lt;xsl:copy-of select=&quot;$coverageCode&quot;/&gt;
                &lt;CoveragePercent&gt;
    				&lt;xsl:choose&gt;
    					&lt;xsl:when test=&quot;$categoryPremiumPerCode != 0&quot;&gt;
    						&lt;xsl:value-of select=&quot;$totalPremiumPerCode div $categoryPremiumPerCode&quot;/&gt;
    					&lt;/xsl:when&gt;
    					&lt;xsl:otherwise&gt;
    						&lt;xsl:value-of select=&quot;0&quot;/&gt;
    					&lt;/xsl:otherwise&gt;
    				&lt;/xsl:choose&gt;
                &lt;/CoveragePercent&gt;
                &lt;xsl:copy-of select=&quot;$vehicleCategory&quot;/&gt;
            &lt;/Vehicle&gt;
        &lt;/xsl:template&gt;
        &lt;xsl:template match=&quot;Cargo/Exposures/Vehicle/CoverageProvided&quot;&gt;
            &lt;xsl:variable name=&quot;vehicleSeqNo&quot; select=&quot;../VehicleSequenceNo&quot;/&gt;
            &lt;xsl:variable name=&quot;coverageCode&quot; select=&quot;CoverageCode&quot;/&gt;
            &lt;xsl:variable name=&quot;vehicleCategory&quot; select=&quot;//Vehicles/Vehicle[VehicleSequenceNo=$vehicleSeqNo]/VehicleCategory&quot;/&gt;
            &lt;xsl:variable name=&quot;totalPremiumPerCode&quot; select=&quot;sum(//Cargo/Exposures/Vehicle/CoverageProvided[CoverageCode=$coverageCode]/Premium)&quot;/&gt;
            &lt;xsl:variable name=&quot;categoryPremiumPerCode&quot; select=&quot;sum(//Cargo/Exposures/Vehicle[VehicleSequenceNo=$vehicleSeqNo]/CoverageProvided[CoverageCode=$coverageCode]/Premium)&quot;/&gt;
            &lt;Vehicle&gt;
                &lt;CoverageSection&gt;Cargo&lt;/CoverageSection&gt;
                &lt;xsl:copy-of select=&quot;$coverageCode&quot;/&gt;
                &lt;CoveragePercent&gt;
    				&lt;xsl:choose&gt;
    					&lt;xsl:when test=&quot;$categoryPremiumPerCode != 0&quot;&gt;
    						&lt;xsl:value-of select=&quot;$totalPremiumPerCode div $categoryPremiumPerCode&quot;/&gt;
    					&lt;/xsl:when&gt;
    					&lt;xsl:otherwise&gt;
    						&lt;xsl:value-of select=&quot;0&quot;/&gt;
    					&lt;/xsl:otherwise&gt;
    				&lt;/xsl:choose&gt;
                &lt;/CoveragePercent&gt;
                &lt;xsl:copy-of select=&quot;$vehicleCategory&quot;/&gt;
            &lt;/Vehicle&gt;
        &lt;/xsl:template&gt;
    &lt;/xsl:stylesheet&gt;

Question: 
a. Either rewrite V1 by removing key function 
b. fix v2 version for desired output.
c. please help me with new version which can give desired output without using key function.
I spent almost 30 hours to get here. Please help.

</details>


# 答案1
**得分**: 0

Saxon .NET 旨在在 .NET 环境下运行 XSLT 2 或 3。

至于您的第一个 XSLT 1.0 版本,它看起来很复杂,但如果您将键的定义更改为

&lt;xsl:key name=&quot;vehicle&quot; match=&quot;Vehicles/Vehicle&quot; use=&quot;VehicleSequenceNo&quot;/&gt;
&lt;xsl:key name=&quot;al-coverage-per-code&quot; match=&quot;AutoLiability/Exposures/Vehicle/CoverageProvided&quot; use=&quot;CoverageCode&quot;/&gt;
&lt;xsl:key name=&quot;al-coverage-per-category&quot; match=&quot;AutoLiability/Exposures/Vehicle/CoverageProvided&quot; use=&quot;//Vehicles/Vehicle[VehicleSequenceNo = current()/../VehicleSequenceNo]/VehicleCategory&quot;/&gt;
&lt;xsl:key name=&quot;cg-coverage-per-code&quot; match=&quot;Cargo/Exposures/Vehicle/CoverageProvided&quot; use=&quot;CoverageCode&quot;/&gt;
&lt;xsl:key name=&quot;cg-coverage-per-category&quot; match=&quot;Cargo/Exposures/Vehicle/CoverageProvided&quot; use=&quot;//Vehicles/Vehicle[VehicleSequenceNo = current()/../VehicleSequenceNo]/VehicleCategory&quot;/&gt;

Microsoft 的 XSLT 1.0 处理器实现应该能够处理这些键。我没有尝试理解或简化代码的其余部分。

<details>
<summary>英文:</summary>

Saxon .NET exists to run XSLT 2 or 3 under .NET.

As for your first XSLT 1.0 version, it looks convoluted but if you change the key definitions to 

    &lt;xsl:key name=&quot;vehicle&quot; match=&quot;Vehicles/Vehicle&quot; use=&quot;VehicleSequenceNo&quot;/&gt;
    &lt;xsl:key name=&quot;al-coverage-per-code&quot; match=&quot;AutoLiability/Exposures/Vehicle/CoverageProvided&quot; use=&quot;CoverageCode&quot;/&gt;
    &lt;xsl:key name=&quot;al-coverage-per-category&quot; match=&quot;AutoLiability/Exposures/Vehicle/CoverageProvided&quot; use=&quot;//Vehicles/Vehicle[VehicleSequenceNo = current()/../VehicleSequenceNo]/VehicleCategory&quot;/&gt;
    &lt;xsl:key name=&quot;cg-coverage-per-code&quot; match=&quot;Cargo/Exposures/Vehicle/CoverageProvided&quot; use=&quot;CoverageCode&quot;/&gt;
    &lt;xsl:key name=&quot;cg-coverage-per-category&quot; match=&quot;Cargo/Exposures/Vehicle/CoverageProvided&quot; use=&quot;//Vehicles/Vehicle[VehicleSequenceNo = current()/../VehicleSequenceNo]/VehicleCategory&quot;/&gt;

Microsoft&#39;s XSLT 1.0 processor implementations should be able to handle the keys. I haven&#39;t made any attempt to understand or simplify the rest of the code.

</details>



huangapple
  • 本文由 发表于 2023年5月23日 01:37:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定