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

Xml 如何使用XSLT基于百分比计算值,xml,xslt-2.0,Xml,Xslt 2.0,我在计算如何修改金额的值时遇到问题 XML示例: <Test> <Person UniqueId= "e7f9a90e-f72f-4483-90db-1bfc"></Person> <Person UniqueId= "c5a420bc-f918-46f1-9c45-40009eede805"></Person> <RealEstateAsset UniqueID="c3b35acd-3535-4e66-9159-7cc

我在计算如何修改金额的值时遇到问题

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>

e7f9a90e-f72f-4483-90db-1bfc
...
99999999999
c3b35acd-3535-4e66-9159-7ccc7284b623
租金收入
租金收入-现有
...
5a420bc-f918-46f1-9c45-40009eede805
...
99999999999
c3b35acd-3535-4e66-9159-7ccc7284b623
租金收入
租金收入-现有
...
我想输出基于百分比的金额,在这种情况下为50%

<Amount>49999999999.5</Amount>
4999999999.5
包含现有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模板中执行此操作?

您可以将Person的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>

谢谢。我使用了一个稍微修改过的版本,效果很好。我有一个问题:函数current()在这里用于匹配个人id UniqueID?不,我使用了
key('asset-owner',$person-id,current())
只搜索当前处理的
realestateaset
元素的子树,前提是输入中可能有多个子树。