Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add Items to get Quantity or Sort xml output? 1

Status
Not open for further replies.

Snug

MIS
Sep 5, 2001
59
0
0
US
I am an XML Newbie, I have an XML output file that list each "Item" for each "Qty". For example, if item "B42" QTY=2, the XML file will list it 2 times with QTY=1 instead of listing it once with QTY=2.

DUE TO THIS HEADACH, is there a way to count/add the quantities? <<<preferred method>>>

If there is no way to count/add, is there a way to sort the output?

Below is the code and the source. I've tried to read and google help, but I cannot get it to work.

I really do not mind in which order it is sorted, just as long as the duplicates are together.

Thank you in advance! -Snug

Output code:
Code:
0-SB36BUTT|1|789|Cabinets|0|1|
0-B42|1|957|Cabinets|0|12|
{NONE}|0|0|Cabinets|-1|1.1|
0-B42|1|957|Cabinets|0|12|

Source XML: (Just 3 items)
Code:
<Item ID="IDI-1-110">
      <UserCode>SB36BUTT</UserCode>
      <Quantity>1</Quantity>
      <Price Type="Cost" Mode="Fixed" Level="1">
      <Value>899</Value>
      <FormattedValue><![CDATA[$899.00]]></FormattedValue>
      </Price>
      <Price Type="List" Mode="Fixed" Level="1">
      <Value>899</Value>
      <FormattedValue><![CDATA[$899.00]]></FormattedValue>
      </Price>
      <Price Type="Retail" Mode="Fixed" Level="1">
      <Value>899</Value>
      <FormattedValue><![CDATA[$899.00]]></FormattedValue>
      </Price>
      <Price Type="Retail" Mode="Fixed" Level="2">
      <Value>0</Value>
      <FormattedValue><![CDATA[$0.00]]></FormattedValue>
      </Price>
      <Var ID="-2" type="String">
      <Value>SB36BUTT</Value>
      <Name><![CDATA[PV_FloorPlanCODE]]></Name>
      </Var>
      <Var ID="-7" type="String">
      <Value>0</Value>
      <Name><![CDATA[PV_FMTPOSitionSTart]]></Name>
      </Var>
      <Var ID="-8" type="String">
      <Value>36</Value>
      <Name><![CDATA[PV_FMTPOSitionEND]]></Name>
      </Var>
</Item>
<Item ID="IDI-1-111">
      <UserCode><![CDATA[B42]]></UserCode>
      <Quantity>1</Quantity>
      <Price Type="Cost" Mode="Fixed" Level="1">
      <Value>0</Value>
      <FormattedValue><![CDATA[$0.00]]></FormattedValue>
      </Price>
      <Price Type="List" Mode="Fixed" Level="1">
      <Value>0</Value>
      <FormattedValue><![CDATA[$0.00]]></FormattedValue>
      </Price>
      <Price Type="Retail" Mode="Fixed" Level="1">
      <Value>456</Value>
      <FormattedValue><![CDATA[$0.00]]></FormattedValue>
      </Price>
      <Price Type="Retail" Mode="Fixed" Level="2">
      <Value>0</Value>
      <FormattedValue><![CDATA[$0.00]]></FormattedValue>
      </Price>
      <Var ID="-9" type="String">
      <Value>18</Value>
      <Name><![CDATA[PV_FMTPOSitionCENter]]></Name>
      </Var>
      <Var ID="-2" type="String">
      <Value><![CDATA[B42]]></Value>
      <Name><![CDATA[PV_FloorPlanCODE]]></Name>
      </Var>
      <Var ID="-7" type="String">
      <Value>0</Value>
      <Name><![CDATA[PV_FMTPOSitionSTart]]></Name>
      </Var>
      <Var ID="-8" type="String">
      <Value>36</Value>
      <Name><![CDATA[PV_FMTPOSitionEND]]></Name>
      </Var>
      <Var ID="-12" type="Qty">
      <Value>54</Value>
      <Name><![CDATA[PV_TYPECAB]]></Name>
      </Var>
</Item>
<Item ID="IDI-1-431">
      <UserCode><![CDATA[B42]]></UserCode>
      <Quantity>1</Quantity>
      <Price Type="Cost" Mode="Fixed" Level="1">
      <Value>0</Value>
      <FormattedValue><![CDATA[$0.00]]></FormattedValue>
      </Price>
      <Price Type="List" Mode="Fixed" Level="1">
      <Value>0</Value>
      <FormattedValue><![CDATA[$0.00]]></FormattedValue>
      </Price>
      <Price Type="Retail" Mode="Fixed" Level="1">
      <Value>456</Value>
      <FormattedValue><![CDATA[$0.00]]></FormattedValue>
      </Price>
      <Price Type="Retail" Mode="Fixed" Level="2">
      <Value>0</Value>
      <FormattedValue><![CDATA[$0.00]]></FormattedValue>
      </Price>
      <Var ID="-9" type="String">
      <Value>18</Value>
      <Name><![CDATA[PV_FMTPOSitionCENter]]></Name>
      </Var>
      <Var ID="-2" type="String">
      <Value><![CDATA[B42]]></Value>
      <Name><![CDATA[PV_FloorPlanCODE]]></Name>
      </Var>
      <Var ID="-7" type="String">
      <Value>0</Value>
      <Name><![CDATA[PV_FMTPOSitionSTart]]></Name>
      </Var>
      <Var ID="-8" type="String">
      <Value>36</Value>
      <Name><![CDATA[PV_FMTPOSitionEND]]></Name>
      </Var>
      <Var ID="-12" type="Qty">
      <Value>54</Value>
      <Name><![CDATA[PV_TYPECAB]]></Name>
      </Var>
