简体   繁体   English

带有分组的XSLT 1.0计算

[英]XSLT 1.0 calculation with grouping

I am beginner in XSLT and using it to transform XML to XML through Java. 我是XSLT的初学者,并使用它通过Java将XML转换为XML。

Source XML: 源XML:

<Response>
    <Data>
        <Book name="A" value="1"/>
        <Book name="B" value="2"/>
        <Book name="C" value="1"/>
    </Data>
    <Result>
        <Prices>
            <Price type="A" value="100"/>
            <Price type="B" value="60"/>
            <Price type="C" value="40"/>
        </Prices>
    </Result>
</Response>

XSLT: XSLT:

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

    <xsl:template match="/">
        <xsl:element name="Books">
            <xsl:variable name="BookType" select="//@type" />
            <xsl:attribute name="Total">
                <xsl:value-of select="sum(//Price[@type=$BookType]/@value)"/>
            </xsl:attribute>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Output XML: 输出XML:

<Books Total="200"/>

Expected Output XML: 预期的输出XML:

<Books Total="260"/>

In source XML i receive no of book and its price but they are not relevant. 在源XML中,我没有收到任何书及其价格,但它们并不相关。

<Price> tag indicates the price of one book. <Price>标签表示一本书的价格。 I need to calculate the total price of all the books as below 我需要按以下方式计算所有书籍的总价

Price of one book x no of books
For A : 100 x 1 = 100
For B : 60 x 2  = 120
For C : 40 x 1  = 040
------------------------
Total Price is  = 260

Please help. 请帮忙。

The easiest approach would be to build up a temporary structure in a variable containing the "price * qty" values, then use the exslt:node-set extension function to sum those values 最简单的方法是在包含“ price * qty”值的变量中建立一个临时结构,然后使用exslt:node-set扩展函数对这些值求和

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:exslt="http://exslt.org/common"
                exclude-result-prefixes="exslt">
  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/">
    <xsl:variable name="subTotals">
      <xsl:for-each select="/Response/Data/Book">
        <totalPrice>
          <xsl:value-of select="@value *
            /Response/Result/Prices/Price[@type = current()/@name]/@value" />
        </totalPrice>
      </xsl:for-each>
    </xsl:variable>
    <Books Total="{sum(exslt:node-set($subTotals)/totalPrice)}" />
  </xsl:template>
</xsl:stylesheet>

It is possible to do this in pure XSLT 1.0 without using the extension function, but it's rather fiddly and involves passing an accumulator parameter down a chain of templates: 可能做到这一点纯XSLT 1.0不使用扩展功能,但它是相当繁琐的,涉及通过蓄能器参数下的模板链:

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

  <xsl:template match="/">
    <Books>
      <xsl:apply-templates select="/Response/Data/Book[1]" />
    </Books>
  </xsl:template>

  <xsl:template match="Book">
    <xsl:param name="runningTotal" select="0" />
    <xsl:variable name="thisBookTotal" select="@value *
       /Response/Result/Prices/Price[@type = current()/@name]/@value" />
    <xsl:choose>
      <xsl:when test="following-sibling::Book">
        <!-- There are more books, process the next one, passing an updated
             running total -->
        <xsl:apply-templates select="following-sibling::Book[1]">
          <xsl:with-param name="runningTotal" select="$runningTotal + $thisBookTotal" />
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <!-- There are no more books, produce the final total -->
        <xsl:attribute name="Total">
          <xsl:value-of select="$runningTotal + $thisBookTotal" />
        </xsl:attribute>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM