This discussion is archived
2 Replies Latest reply: Nov 30, 2012 7:06 AM by 970002 RSS

XSLT Sort Issues

970002 Newbie
Currently Being Moderated
I have an XML document that give me a list of PO lines and orders. When I get the PIX message I want to sort it by PO number and then PO Line. For some reason my syntax below isn't working. I need to sort by Reference2 then Reference4. I tried to just get 4 to work but that wasn't working either.


<xsl:template match="/">
<xsl:for-each select="tXML/Message/PIX">
<xsl:sort select="PIXFields/Reference4"/>
     <xsl:apply-templates select="."/>     
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>



<tXML>
     <Message>
          <PIX>
               <TransactionType>603</TransactionType>
               <TransactionCode>02</TransactionCode>
               <TransactionNumber>10731</TransactionNumber>
               <SequenceNumber>1</SequenceNumber>
               <PIXFields>
                    <DateCreated>11/29/12 13:25 EST</DateCreated>
                    <ReferenceCode1>104</ReferenceCode1>
                    <Reference1>ASN1769527</Reference1>
                    <ReferenceCode2>108</ReferenceCode2>
                    <Reference2>1769527</Reference2>
                    <ReferenceCode3>107</ReferenceCode3>
                    <ReferenceCode4>43</ReferenceCode4>
                    <Reference4>7.000</Reference4>
               </PIXFields>
          </PIX>
          <PIX>
               <TransactionType>603</TransactionType>
               <TransactionCode>02</TransactionCode>
               <TransactionNumber>10731</TransactionNumber>
               <SequenceNumber>2</SequenceNumber>
               <PIXFields>
                    <DateCreated>11/29/12 13:25 EST</DateCreated>
                    <ReferenceCode1>104</ReferenceCode1>
                    <Reference1>ASN1769527</Reference1>
                    <ReferenceCode2>108</ReferenceCode2>
                    <Reference2>1769527</Reference2>
                    <ReferenceCode3>107</ReferenceCode3>
                    <ReferenceCode4>43</ReferenceCode4>
                    <Reference4>7.000</Reference4>
               </PIXFields>
          </PIX>
          <PIX>
               <TransactionType>603</TransactionType>
               <TransactionCode>02</TransactionCode>
               <TransactionNumber>10731</TransactionNumber>
               <SequenceNumber>3</SequenceNumber>
               <PIXFields>
                    <DateCreated>11/29/12 13:25 EST</DateCreated>
                    <ReferenceCode1>104</ReferenceCode1>
                    <Reference1>ASN1769527</Reference1>
                    <ReferenceCode2>108</ReferenceCode2>
                    <Reference2>1769527</Reference2>
                    <ReferenceCode3>107</ReferenceCode3>
                    <ReferenceCode4>43</ReferenceCode4>
                    <Reference4>3.000</Reference4>
               </PIXFields>
          </PIX>
          <PIX>
               <TransactionType>603</TransactionType>
               <TransactionCode>02</TransactionCode>
               <TransactionNumber>10731</TransactionNumber>
               <SequenceNumber>4</SequenceNumber>
               <PIXFields>
                    <DateCreated>11/29/12 13:25 EST</DateCreated>
                    <ReferenceCode1>104</ReferenceCode1>
                    <Reference1>ASN1769527</Reference1>
                    <ReferenceCode2>108</ReferenceCode2>
                    <Reference2>1769527</Reference2>
                    <ReferenceCode3>107</ReferenceCode3>
                    <ReferenceCode4>43</ReferenceCode4>
                    <Reference4>6.000</Reference4>
               </PIXFields>
          </PIX>
          <PIX>
               <TransactionType>603</TransactionType>
               <TransactionCode>02</TransactionCode>
               <TransactionNumber>10731</TransactionNumber>
               <SequenceNumber>5</SequenceNumber>
               <PIXFields>
                    <DateCreated>11/29/12 13:25 EST</DateCreated>
                    <ReferenceCode1>104</ReferenceCode1>
                    <Reference1>ASN1769527</Reference1>
                    <ReferenceCode2>108</ReferenceCode2>
                    <Reference2>1769527</Reference2>
                    <ReferenceCode3>107</ReferenceCode3>
                    <ReferenceCode4>43</ReferenceCode4>
                    <Reference4>10.000</Reference4>
               </PIXFields>
          </PIX>
          <PIX>
               <TransactionType>603</TransactionType>
               <TransactionCode>02</TransactionCode>
               <TransactionNumber>10731</TransactionNumber>
               <SequenceNumber>6</SequenceNumber>
               <PIXFields>
                    <DateCreated>11/29/12 13:25 EST</DateCreated>
                    <ReferenceCode1>104</ReferenceCode1>
                    <Reference1>ASN1769527</Reference1>
                    <ReferenceCode2>108</ReferenceCode2>
                    <Reference2>1769527</Reference2>
                    <ReferenceCode3>107</ReferenceCode3>
                    <ReferenceCode4>43</ReferenceCode4>
                    <Reference4>2.000</Reference4>
               </PIXFields>
          </PIX>
     </Message>
</tXML>
  • 1. Re: XSLT Sort Issues
    odie_63 Guru
    Currently Being Moderated
    This is usually achieved with a classic identity transformation, amended with a special template to process element(s) of interest :
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="node()">
        <xsl:copy>
          <xsl:apply-templates select="node()"/>
        </xsl:copy>
      </xsl:template>
      <xsl:template match="Message">
        <xsl:copy>
          <xsl:apply-templates select="PIX">
            <xsl:sort select="PIXFields/Reference2" data-type="number"/>
            <xsl:sort select="PIXFields/Reference4" data-type="number"/>
          </xsl:apply-templates>
        </xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
  • 2. Re: XSLT Sort Issues
    970002 Newbie
    Currently Being Moderated
    Thanks so much. It works as expected.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points