首页 \ 问答 \ 在XSLT 2.0中对变量进行分组(Grouping of variables in XSLT 2.0)

在XSLT 2.0中对变量进行分组(Grouping of variables in XSLT 2.0)

对于具有相同<CtryCd>值的每个<LineDetail>节点,并使用<Amt>的总和填充1 <LineDetail> <Amt> 。 但是,我还需要检查<SI><TI>节点。 求和的输出标记将基于<SI><TI>节点。 如果<SI><TI>的值均为假,则生成<Amt2> ,而如果<SI>值为真且<TI>为假,则生成<Amt2> 。 我创建了一个XSLT并且已经得到了总结。 但是我的输出中仍然缺少一些东西。

这是一个示例XML:

<Record>
<Data>
    <Process>
        <Header>
            <ID>22-BBB</ID>
            <Date>2017-02-14</Date>
            <ContactName>Abegail</ContactName>
            <!-- some other elements -->
        </Header>
        <Detail>
            <ID>22-CCC</ID>
            <RequestedDate>2017-02-14</RequestedDate>
            <!-- some other elements -->
            <LineDetail>
                <CtryCd>AF</CtryCd>
                <SI>false</SI>
                <TI>false</TI>
                <Amt>11.11</Amt>
            </LineDetail>
            <LineDetail>
                <CtryCd>SE</CtryCd>
                <SI>true</SI>
                <TI>false</TI>
                <Amt>22.22</Amt>
            </LineDetail>
            <LineDetail>
                <CtryCd>AF</CtryCd>
                <SI>false</SI>
                <TI>false</TI>
                <Amt>33.33</Amt>
            </LineDetail>
            <LineDetail>
                <CtryCd>AF</CtryCd>
                <SI>true</SI>
                <TI>false</TI>
                <Amt>55.55</Amt>
            </LineDetail>       
        </Detail>
    </Process>
</Data>
</Record>

生成的输出:

<Record>
<Data>
    <Process>
        <Header>
            <ID>22-BBB</ID>
            <Date>2017-02-14</Date>
            <ContactName>Abegail</ContactName>
            <!-- some other elements -->
        </Header>
        <Detail>
            <LineDetail>
                <CtryCd>AF</CtryCd>
                <Amt1>99.99</Amt1>
            </LineDetail>
        </Detail>
        <Detail>
            <LineDetail>
                <C<Amt2>22.22</Amt2>
            </LineDetail>
        </Detail>
    </Process>
</Data>
</Record>

预期产量:

<Record>
<Data>
    <Process>
        <Header>
            <ID>22-BBB</ID>
            <Date>2017-02-14</Date>
            <ContactName>Abegail</ContactName>
            <!-- some other elements -->
        </Header>
        <Detail>
            <ID>22-CCC</ID>
            <RequestedDate>2017-02-14</RequestedDate>
            <!-- some other elements -->
            <LineDetail>
                <CtryCd>AF</CtryCd>
                <Amt1>44.44</<Amt1>
                <Amt2>55.55</Amt2>
            </LineDetail>
            <LineDetail>
                <CtryCd>SE</CtryCd>
                <Amt1>0</<Amt1>
                <Amt2>22.22</Amt2>
            </LineDetail>           
        </Detail>
    </Process>
</Data>
</Record>

XSLT:

<xsl:stylesheet version="2.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:key name="CtryCd" match="LineDetail" use="CtryCd"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="Detail">
    <xsl:for-each-group select="LineDetail" group-by="CtryCd">
        <Detail>
            <LineDetail>
                <CtryCd>
                    <xsl:value-of select="CtryCd"/>
                </CtryCd>
                <xsl:if test="lower-case(SI)='false' and lower-case(TI)='false'">
                    <Amt1>
                        <xsl:value-of select="sum(current-group()/Amt)"/>
                    </Amt1>
                <xsl:if test="lower-case(SI)='true' and lower-case(TI)='false'">
                    <Amt2>
                        <xsl:value-of select="sum(current-group()/Amt)"/>
                    </Amt2>
                </xsl:if>
                </xsl:if>
                <xsl:if test="lower-case(SI)='true' and lower-case(TI)='false'">
                    <Amt2>
                        <xsl:value-of select="sum(current-group()/Amt)"/>
                    </Amt2>
                </xsl:if>
            </LineDetail>
        </Detail>
    </xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>

For each <LineDetail> node that has the same value of <CtryCd> and populates 1 <LineDetail> with the summation of <Amt>. But, I need to check also the <SI> and <TI> node. The output tag of the summation will be based from the <SI> and <TI> node. The <Amt1> will generate if the value of <SI> and <TI> are both false, while <Amt2> will generate if the value of <SI> is true and <TI> is false. I created an XSLT and already got the summation. But there's still something missing in my output.

