Grouping xml data to multiple requests

懵懂的女人 提交于 2019-12-13 01:57:44

问题


I am stuck on a scenario where I have to take data from a XML and make 3 web service request from it. EX:

<Data>
<Line>
<order>1</order>
<id>10</id>
<amount>10</amount>
<tax>5</tax>
</Line>
<Line>
<order>3</order>
<id>15</id>
<amount>10</amount>
<tax>5</tax>
</Line>
<Line>
<order>1</order>
<id>19</id>
<amount>10</amount>
<tax>5</tax>
</Line>
<Line>
<order>2</order>
<id>12</id>
<amount>10</amount>
<tax>5</tax>
</Line>
<Line>
<order>1</order>
<id>11</id>
<amount>10</amount>
<tax>5</tax>
</Line>
</Data>

So if you see there are three orders so i need to have 3 requests. Like:

<Order>
<no>1</no>
<totalamount>30</totalamount>
<totaltax>15</totaltax>
<list>
<id>10<id>
<id>19<id>
<id>11<id>
</list>
</Order>
<Order>
<no>2</no>
<totalamount>10</totalamount>
<totaltax>5</totaltax>
<list>
<id>12<id>
</list>
</Order>

Am thinking of creating a combined xml and splitting on element order to create multiple requests. How do I approach to do this in XSL.


回答1:


The XSLT:

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

<xsl:output method="xml" encoding="utf-8" indent="yes"/>

<xsl:template match="Data">
  <xsl:for-each-group select="Line" group-by="order">
    <xsl:sort select="current-grouping-key()"/>
    <Order>
      <no><xsl:value-of select="current-grouping-key()"/></no>
      <totalamount><xsl:value-of select="sum(current-group()/amount)"/></totalamount>
      <totaltax><xsl:value-of select="sum(current-group()/tax)"/></totaltax>
      <list>
        <xsl:for-each select="current-group()">
          <id><xsl:value-of select="id"/></id>
        </xsl:for-each>
      </list>
    </Order>
  </xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>

The result:

<?xml version="1.0" encoding="utf-8"?>
<Order>
   <no>1</no>
   <totalamount>30</totalamount>
   <totaltax>15</totaltax>
   <list>
      <id>10</id>
      <id>19</id>
      <id>11</id>
   </list>
</Order>
<Order>
   <no>2</no>
   <totalamount>10</totalamount>
   <totaltax>5</totaltax>
   <list>
      <id>12</id>
   </list>
</Order>
<Order>
   <no>3</no>
   <totalamount>10</totalamount>
   <totaltax>5</totaltax>
   <list>
      <id>15</id>
   </list>
</Order>

Tested by this Online XSLT processor.

Update.

For XSLT 1.0 the same transform can be done using Muenchian grouping method:

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

<xsl:output method="xml" encoding="utf-8" indent="yes"/>

<xsl:key name="lines-by-order" match="Line" use="order"/>

<xsl:template match="Data">
  <xsl:for-each select="Line[count(. | key('lines-by-order', order)[1]) = 1]">
    <xsl:sort select="order"/>
    <Order>
      <no><xsl:value-of select="order"/></no>
      <totalamount><xsl:value-of select="sum(key('lines-by-order', order)/amount)"/></totalamount>
      <totaltax><xsl:value-of select="sum(key('lines-by-order', order)/tax)"/></totaltax>
      <list>
        <xsl:copy-of select="key('lines-by-order', order)/id"/>
      </list>
    </Order>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>


来源:https://stackoverflow.com/questions/32945177/grouping-xml-data-to-multiple-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!