Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
94 views
in Technique[技术] by (71.8m points)

XSLT 3.0 assertion and exception throwing after iteration

I have XSLT module containing dependent components: iteration, validation and …

Input example port1.xml

<valuationDocument>
  <valuationSet id="val1">
    <assetValuation>
      <objectReference href="tid-1" />
      <quote>
        <value>1000000</value>
        <sensitivitySet>
          <sensitivity name="SEP21">0.707</sensitivity>
          <sensitivity name="2Y">1.82</sensitivity>
          <sensitivity name="3Y">2.73</sensitivity>
          <sensitivity name="4Y">3.68</sensitivity>
          <sensitivity name="5Y">4.51</sensitivity>
          <sensitivity name="7Y">3.64</sensitivity>
          <sensitivity name="10Y">9.06</sensitivity>
          <sensitivity name="MAR21">0.315</sensitivity>
          <sensitivity name="15Y">13.59</sensitivity>
        </sensitivitySet>
      </quote>
    </assetValuation>
    <assetValuation>
      <objectReference href="tid-2" />
      <quote>
        <value>1100000</value>
        <sensitivitySet>
          <sensitivity name="SEP21">0.947</sensitivity>
          <sensitivity name="MAR21">0.502</sensitivity>
          <sensitivity name="2Y">1.91</sensitivity>
          <sensitivity name="3Y">2.84</sensitivity>
          <sensitivity name="4Y">3.72</sensitivity>
          <sensitivity name="5Y">4.63</sensitivity>
          <sensitivity name="7Y">3.71</sensitivity>
          <sensitivity name="10Y">9.15</sensitivity>
          <sensitivity name="15Y">13.64</sensitivity>
        </sensitivitySet>
      </quote>
    </assetValuation>
  </valuationSet>
</valuationDocument>

I am quasi-satisfied with the iteration component (incapacitating the streaming…). The iteration result is below:

<valuationDocument>
   <valuationSet id="val1">
      <assetValuation>
         <objectReference href="tid-1"/>
         <quote>
            <value>1000000</value>
            <BasisPointValue>
               <shiftUp units="100bp" sensitivityName="SEP21" sensitivity="0.707">992,930.00</shiftUp>
               <shiftDown units="100bp" sensitivityName="SEP21" sensitivity="0.707">1,007,070.00</shiftDown>
==========================================================================================
               <shiftUp units="100bp" sensitivityName="15Y" sensitivity="13.59">864,100.00</shiftUp>
               <shiftDown units="100bp" sensitivityName="15Y" sensitivity="13.59">1,135,900.00</shiftDown>
            </BasisPointValue>
            <sensitivitySet>
               <sensitivity name="SEP21">0.707</sensitivity>
               <sensitivity name="2Y">1.82</sensitivity>
               <sensitivity name="3Y">2.73</sensitivity>
               <sensitivity name="4Y">3.68</sensitivity>
               <sensitivity name="5Y">4.51</sensitivity>
               <sensitivity name="7Y">3.64</sensitivity>
               <sensitivity name="10Y">9.06</sensitivity>
               <sensitivity name="MAR21">0.315</sensitivity>
               <sensitivity name="15Y">13.59</sensitivity>
            </sensitivitySet>
         </quote>
      </assetValuation>
      <assetValuation>
        <objectReference href="tid-2" />
           <quote>
              <value>1100000</value>
            <BasisPointValue>
               <shiftUp units="100bp" sensitivityName="SEP21" sensitivity="0.947">1,089,583.00</shiftUp>
               <shiftDown units="100bp" sensitivityName="SEP21" sensitivity="0.947">1,110,417.00</shiftDown>
==========================================================================================
               <shiftUp units="100bp" sensitivityName="15Y" sensitivity="13.64">949,960.00</shiftUp>
               <shiftDown units="100bp" sensitivityName="15Y" sensitivity="13.64">1,250,040.00</shiftDown>
            </BasisPointValue>
            <sensitivitySet>
               <sensitivity name="SEP21">0.947</sensitivity>
               <sensitivity name="MAR21">0.502</sensitivity>
               <sensitivity name="2Y">1.91</sensitivity>
               <sensitivity name="3Y">2.84</sensitivity>
               <sensitivity name="4Y">3.72</sensitivity>
               <sensitivity name="5Y">4.63</sensitivity>
               <sensitivity name="7Y">3.71</sensitivity>
               <sensitivity name="10Y">9.15</sensitivity>
               <sensitivity name="15Y">13.64</sensitivity>
            </sensitivitySet>
         </quote>
      </assetValuation>
   </valuationSet>
</valuationDocument>