</Item>
XSL:
Code:
<xsl:template match="Item">
  <xsl:if test="ItemTypeName != 'Appliances'">
    <xsl:if test="Price[3]/Value=0">
      <br>
        <xsl:value-of select="Var[@ID='-2']/Value"/>
        <xsl:text>|0|0|</xsl:text>
        <xsl:value-of select="ItemTypeName"/>
        <xsl:text>|-1|</xsl:text>
        <xsl:value-of select="LineItemNumber"/>
        <xsl:text>|</xsl:text>
      </br>
    </xsl:if>
    <xsl:if test="Price[3]/Value != 0">
      <br>
      <xsl:for-each select="Item">
      <xsl:sort select="Var[@ID='-2']/Value" data-type="number" order="ascending">
       </xsl:sort>
        </xsl:for-each>
        <xsl:value-of select="Var[@ID='-7']/Value"/>
        <xsl:text>-</xsl:text>
        <xsl:value-of select="Var[@ID='-2']/Value"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="Quantity"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="Price[3]/Value + Price[6]/Value"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="ItemTypeName"/>
        <xsl:text>|</xsl:text>
        <xsl:choose>
	<xsl:when test="SubTypeName = 'Charge'">
	<xsl:text>1</xsl:text>
	</xsl:when>
	<xsl:otherwise>
	<xsl:text>0</xsl:text>
	</xsl:otherwise>
        </xsl:choose>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="LineItemNumber"/>
        <xsl:text>|</xsl:text>
      </br>
    </xsl:if>
  </xsl:if>
</xsl:template>

 
Thank you Tom. I have been reading the grouping, and googled it for examples, but my code will not generate any output. If I put the code in where "Item != 0", then all those items have a blank space in the output.

Would you mind looking at my code to see if I'm close?
Thanks,
Snug

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0" xsi:schemaLocation="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform[/URL] xslt.xsd" xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance">[/URL]
 <xsl:key name="Group" match="Item" use="Var[@ID='-2']/Value"/>
 <xsl:output method="html"/>
 <xsl:template match="/">
  <xsl:apply-templates/>
 </xsl:template>
<xsl:template match="Item">
 <xsl:if test="ItemTypeName != 'Appliances'">
  <xsl:for-each select="Item/Var=key('Group', Var[@ID='-2']/Value)">
<!--  <xsl:for-each select="Item/Var[generate-id(.)=generate-id(key('Group', ../ID='-2'/Value)[1])]">-->
  <xsl:sort select="@ID='-2'/Value"/>
    <xsl:if test="Price[3]/Value=0">
     <br>
     <xsl:value-of select="Var[@ID='-2']/Value"/>
     <xsl:text>|0|0|</xsl:text>
     <xsl:value-of select="ItemTypeName"/>
     <xsl:text>|-1|</xsl:text>
     <xsl:value-of select="LineItemNumber"/>
     <xsl:text>|</xsl:text>
     </br>
     </xsl:if>
     <xsl:if test="Price[3]/Value != 0">
     <br>
     <xsl:value-of select="Var[@ID='-7']/Value"/>
     <xsl:text>-</xsl:text>
     <xsl:value-of select="Var[@ID='-2']/Value"/>
     <xsl:text>|</xsl:text>
     <xsl:value-of select="Quantity"/>
     <xsl:text>|</xsl:text>
     <xsl:value-of select="Price[3]/Value + Price[6]/Value"/>
     <xsl:text>|</xsl:text>
     <xsl:value-of select="ItemTypeName"/>
     <xsl:text>|</xsl:text>
     <xsl:choose>
          <xsl:when test="SubTypeName = 'Charge'">
          <xsl:text>1</xsl:text>
          </xsl:when>
          <xsl:otherwise>
           <xsl:text>0</xsl:text>
          </xsl:otherwise>
     </xsl:choose>
     <xsl:text>|</xsl:text>
     <xsl:value-of select="LineItemNumber"/>
     <xsl:text>|</xsl:text>
     </br>
     </xsl:if>
    </xsl:for-each>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>
 
[0] You have there a lot of options etc not essential to the functionality you are after. There are even syntax error and I wonder why the application does not flag it to notice you.

[1] This is the simple no non-sense illustration how it gets the (a) grouping by that specific Value and (b) sum over Quantity of that reference Value's item.
[tt]
<xsl:key name="Group" match="Item" use="Var[@ID='-2']/Value" />
<xsl:template match="/">
<xsl:apply-templates select="//Item[count(.|key('Group',Var[@ID='-2']/Value)[1])=1]" />
</xsl:template>
<xsl:template match="Item">
<div>
<xsl:value-of select="Var[@ID='-2']/Value" />
<xsl:text>|</xsl:text>
<xsl:value-of select="sum(key('Group',Var[@ID='-2']/Value)/Quantity)" />
</div>
</xsl:template>
[/tt]
ps: As an aside, there is no such thing as non-empty <br></br>. Use <div> and </div> at their place.
 
Tsuji,

You are the best!!!! That was it!

And yes, the code I uploaded did give an error, I forgot to remove my "trial & error" code.

I'm new to xml/xsl, and learn by examples. I cannot THANK YOU ENOUGH!!
 
Glad that you've got the idea. If you want sorting, it is here you apply it.
[tt]
<xsl:template match="/">
<xsl:apply-templates select="//Item[count(.|key('Group',Var[@ID='-2']/Value)[1])=1]">
[blue]<xsl:sort select="Var[@ID='-2']/Value" order="ascending" />[/blue]
</xsl:apply-templates>
</xsl:template>
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top