This is a sample XML:

<Record>
<Data>
    <Process>
        <Header>
            <ID>22-BBB</ID>
            <Date>2017-02-14</Date>
            <ContactName>Abegail</ContactName>
            <!-- some other elements -->
        </Header>
        <Detail>
            <ID>22-CCC</ID>
            <RequestedDate>2017-02-14</RequestedDate>
            <!-- some other elements -->
            <LineDetail>
                <CtryCd>AF</CtryCd>
                <SI>false</SI>
                <TI>false</TI>
                <Amt>11.11</Amt>
            </LineDetail>
            <LineDetail>
                <CtryCd>SE</CtryCd>
                <SI>true</SI>
                <TI>false</TI>
                <Amt>22.22</Amt>
            </LineDetail>
            <LineDetail>
                <CtryCd>AF</CtryCd>
                <SI>false</SI>
                <TI>false</TI>
                <Amt>33.33</Amt>
            </LineDetail>
            <LineDetail>
                <CtryCd>AF</CtryCd>
                <SI>true</SI>
                <TI>false</TI>
                <Amt>55.55</Amt>
            </LineDetail>       
        </Detail>
    </Process>
</Data>
</Record>

Generated output:

<Record>
<Data>
    <Process>
        <Header>
            <ID>22-BBB</ID>
            <Date>2017-02-14</Date>
            <ContactName>Abegail</ContactName>
            <!-- some other elements -->
        </Header>
        <Detail>
            <LineDetail>
                <CtryCd>AF</CtryCd>
                <Amt1>99.99</Amt1>
            </LineDetail>
        </Detail>
        <Detail>
            <LineDetail>
                <C<Amt2>22.22</Amt2>
            </LineDetail>
        </Detail>
    </Process>
</Data>
</Record>

Expected output:

<Record>
<Data>
    <Process>
        <Header>
            <ID>22-BBB</ID>
            <Date>2017-02-14</Date>
            <ContactName>Abegail</ContactName>
            <!-- some other elements -->
        </Header>
        <Detail>
            <ID>22-CCC</ID>
            <RequestedDate>2017-02-14</RequestedDate>
            <!-- some other elements -->
            <LineDetail>
                <CtryCd>AF</CtryCd>
                <Amt1>44.44</<Amt1>
                <Amt2>55.55</Amt2>
            </LineDetail>
            <LineDetail>
                <CtryCd>SE</CtryCd>
                <Amt1>0</<Amt1>
                <Amt2>22.22</Amt2>
            </LineDetail>           
        </Detail>
    </Process>
</Data>
</Record>

XSLT:

<xsl:stylesheet version="2.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:key name="CtryCd" match="LineDetail" use="CtryCd"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="Detail">
    <xsl:for-each-group select="LineDetail" group-by="CtryCd">
        <Detail>
            <LineDetail>
                <CtryCd>
                    <xsl:value-of select="CtryCd"/>
                </CtryCd>
                <xsl:if test="lower-case(SI)='false' and lower-case(TI)='false'">
                    <Amt1>
                        <xsl:value-of select="sum(current-group()/Amt)"/>
                    </Amt1>
                <xsl:if test="lower-case(SI)='true' and lower-case(TI)='false'">
                    <Amt2>
                        <xsl:value-of select="sum(current-group()/Amt)"/>
                    </Amt2>
                </xsl:if>
                </xsl:if>
                <xsl:if test="lower-case(SI)='true' and lower-case(TI)='false'">
                    <Amt2>
                        <xsl:value-of select="sum(current-group()/Amt)"/>
                    </Amt2>
                </xsl:if>
            </LineDetail>
        </Detail>
    </xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>

原文:https://stackoverflow.com/questions/42240723
更新时间:2023-08-01 06:08

最满意答案

它都是非常实现的定义,但通常,编译器会将Derived

Base1: vptr
Base1: data
Base2: vptr
Base2: data
Derived: data

Derived的地址是整体的地址,因此物理地址与Base1地址相同。 Derived的vtable的初始部分与Base1的初始部分相同,而Base1:vptr实际上将指向Derived的vtable。

如果在声明中反转Base1Base2 ,编译器将在上面反转它们的角色。 (通常。标准中没有任何内容可以阻止编译器按字母顺序排列基数,或者将派生数据放在第一位,但我从来没有听说过编译器会这样做。)


It's all very implementation defined, but typically, a compiler will lay out Derived as

Base1: vptr
Base1: data
Base2: vptr
Base2: data
Derived: data