I would like to validate the iteration result as soon as it completes. Should any of the validation fail, the exception is reported and the program is terminated. However, the validation ISN'T played out after the iteration. (The actual validation is to raise exception through xsl:try|catch and terminate if any of the assertion fails)

The validation logic is to compare three metrics of the sensitivitySet/sensitivity with those of iterated & transformed BasisPointValue/shiftUp|shiftDown/@sensitivity (NOTE: xsl:text is to simulate exception throwing, apparently, the XSLT editor can’t emit the message)

    <xsl:variable name="MinE">
        <!-- <xsl:message terminate="yes">Fatal Error: Minimum NPV and BPV sensitivity is different</xsl:message>-->
        <xsl:text>&#x9;&#xA;</xsl:text>
        <xsl:text>Retro: Min NPV and BPV sensitivity</xsl:text>
        <xsl:text>&#x9;&#xA;</xsl:text>
    </xsl:variable> 
    <xsl:variable name="MaxE">
        <!--<xsl:message terminate="yes">Fatal Error: Maximum NPV and BPV sensitivity is different</xsl:message>-->
        <xsl:text>&#x9;&#xA;</xsl:text>
        <xsl:text>Retro: Max NPV and BPV sensitivity</xsl:text>
        <xsl:text>&#x9;&#xA;</xsl:text>
    </xsl:variable> 
    <xsl:variable name="AvgDownE">
        <!--<xsl:message terminate="yes">Fatal Error: Average NPV and BPV ShiftDown sensitivity is different</xsl:message>-->
        <xsl:text>&#x9;&#xA;</xsl:text>
        <xsl:text>Retro: Average NPV and BPV ShiftDown</xsl:text>
        <xsl:text>&#x9;&#xA;</xsl:text>
    </xsl:variable>
    
    <xsl:template match="/">
        <xsl:source-document href="port1.xml">
             <xsl:apply-templates select="valuationDocument" mode="val"/>
        </xsl:source-document>
    </xsl:template>

    <xsl:template match="sensitivitySet" mode="val">

<!--  ITERATION -->     
    ==========================================
        <!--        <xsl:apply-imports/>-->
         <xsl:copy-of select="."/>
        <xsl:apply-templates select="parent::quote" mode="validation"/>
    </xsl:template>
  
<!--  VALIDATION -->
    <xsl:template match="quote" mode="validation" expand-text="yes">
        <Assertion>
             <xsl:variable name="m" as="map(*)">
                <xsl:map>
                   <xsl:map-entry key="'min-npv'" select="min(outermost(descendant::sensitivity)/number())"/>
                   <xsl:map-entry key="'max-npv'" select="max(outermost(descendant::sensitivity)/number())"/>
                   <xsl:map-entry key="'min-bpv'" select="min(outermost(descendant::BasisPointValue//@sensitivity)/number())"/>
                   <xsl:map-entry key="'max-bpv'" select="max(outermost(descendant::BasisPointValue//@sensitivity)/number())"/>
                   <xsl:map-entry key="'avg-npv'" select="avg(outermost(descendant::sensitivity)/number())"/>
                   <xsl:map-entry key="'avg-bpv-down'" select="avg(outermost(descendant::BasisPointValue/shiftDown/@sensitivity)/number())"/>
                 </xsl:map>
              </xsl:variable>
                    <xsl:choose>
                        <xsl:when test="deep-equal($m('min-npv'), $m('min-bpv'))">
                            <xsl:value-of select="$MinE"/>
                        </xsl:when>
                        <xsl:when test="deep-equal($m('max-npv'), $m('max-bpv'))">
                            <xsl:value-of select="$MaxE"/>
                        </xsl:when>
                        <xsl:when test="deep-equal($m('avg-npv'), $m('avg-bpv-down'))">
                            <xsl:value-of select="$AvgDownE"/>
                        </xsl:when>
                        <xsl:otherwise><xsl:value-of select="'Validation Succeeds!'"/></xsl:otherwise>
                    </xsl:choose>
        </Assertion> 
    </xsl:template>

What is the XSLT3.0 solution of validation after the iteration?

question from:https://stackoverflow.com/questions/65558051/xslt-3-0-assertion-and-exception-throwing-after-iteration

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I understand the question correctly, this is a standard multi-phase transformation. You can either use two separate stylesheets, chained together using some external API (e.g. shell script, Ant/gradle, XProc, Java application) or you can put both phases in the same stylesheet using the logic

<xsl:variable name="temp">
  <xsl:apply-templates select="/" mode="phase-1"/>
</xsl:variable>
<xsl:apply-templates select="$temp" mode="phase-2"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...