xslt 1.0使用复合键分组(在不同级别)

时间:2013-04-24 00:06:49

标签: xml xslt muenchian-grouping

我有一个转换,我试图记录一组扁平的交易细节,并在文件的末尾提供按招标类型和登记号分组的总数的总和。
记录一组单独的交易细节是很容易的部分,我有这个工作正常,但我正在努力与摘要部分。

麻烦的是,我不知道有多少不同的寄存器号,或者有多少招标类型(因此明确列出xslt中的静态过滤字符串的摘要是不行的) ,所以某种分组似乎是有序的。

还有一个扳手 - 我使用XSLT 1.0 ...

我尝试搞乱muenchian分组,但是在复合键要求之间(寄存器生活在与支付方式不同的级别)和我对muenchian方法和键如何工作的有限理解我不能似乎让它工作,但我认为它仍然可能是需要的技巧......

关于我如何使用muenchia的任何建议 - 神奇地让它发挥作用?

这是一个示例源文档:

<s0:SalesCollection xmlns:s0="http://mySourceSchema">
  <s0:Sale transactionnumber="1" register="1">
    <s0:Tender amount="1.11" paymentmethod="visa" />
    <s0:Tender amount="2.22" paymentmethod="mastercard" />
  </s0:Sale>
  <s0:Sale transactionnumber="2" register="1">
    <s0:Tender amount="5.55" paymentmethod="discover" />
    <s0:Tender amount="4.44" paymentmethod="visa" />
  </s0:Sale>
  <s0:Sale transactionnumber="1" register="2">
    <s0:Tender amount="9.99" paymentmethod="amex" />
    <s0:Tender amount="8.88" paymentmethod="visa" />
  </s0:Sale>
</s0:SalesCollection>

这就是我想要的(再次,我已经记录了[@ type ='detail']记录):

<ns0:root xmlns:ns0="http://myDestinationSchema">
  <ns0:record type="detail" transactionnumber="1" register="1" amount="1.11" paymentmethod="visa" />
  <ns0:record type="detail" transactionnumber="1" register="1" amount="2.22" paymentmethod="mastercard" />
  <ns0:record type="detail" transactionnumber="2" register="1" amount="5.55" paymentmethod="discover" />
  <ns0:record type="detail" transactionnumber="2" register="1" amount="4.44" paymentmethod="visa" />
  <ns0:record type="detail" transactionnumber="1" register="2" amount="9.99" paymentmethod="amex" />
  <ns0:record type="detail" transactionnumber="1" register="2" amount="8.88" paymentmethod="visa" />
  <ns0:record type="summary" register="1" amount="5.55" paymentmethod="visa" />
  <ns0:record type="summary" register="1" amount="2.22" paymentmethod="mastercard" />
  <ns0:record type="summary" register="1" amount="5.55" paymentmethod="discover" />
  <ns0:record type="summary" register="2" amount="9.99" paymentmethod="amex" />
  <ns0:record type="summary" register="2" amount="8.88" paymentmethod="visa" />
</ns0:root>

那么如何使用xslt 1.0创建按寄存器和paymentmethod分组的摘要记录?

2 个答案:

答案 0 :(得分:3)

此转化

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:s0="http://mySourceSchema"
 xmlns:ns0="http://myDestinationSchema" exclude-result-prefixes="s0">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kTendByTypeAndReg" match="s0:Tender"
  use="concat(../@register, '#', @paymentmethod)"/>

 <xsl:template match="/*">
  <ns0:root xmlns:ns0="http://myDestinationSchema">
   <xsl:apply-templates select="*/*"/>
   <xsl:apply-templates mode="group" select=
   "*/*[generate-id()
       =generate-id(key('kTendByTypeAndReg',
                        concat(../@register, '#', @paymentmethod))[1]
                        )
       ]"/>
  </ns0:root>
 </xsl:template>

 <xsl:template match="s0:Tender">
   <ns0:record type="detail" transactionnumber="{../@transactionnumber}"
       register="{../@register}" amount="{@amount}" paymentmethod="{@paymentmethod}" />
 </xsl:template>

 <xsl:template match="*" mode="group">
    <ns0:record type="summary" register="{../@register}" paymentmethod="{@paymentmethod}"
  amount="{sum(key('kTendByTypeAndReg',concat(../@register,'#',@paymentmethod))
                   /@amount)}"/>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档时:

<s0:SalesCollection xmlns:s0="http://mySourceSchema">
  <s0:Sale transactionnumber="1" register="1">
    <s0:Tender amount="1.11" paymentmethod="visa" />
    <s0:Tender amount="2.22" paymentmethod="mastercard" />
  </s0:Sale>
  <s0:Sale transactionnumber="2" register="1">
    <s0:Tender amount="5.55" paymentmethod="discover" />
    <s0:Tender amount="4.44" paymentmethod="visa" />
  </s0:Sale>
  <s0:Sale transactionnumber="1" register="2">
    <s0:Tender amount="9.99" paymentmethod="amex" />
    <s0:Tender amount="8.88" paymentmethod="visa" />
  </s0:Sale>
</s0:SalesCollection>

产生想要的结果