The address of the Derived is the address of the whole, thus the same physical address as that of Base1. The initial part of the vtable for Derived will be identical with that of Base1, and the Base1: vptr will in fact point to the vtable of Derived.

If you inverse Base1 and Base2 in the declaration, the compiler will inverse their roles above. (Typically. There's nothing in the standard which would prevent the compiler from arranging the bases in alphabetical order, or putting the derived data first, but I've never heard of a compiler which did so.)

相关问答

更多
  • 它的实现已定义。 C ++ 11在标准的实现数量部分给出了建议的最小值: - 直接和间接基类[16 384]。 - 单个类的直接基类[1 024]。 [...] - 一个班级的直接和间接虚拟基地[1 024]。 我会说这非常慷慨。 It's implementation defined. C++11 gives recommended minimums in the Implementation quantities section of the standard: — Direct and indirec ...
  • 调用这种“多重继承”可能会让人感到困惑,因为多重继承是面向对象的编程问题,而C语言中不存在这种问题。 在我看来,您的困难可能是您正在尝试#include可执行代码(即.c文件),而不是链接.c文件和#including标头( .h )文件,这些文件为.c的函数提供声明。文件。 Calling this "multiple inheritance" may be confusing because multiple inheritance is an object-oriented programming i ...
  • Objective-C不支持多重继承,因此这是不可能的。 继承是一种“is-a”关系,一个类只能通过继承拥有一个is-a关系。 您可以通过协议(Java或C#parlace中的接口,或C ++中的完全抽象类)拥有多个is-a关系。 您可以使用is-a关系来模拟“has-a”关系,而不是is-a关系。 任何对象都可以有任意数量的has-a关系。 在实践中,您只需要拥有所需的每个类的一个实例变量。 例如: @interface A : NSObject { @private B* _b; C ...
  • 回答NO。 在java中只存在简单继承。 public class A { // Extends from `Object` class } class B extends A { // Extends from `A` } class C extends B { // Extends from `B` } 你在这里有什么 C是B,B是A,A是对象 (C是传递性的A) 多重继承 C is a B and also is a D, D is an object, B is a A , A is an ...
  • Java(与C ++不同)不允许多 态 继承 状态 ,因此不会遭受钻石问题的困扰 。 它允许通过接口多种类型的继承(一个类可以实现多个接口)。 从Java 8开始,通过接口中的default方法还有多个行为继承。 Java (unlike C++) does not allow multiple inheritance of state and, therefore, does not suffer from a diamond problem. It allows multiple inheritance ...
  • 有一件事,你可以使用多重继承for mixins 。 您可以使用mixins为在另一个类(要混合的类)中定义的类添加功能。 一个特别关于C ++中mixin的链接: MixinsForCeePlusPlus One thing that you can use multiple inheritance for is for mixins. You can use mixins to add functionality to a class that is defined in another class ( ...
  • 您不需要任何函数,因为C只从A和B派生一次。除非您多次从A或B派生(没有虚拟继承),否则您只需要使用: A *pbb = pc; B *pba = pc; AtoC和BtoC只有通过以下方式才能安全: C *c = dynamic_cast(a_or_b_pointer); You don't need any function, since C only derives once from A and B. Unless you derive from A or B multiple time ...
  • “多重继承通常是代码设计错误的标志” - 纯接口的父级不计入此规则。 您的I*类是纯接口(仅包含纯虚函数),因此您可以在这方面使用Digital*Point类 "multiple inheritance is typically a sign of a bad code design" - parents that are pure interfaces are not counted in regards to this rule. Your I* classes are pure interfaces ...
  • 它都是非常实现的定义,但通常,编译器会将Derived为 Base1: vptr Base1: data Base2: vptr Base2: data Derived: data Derived的地址是整体的地址,因此物理地址与Base1地址相同。 Derived的vtable的初始部分与Base1的初始部分相同,而Base1:vptr实际上将指向Derived的vtable。 如果在声明中反转Base1和Base2 ,编译器将在上面反转它们的角色。 (通常。标准中没有任何内容可以阻止编译器按字母顺序排 ...
  • 多重继承并不难添加,这么多语言不包含它的原因是因为它有点深奥和混乱。 多重继承通常会混淆代码,导致封装不良,并使代码行为不可预测。 通常,在代码中允许多重继承只会导致大量错误的编程来自该语言,通常会导致语言声誉不佳。 如果由于某种原因需要多重继承,那么一个聪明到能够正确使用它的编码器很可能足够聪明地编写自己的方法来实现它。 Multiple inheritance is not hard to add, the reason why so many languages don't include it is ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)