在XSLT中添加了匹配字段

时间:2012-01-22 21:29:17

标签: xml xslt

我需要有关XSLT问题的帮助。我没有太多时间去搜索但是我做了什么我找不到答案。我有五个字段的xml:Policy_Type,Policy_Date,State,Zip和amount Paid。我需要找到一种方法,为具有相同Policy_Type,Policy_Date,State和Zip的所有条目添加Amount Paid字段。有没有人建议在XSL中进行此操作的最佳实践?

这些字段也是从Interbase数据库中提取的。如果有一种方法可以通过也欢迎的SQL语句来实现这一点。我对Interbase不太满意。

1 个答案:

答案 0 :(得分:1)

此转化

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

 <xsl:key name="kTrans" match="transaction"
  use="concat(Policy_Type,
            '+',
              Policy_Date,
            '+',
              State,
            '+',
              Zip)"/>

 <xsl:template match=
  "transaction
    [generate-id()
    =
     generate-id(key('kTrans',
                      concat(Policy_Type,
                            '+',
                              Policy_Date,
                            '+',
                              State,
                            '+',
                              Zip)
                     )[1]
                  )
    ]
  ">
  <transaction>
   <xsl:copy-of select="*[not(self::AmountPaid)]"/>

   <TotalAmountPaid>
     <xsl:value-of select=
     "sum(key('kTrans',
              concat(Policy_Type,
                   '+',
                     Policy_Date,
                   '+',
                     State,
                   '+',
                     Zip)
                     )
                     /AmountPaid
          )
     "/>
   </TotalAmountPaid>
  </transaction>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

应用于以下XML文档(因为没有提供源XML文档!):

<transactions>
 <transaction>
  <Policy_Type>A</Policy_Type>
  <Policy_Date>2012-01-11</Policy_Date>
  <State>WA</State>
  <Zip>98004</Zip>
  <AmountPaid>189.32</AmountPaid>
 </transaction>
 <transaction>
  <Policy_Type>B</Policy_Type>
  <Policy_Date>2012-01-11</Policy_Date>
  <State>NY</State>
  <Zip>111111</Zip>
  <AmountPaid>111.11</AmountPaid>
 </transaction>
 <transaction>
  <Policy_Type>A</Policy_Type>
  <Policy_Date>2012-01-11</Policy_Date>
  <State>WA</State>
  <Zip>98004</Zip>
  <AmountPaid>223.05</AmountPaid>
 </transaction>
 <transaction>
  <Policy_Type>B</Policy_Type>
  <Policy_Date>2012-01-11</Policy_Date>
  <State>NY</State>
  <Zip>111111</Zip>
  <AmountPaid>111.11</AmountPaid>
 </transaction>
 <transaction>
  <Policy_Type>C</Policy_Type>
  <Policy_Date>2012-01-11</Policy_Date>
  <State>NY</State>
  <Zip>111111</Zip>
  <AmountPaid>111.11</AmountPaid>
 </transaction>
</transactions>

生成想要的正确结果

<transaction>
   <Policy_Type>A</Policy_Type>
   <Policy_Date>2012-01-11</Policy_Date>
   <State>WA</State>
   <Zip>98004</Zip>
   <TotalAmountPaid>412.37</TotalAmountPaid>
</transaction>
<transaction>
   <Policy_Type>B</Policy_Type>
   <Policy_Date>2012-01-11</Policy_Date>
   <State>NY</State>
   <Zip>111111</Zip>
   <TotalAmountPaid>222.22</TotalAmountPaid>
</transaction>
<transaction>
   <Policy_Type>C</Policy_Type>
   <Policy_Date>2012-01-11</Policy_Date>
   <State>NY</State>
   <Zip>111111</Zip>
   <TotalAmountPaid>111.11</TotalAmountPaid>
</transaction>

说明:Muenchian grouping