<ns0:root xmlns:ns0="http://myDestinationSchema">
    <ns0:record type="detail" transactionnumber="1" register="1" amount="1.11" paymentmethod="visa"/>
    <ns0:record type="detail" transactionnumber="1" register="1" amount="2.22" paymentmethod="mastercard"/>
    <ns0:record type="detail" transactionnumber="2" register="1" amount="5.55" paymentmethod="discover"/>
    <ns0:record type="detail" transactionnumber="2" register="1" amount="4.44" paymentmethod="visa"/>
    <ns0:record type="detail" transactionnumber="1" register="2" amount="9.99" paymentmethod="amex"/>
    <ns0:record type="detail" transactionnumber="1" register="2" amount="8.88" paymentmethod="visa"/>
    <ns0:record type="summary" register="1" paymentmethod="visa" amount="5.55"/>
    <ns0:record type="summary" register="1" paymentmethod="mastercard" amount="2.22"/>
    <ns0:record type="summary" register="1" paymentmethod="discover" amount="5.55"/>
    <ns0:record type="summary" register="2" paymentmethod="amex" amount="9.99"/>
    <ns0:record type="summary" register="2" paymentmethod="visa" amount="8.88"/>
</ns0:root>

<强>解释

正确使用:

  1. <强> Muenchian Grouping method

  2. AVT s(属性值模板)。

答案 1 :(得分:1)

当这个XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ns0="http://myDestinationSchema"
  xmlns:s0="http://mySourceSchema"
  exclude-result-prefixes="s0"
  version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key
    name="kTenderByRegisterAndMethod"
    match="s0:Tender"
    use="concat(parent::*/@register, '+', @paymentmethod)"/>

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

  <xsl:template match="/*">
    <ns0:root>
      <xsl:apply-templates select="*/s0:Tender"/>
      <xsl:apply-templates 
        select="*/s0:Tender[generate-id() = 
                            generate-id(key(
                              'kTenderByRegisterAndMethod',
                              concat(parent::*/@register,
                                     '+',
                                     @paymentmethod))[1])]"
        mode="summary"/>
    </ns0:root>
  </xsl:template>

  <xsl:template match="s0:Tender">
    <ns0:record type="detail">
      <xsl:apply-templates select="parent::*/@*|@*"/>
    </ns0:record>
  </xsl:template>

  <xsl:template match="s0:Tender" mode="summary">
    <ns0:record type="summary" register="{parent::*/@register}">
      <xsl:attribute name="amount">
        <xsl:value-of
          select="sum(
                    key('kTenderByRegisterAndMethod',
                        concat(parent::*/@register,
                        '+', @paymentmethod))/@amount)"/>
      </xsl:attribute>
      <xsl:apply-templates select="@*[not(name() = 'amount')]"/>
    </ns0:record>
  </xsl:template>
</xsl:stylesheet>

...适用于原始XML:

<s0:SalesCollection xmlns:s0="http://mySourceSchema">
  <s0:Sale transactionnumber="1" register="1">
    <s0:Tender amount="1.11" paymentmethod="visa"/>
    <s0:Tender amount="2.22" paymentmethod="mastercard"/>
  </s0:Sale>
  <s0:Sale transactionnumber="2" register="1">
    <s0:Tender amount="5.55" paymentmethod="discover"/>
    <s0:Tender amount="4.44" paymentmethod="visa"/>
  </s0:Sale>
  <s0:Sale transactionnumber="1" register="2">
    <s0:Tender amount="9.99" paymentmethod="amex"/>
    <s0:Tender amount="8.88" paymentmethod="visa"/>
  </s0:Sale>
</s0:SalesCollection>

...生成了想要的结果:

<ns0:root xmlns:ns0="http://myDestinationSchema">
  <ns0:record type="detail" transactionnumber="1" register="1" amount="1.11" paymentmethod="visa"/>
  <ns0:record type="detail" transactionnumber="1" register="1" amount="2.22" paymentmethod="mastercard"/>
  <ns0:record type="detail" transactionnumber="2" register="1" amount="5.55" paymentmethod="discover"/>
  <ns0:record type="detail" transactionnumber="2" register="1" amount="4.44" paymentmethod="visa"/>
  <ns0:record type="detail" transactionnumber="1" register="2" amount="9.99" paymentmethod="amex"/>
  <ns0:record type="detail" transactionnumber="1" register="2" amount="8.88" paymentmethod="visa"/>
  <ns0:record type="summary" register="1" amount="5.55" paymentmethod="visa"/>
  <ns0:record type="summary" register="1" amount="2.22" paymentmethod="mastercard"/>
  <ns0:record type="summary" register="1" amount="5.55" paymentmethod="discover"/>
  <ns0:record type="summary" register="2" amount="9.99" paymentmethod="amex"/>
  <ns0:record type="summary" register="2" amount="8.88" paymentmethod="visa"/>
</ns0:root>

<强>解释

你是在正确的轨道上检查Muenchian Grouping。注意我使用的复合键匹配s0:Tender元素,方法是连接父级的@register值,“+”符号(为方便起见,确保连接不会被破坏),以及它们的{{1价值。

坚持使用Muenchian Grouping,即使这不是最容易理解的概念。我,我自己,通过查看Muenchian Grouping-related questions on SO并试图回答它们来学习;随着时间的推移,它开始为我清理。