汇总相关值 [英] Summing related values

查看:23
本文介绍了汇总相关值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 .xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<output>
    <orders>
        <order>
            <id>1</id>
            <number>10002</number>
            <type>Loading</type>
            <date>2013-01-01T02:30:00</date>
        </order>
        <order>
            <id>2</id>
            <number>10003</number>
            <type>Loading</type>
            <date>2013-01-01T010:30:00</date>
        </order>
        <order>
            <id>3</id>
            <number>10004</number>
            <type>Loaded</type>
            <date>2013-01-01T12:30:00</date>
        </order>
    </orders>
    <quantities>
        <quantity>
            <id_order>1</id_order>
            <unit>KG</unit>
            <value>1000</value>
        </quantity>
        <quantity>
            <id_order>1</id_order>
            <unit>PAL</unit>
            <value>3</value>
        </quantity>
        <quantity>
            <id_order>1</id_order>
            <unit>M3</unit>
            <value>1.5</value>
        </quantity>
        <quantity>
            <id_order>2</id_order>
            <unit>KG</unit>
            <value>2000</value>
        </quantity>
        <quantity>
            <id_order>2</id_order>
            <unit>PAL</unit>
            <value>4</value>
        </quantity>
        <quantity>
            <id_order>3</id_order>
            <unit>KG</unit>
            <value>5000</value> 
        </quantity>
    </quantities>
</output>

并且所需的转换类似于

<output>
    <amount>KG</amount>
        <total> "sum of all KG elements" </total>
    <amount>PAL</amount>
        <total> "sum of all PAL elements" </total>
    <amount>M3</amount>
        <total> "sum of all M3 elements" </total>
</output>

我尝试过类似的东西

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


<xsl:template match="/">
<output>
        <xsl:for-each select="output/quantities/quantity"> 
            <amount>
                <xsl:value-of select="unit"/>

                <xsl:for-each select="output/quantities/quantity/value">
                        <xsl:value-of select="sum(output/quantities/quantity/value)"/>   

                </xsl:for-each> 
            </amount>    
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

但我没有得到值的总和并且输出像这样被破坏

but I don't get the sum of values and the output is broken like this

<output>
  <amount>KG<total/></amount>
  <amount>PAL<total/></amount>
  <amount>M3<total/></amount>
  <amount>KG<total/></amount>
  <amount>PAL<total/></amount>
  <amount>KG<total/></amount>
</output>

我尝试了一些其他解决方法,但都没有达到我想要的输出.如果有人可以提出任何建议或以任何方式提供帮助,我将不胜感激.

I've tried few other workarounds but none worked to my desired output. If anyone could suggest anything or help in any way I would appreciate it.

现在我试图在第一个 xml 中添加另一个节点,在此之后调用应该基本上将复制值的值剥离以仅在几小时后显示 (hh:mm:ss).我设法创建了原始 xml 的副本并添加了额外的标签,但我不确定如何提取小时数据并显示在内部标签中.

Now Im trying to add another node, in the first xml, called after which should basically have value of copied value stripped to show only after hours (hh:mm:ss). I've managed to create a copy of the original xml and add the extra tag, but I'm not sure on how to extract the hour data and show in inside tags.

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


   <xsl:template match="@* | node()">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="date">
      <xsl:copy-of select="."/>
      <hour>
<xsl:comment> <xsl:copy-of select="substring(output/orders/order/date,11)"/> </xsl:comment>
       </hour>
   </xsl:template>

</xsl:stylesheet>

推荐答案

试试看:

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

<xsl:key name="qty" match="quantity" use="unit"/>  

<xsl:template match="/">
    <output>
        <amount>KG</amount>
        <total><xsl:value-of select="sum(key('qty', 'KG')/value)"/></total>
        <amount>PAL</amount>
        <total><xsl:value-of select="sum(key('qty', 'PAL')/value)"/></total>
        <amount>M3</amount>
        <total><xsl:value-of select="sum(key('qty', 'M3')/value)"/></total>
    </output>
</xsl:template>

</xsl:stylesheet>

请注意,这不是您可以选择的最佳输出格式;理想情况下,单位和数量应包含在一个公共元素中,而不仅仅是相邻的兄弟元素.

Note that this is not the best output format you could choose; ideally, the unit and the amount would be contained by a common element, not just adjacent siblings.

这篇关于汇总相关值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