如何使用XSLT基于百分比计算值

时间:2016-12-20 12:08:45

标签: xml xslt-2.0

我在弄清楚如何修改Amount的值时遇到了问题。

XML示例:

<Test>

 <Person UniqueId= "e7f9a90e-f72f-4483-90db-1bfc"></Person>
 <Person UniqueId= "c5a420bc-f918-46f1-9c45-40009eede805"></Person>

 <RealEstateAsset UniqueID="c3b35acd-3535-4e66-9159-7ccc7284b623" Transaction="Owns">
    <PercentOwned Proportions="Specified">
      <Owner Percent="50" x_Party="e7f9a90e-f72f-4483-90db-1bfc" />
      <Owner Percent="50" x_Party="c5a420bc-f918-46f1-9c45-40009eede805" />
    </PercentOwned>
    <RentalIncome RentalAmount="99999999999"></RentalIncome>
  </RealEstateAsset>
</Test>

输出信息:

<Applicant>
  <ID>e7f9a90e-f72f-4483-90db-1bfc</ID>
   ...
  <Income>
     <Amount>99999999999</Amount>
     <AssetIntegrationId>c3b35acd-3535-4e66-9159-7ccc7284b623</AssetIntegrationId>
     <Comments>Rental Income</Comments>
     <IncomeType>Rental Income - Existing</IncomeType>
  </Income>

  ...
 </Applicant>

 <Applicant>
  <ID>5a420bc-f918-46f1-9c45-40009eede805</ID>
   ...
  <Income>
     <Amount>99999999999</Amount>
     <AssetIntegrationId>c3b35acd-3535-4e66-9159-7ccc7284b623</AssetIntegrationId>
     <Comments>Rental Income</Comments>
     <IncomeType>Rental Income - Existing</IncomeType>
  </Income>
 ...
 </Applicant>

我想根据百分比输出金额50%。

<Amount>49999999999.5</Amount>

包含现有XSLT代码的示例:

 <xsl:template match="Person">
    <xsl:variable name="x_UniqueId" select="@UniqueId"/>

    <Applicant>

        <ID>
            <xsl:value-of select="@UniqueId"/>
        </ID>

        <xsl:apply-templates select="../RealEstateAsset/RentalIncome[../PercentOwned/Owner[not(@Percent = '0')]/@x_Party = $x_UniqueId]"/>
    </Applicant>
</xsl:template>



<xsl:template match="RentalIncome">
    <Income>
        <Amount_Test2>
            <xsl:variable name="x_Party" select="../PercentOwned/Owner/@x_Party"/>
            <xsl:variable name="Percent" select="../PercentOwned/Owner[@x_Party = $GlobalVar]/@Percent"/>

            <xsl:value-of select="@RentalAmount div (100 div $Percent)"/>
        </Amount_Test2>

        <Amount>
            <xsl:value-of select="@RentalAmount"/>
        </Amount>

        <AssetIntegrationId>
            <xsl:value-of select="../@UniqueID"/>
        </AssetIntegrationId>

        <Comments>
            <xsl:value-of select="fn:InsertSpace(local-name())"/><!-- Ignore this -->
        </Comments>

        <IncomeType>
            <xsl:choose>
                <xsl:when test="../@Transaction = ('Owns', 'Sold' )">Rental Income - Existing</xsl:when>
                <xsl:when test="../@Transaction = 'Purchasing'">Rental Income - New</xsl:when>
            </xsl:choose>
        </IncomeType>

    </Income>
</xsl:template>

在Amount_Test2中,我试图根据全局变量计算所需的信息,但这不起作用,因为总是会有两个所有者。 我猜我需要在Person模板中执行此操作吗?

1 个答案:

答案 0 :(得分:2)

您可以将此人的id属性作为参数传递,您也可以使用键来跟踪交叉引用:

<xsl:key name="owner" match="RealEstateAsset" use="PercentOwned/Owner/@x_Party"/>
<xsl:key name="asset-owner" match="PercentOwned/Owner/@Percent" use="../@x_Party"/>

<xsl:template match="Person">

    <Applicant>

        <ID>
            <xsl:value-of select="@UniqueId"/>
        </ID>

        <xsl:apply-templates select="key('owner', @UniqueId)">
            <xsl:with-param name="person-id" select="@UniqueId"/>
        </xsl:apply-templates>
    </Applicant>
</xsl:template>

<xsl:template match="RealEstateAsset">
    <xsl:param name="person-id"/>
    <Income>
        <Amount_Test2>

            <xsl:value-of select="RentalIncome/@RentalAmount div (100 div key('asset-owner', $person-id, current()))"/>
        </Amount_Test2>

        <Amount>
            <xsl:value-of select="@RentalAmount"/>
        </Amount>

        <AssetIntegrationId>
            <xsl:value-of select="@UniqueID"/>
        </AssetIntegrationId>


    </Income>
</xsl:template>