如何使用 XSLT 1.0 对 XMl 进行排序?

发布于 2024-09-14 14:00:46 字数 1459 浏览 11 评论 0原文

我正在寻找一个 XSL 来按一个节点对 XML 进行排序和分组,并按另一个节点进行求和。我将不得不使用 XSLT 1.0。

这是我的 XML。 排序后,我需要对新的 XML 进行排序并按 进行分组,并且应为每个组对所有 进行求和。我正在寻找用于此任务的 XSL。任何帮助表示赞赏。排序后,新的 XML 应该只有三个按 升序排序的节点。所有 必须添加在一起。

这是我的 XML:

<Transactions>
 <Transaction>
  <TransCode>0008</TransCode>
  <TransType>Purchase</TransType>
  <TransAmt>12.30</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0002</TransCode>
  <TransType>Cash</TransType>
  <TransAmt>26.00</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0008</TransCode>
  <TransType>Purchase</TransType>
  <TransAmt>25.00</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0015</TransCode>
  <TransType>FinanceCharge</TransType>
  <TransAmt>25.00</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0002</TransCode>
  <TransType>Cash</TransType>
  <TransAmt>50.00</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0008</TransCode>
  <TransType>Purchase</TransType>
  <TransAmt>40.00</TransAmt>
 </Transaction>
</Transactions>

I'm looking for an XSL to sort and group an XML by a node and sum by another node. I will have to use XSLT 1.0.

Here is my XML.
After sorting I need to have a new XML sorted and Grouped by <TransCode>, and all <TransAmt> should be summed for each group. I'm looking for the XSL for this task. Any help is appreciated. After sorting new XML should have only three nodes sorted in ascending order of <TransCode>. All the <TransAmt> must be added together.

Here is my XML:

<Transactions>
 <Transaction>
  <TransCode>0008</TransCode>
  <TransType>Purchase</TransType>
  <TransAmt>12.30</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0002</TransCode>
  <TransType>Cash</TransType>
  <TransAmt>26.00</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0008</TransCode>
  <TransType>Purchase</TransType>
  <TransAmt>25.00</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0015</TransCode>
  <TransType>FinanceCharge</TransType>
  <TransAmt>25.00</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0002</TransCode>
  <TransType>Cash</TransType>
  <TransAmt>50.00</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0008</TransCode>
  <TransType>Purchase</TransType>
  <TransAmt>40.00</TransAmt>
 </Transaction>
</Transactions>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生活了然无味 2024-09-21 14:00:46

此转换

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

 <xsl:key name="kTransByCode"
     match="Transaction" use="TransCode"/>

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

 <xsl:template match="/Transactions">
  <Transactions>
   <xsl:apply-templates select=
    "Transaction[generate-id()
                =
                 generate-id(key('kTransByCode',
                                  TransCode
                                  )[1]
                             )
                 ]
    ">
     <xsl:sort select="TransCode" data-type="number"/>
    </xsl:apply-templates>
  </Transactions>
 </xsl:template>

 <xsl:template match="TransAmt">
  <TransAmt>
    <xsl:value-of select=
    "sum(key('kTransByCode',../TransCode)/TransAmt)"/>
  </TransAmt>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时

<Transactions>
 <Transaction>
  <TransCode>0008</TransCode>
  <TransType>Purchase</TransType>
  <TransAmt>12.30</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0002</TransCode>
  <TransType>Cash</TransType>
  <TransAmt>26.00</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0008</TransCode>
  <TransType>Purchase</TransType>
  <TransAmt>25.00</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0015</TransCode>
  <TransType>FinanceCharge</TransType>
  <TransAmt>25.00</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0002</TransCode>
  <TransType>Cash</TransType>
  <TransAmt>50.00</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0008</TransCode>
  <TransType>Purchase</TransType>
  <TransAmt>40.00</TransAmt>
 </Transaction>
</Transactions>

产生所需的正确结果

<Transactions>
   <Transaction>
      <TransCode>0002</TransCode>
      <TransType>Cash</TransType>
      <TransAmt>76</TransAmt>
   </Transaction>
   <Transaction>
      <TransCode>0008</TransCode>
      <TransType>Purchase</TransType>
      <TransAmt>77.3</TransAmt>
   </Transaction>
   <Transaction>
      <TransCode>0015</TransCode>
      <TransType>FinanceCharge</TransType>
      <TransAmt>25</TransAmt>
   </Transaction>
</Transactions>

请注意

  1. < p>使用 Muenchian 分组方法

  2. 使用按键可以轻松高效地求和每次转码。

  3. 身份规则用于按原样复制大多数元素,并被匹配输出中不同元素的模板覆盖。

This transformation:

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

 <xsl:key name="kTransByCode"
     match="Transaction" use="TransCode"/>

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

 <xsl:template match="/Transactions">
  <Transactions>
   <xsl:apply-templates select=
    "Transaction[generate-id()
                =
                 generate-id(key('kTransByCode',
                                  TransCode
                                  )[1]
                             )
                 ]
    ">
     <xsl:sort select="TransCode" data-type="number"/>
    </xsl:apply-templates>
  </Transactions>
 </xsl:template>

 <xsl:template match="TransAmt">
  <TransAmt>
    <xsl:value-of select=
    "sum(key('kTransByCode',../TransCode)/TransAmt)"/>
  </TransAmt>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<Transactions>
 <Transaction>
  <TransCode>0008</TransCode>
  <TransType>Purchase</TransType>
  <TransAmt>12.30</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0002</TransCode>
  <TransType>Cash</TransType>
  <TransAmt>26.00</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0008</TransCode>
  <TransType>Purchase</TransType>
  <TransAmt>25.00</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0015</TransCode>
  <TransType>FinanceCharge</TransType>
  <TransAmt>25.00</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0002</TransCode>
  <TransType>Cash</TransType>
  <TransAmt>50.00</TransAmt>
 </Transaction>

 <Transaction>
  <TransCode>0008</TransCode>
  <TransType>Purchase</TransType>
  <TransAmt>40.00</TransAmt>
 </Transaction>
</Transactions>

produces the wanted, correct result:

<Transactions>
   <Transaction>
      <TransCode>0002</TransCode>
      <TransType>Cash</TransType>
      <TransAmt>76</TransAmt>
   </Transaction>
   <Transaction>
      <TransCode>0008</TransCode>
      <TransType>Purchase</TransType>
      <TransAmt>77.3</TransAmt>
   </Transaction>
   <Transaction>
      <TransCode>0015</TransCode>
      <TransType>FinanceCharge</TransType>
      <TransAmt>25</TransAmt>
   </Transaction>
</Transactions>

Do note:

  1. The Muenchian method for grouping is used.

  2. The use of keys allows for easy and efficient summation per transcode.

  3. The identity rule is used to copy as-is most elements and is overridden by templates matching elements that will be different in the output.

薆情海 2024-09-21 14:00:46

XSL 内置了通过 元素,并使用 XPath sum< /a> 函数。

XSLT 1.0 中支持两者。

您在使用这些产品时遇到了哪些问题?

XSL has built in sorting through the <xsl:sort> element, and summing using the XPath sum function.

Both are supported in XSLT 1.0.

What are the issues you are having using these?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文