简体   繁体   English

XSLT 1.0 慕尼黑分组

[英]XSLT 1.0 Muenchian Grouping

I've been working on some examples using the Muenchian method of group, and now have one working where by key and value for summing are siblings within the same parent node but now I'm looking at an XML input where the structure is slightly different.我一直在使用 Muenchian 组方法处理一些示例,现在有一个工作,其中通过键和求和值是同一父节点中的兄弟姐妹,但现在我正在查看 XML 输入,其中结构略有不同.

This is my input这是我的输入

<?xml version="1.0" encoding="UTF-8"?>
<ArrayList>
    <Item>
        <amounts>
            <amount>1000.00</amount>
        </amounts>
        <invoice>
            <customerId>1234</customerId>
        </invoice>
    </Item>
    <Item>
        <amounts>
            <amount>1500.00</amount>
        </amounts>
        <invoice>
            <customerId>7755</customerId>
        </invoice>
    </Item>
    <Item>
        <amounts>
            <amount>800.00</amount>
        </amounts>
        <invoice>
            <customerId>1018</customerId>
        </invoice>
    </Item>
</ArrayList>

And I want to get to我想去

<?xml version="1.0" encoding="UTF-8"?>
<customers>
    <customer>
        <customerID>1234</customerID>
        <totalAmount>1800</totalAmount>
    </customer>
    <customer>
        <customerID>7755</customerID>
        <totalAmount>1500</totalAmount>
    </customer>
</customers>

Initially I was getting my distinct customerId's OK but total amount was 0.00 and now playing around with it so long my XSL is no longer valid.最初,我得到了我独特的 customerId 的确定,但总量为 0.00,现在玩它这么久,我的 XSL 不再有效。 I was trying to avoid posting this until I'd managed to resolve it but I've become blind to it now and can't spot the problem.我试图避免发布此问题,直到我设法解决它,但我现在对此视而不见,无法发现问题。 Even once I have sorted that I know it won't give me what I want.即使我已经排序,我知道它不会给我我想要的。

This is my XSL as it stands:这是我的 XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes" />

<xsl:key name="group" match="Item" use="invoice/customerId" />

    <xsl:template match="ArrayList">
        <customers>
            <xsl:apply-templates select="Item[generate-id() = generate-id(key('group', invoice/customerId[1])]"/>
        </customers>
    </xsl:template>
    
    <xsl:template match="Item">
        <customer>
            <xsl:copy-of select="customerID"/>
            <totalAmount><xsl:value-of select="sum(key('group', invoice/customerId)/amounts/amount)" /></totalAmount>
        </customer>
    </xsl:template>

</xsl:stylesheet>

Thanks谢谢

There is a missing closing ) in the apply-templates expression, make that <xsl:apply-templates select="Item[generate-id() = generate-id(key('group', invoice/customerId[1]))]"/> . apply-templates 表达式中缺少 close ) ,使<xsl:apply-templates select="Item[generate-id() = generate-id(key('group', invoice/customerId[1]))]"/> .

On the other hand, Muenchian grouping as I suggested it on Twitter would be <xsl:apply-templates select="Item[generate-id() = generate-id(key('group', invoice/customerId)[1])]"/> .另一方面,我在 Twitter 上建议的 Muenchian 分组将是<xsl:apply-templates select="Item[generate-id() = generate-id(key('group', invoice/customerId)[1])]"/> .

Finally, in the context of an Item element you want eg <xsl:copy-of select="invoice/customerId"/> instead of <xsl:copy-of select="customerID"/> .最后,在您想要的Item元素的上下文中,例如<xsl:copy-of select="invoice/customerId"/>而不是<xsl:copy-of select="customerID"/>

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

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