Сумма числа из текста с использованием xslt 1.0 - PullRequest
0 голосов
/ 04 мая 2020

Проблема на самом деле, я должен проверить, если CommandList / GetBookingDetails / SupplierConfirmationCancelDataItemList / SupplierconfirmationCancelDataItem / Name = 'Refund Amount Breakdown', то нужно получить значение из CommandList / GetBookingDetails / SupplierConfirmationCancelDataItata_ImageLemageLextList числовые значения c и их сумма.

XML

<?xml version="1.0" encoding="UTF-8"?>
<CommandList>
   <GetBookingDetails>
    <SupplierConfirmationCancelDataItemList>
        <SupplierConfirmationCancelDataItem>
           <Name>Paid Amount</Name>
           <Value>110.59 EUR</Value>
        </SupplierConfirmationCancelDataItem>
        <SupplierConfirmationCancelDataItem>
           <Name>Refund Amount</Name>
           <Value>110.59 EUR</Value>
        </SupplierConfirmationCancelDataItem>
        <SupplierConfirmationCancelDataItem>
            <Name>Refund Amount Breakdown</Name>
            <Value>YQ: LH YQ surcharge: 10.00 EUR; GB: United Kingdom Air Passenger Duty APD: 15.46 EUR; RA: Germany Passenger Service Charge International Departure: 26.59 EUR; DE: Germany Airport Security Charge: 9.50 EUR; OY: Germany Air Transport Tax: 7.38 EUR; UB: United Kingdom Passenger Service Charge Departures: 20.70 EUR</Value>
        </SupplierConfirmationCancelDataItem>
    </SupplierConfirmationCancelDataItemList>   
  </GetBookingDetails>
</CommandList>

XSLT, где я работаю Третья строка соответствует = "/" существует, потому что она использовалась для других тегов, которые я здесь не упомянул.

<?xml version="1.0" encoding="utf-8"?>

<xsl:template match="CommandList/GetBookingDetails/SupplierConfirmationCancelDataItemList/SupplierconfirmationCancelDataItem/Value">
    <total>
        <xsl:call-template name="total">
            <xsl:with-param name="string" select="concat(., '; ')"/>
        </xsl:call-template>
    </total>
</xsl:template>


<xsl:template name="total">
    <xsl:param name="string"/>
    <xsl:param name="sum" select="0"/>
    <xsl:choose>
        <xsl:when test="contains($string, ';')">
            <xsl:variable name="token" select="substring-before($string, '; ')" />
            <xsl:variable name="amount" select="substring-before(substring-after(substring-after($token, ': '), ': '), ' EUR')" />
            <xsl:call-template name="total">
                <xsl:with-param name="string" select="substring-after($string, '; ')" />
                <xsl:with-param name="sum" select="$sum + $amount" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$sum"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:template>
</xsl:stylesheet>

Результат должен быть 89,63

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...