I can't seem to get my sort to work. I have the following xsl script: ********************************************* <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/*"> <xsl:copy> <xsl:apply-templates select="appraisal/data/form/tag | appraisal/data/form/section/tag"> <xsl:sort select="ancestor::form/@primary" order="descending"/> <xsl:sort select="ancestor::section/@number" order="ascending" data-type="number"/> </xsl:apply-templates> <xsl:apply-templates select="appraisal/attachments/attachment"/> </xsl:copy> </xsl:template> <xsl:template match="tag"> <form> <sectionNumber> <xsl:value-of select="ancestor::section/@number"/> </sectionNumber> <primary> <xsl:value-of select="ancestor::form/@primary"/> </primary> <formName> <xsl:value-of select="ancestor::form/@name"/> </formName> <tagName> <xsl:value-of select="@name"/> </tagName> <flags> <xsl:if test="not(@flags)"> <xsl:attribute name="xsi:nil">true</xsl:attribute> </xsl:if> <xsl:value-of select="@flags"/> </flags> <format> <xsl:if test="not(@format)"> <xsl:attribute name="xsi:nil">true</xsl:attribute> </xsl:if> <xsl:value-of select="@format"/> </format> <value> <xsl:value-of select="text()"/> </value> </form> <xsl:apply-templates select="addendum/heading"/> </xsl:template> <xsl:template match="heading"> <form> <sectionNumber>0</sectionNumber> <primary>false</primary> <formName>addendum</formName> <tagName> <xsl:value-of select="text()"/> </tagName> <flags>0</flags> <format>0</format> <value><xsl:value-of select="following-sibling::body/text()"/></value> </form> </xsl:template> <xsl:template match="attachments/attachment"> <attachment> <key> <xsl:value-of select="@key"/> </key> <type> <xsl:value-of select="@type"/> </type> <label> <xsl:value-of select="@label"/> </label> <imageFormat> <xsl:value-of select="image/binary/@format"/> </imageFormat> <imageText> <xsl:value-of select="image/binary/text()"/> </imageText> <thumbnailFormat> <xsl:value-of select="thumbnail/binary/@format"/> </thumbnailFormat> <thumbnailImage> <xsl:value-of select="thumbnail/binary/text()"/> </thumbnailImage> <sourceFormat> <xsl:value-of select="source/@format"/> </sourceFormat> <sourceKey> <xsl:value-of select="source/@key"/> </sourceKey> <sourceText> <xsl:value-of select="source/binary/text()"/> </sourceText> </attachment> </xsl:template> </xsl:stylesheet> ********************************************* with the following xml file ********************************************* <?xml version="1.0" encoding="utf-8"?> <ACI-Report> <appraisal> <data> <form name="title" primary="false"> <tag name="APPR_NAME.1" flags="1" format="12288">FRED GINO</tag> <tag name="AS_OF_DATE.1" flags="1" format="12288">October 24, 2007</tag> <tag name="BORROWER_NAME.1" flags="1" format="12288">ALI SOUAN</tag> </form> <form name="1025_05" primary="true"> <section type="subject" number="0"> <tag name="CITY.1" flags="1" format="4096">DEARBORN</tag> <tag name="STATE.1" flags="1" format="4096">MI</tag> <tag name="COUNTY.1" flags="1" format="4096">WAYNE</tag> </section> <section type="subject" number="1"> <tag name="GR_AGE.1" flags="1" format="4096">1938</tag> <tag name="GS_COMMENTS.1" flags="4" format="4096">SEE ADDENDUM <addendum> <heading>Comments on Sales Comparison</heading> <body>VARYING WEIGHT AND CONSIDERATION WAS GIVEN</body> </addendum> </tag> <tag name="GS_GBA_SQFT.1" flags="8" format="4128">2477</tag> <tag name="GS_LOCATION.1" flags="1" format="4096">SUBURBAN</tag> </section> <section type="subject" number="2"> <tag name="ADDEND_CMNTS.2" flags="6" format="4096">See Attached Addendum <addendum> <heading>New Comparison</heading> <body>IN MICHIGAN</body> </addendum> </tag> <tag name="APPR_ADDRESS.1" flags="3145729" format="4096">494 MICHELLE ANN</tag> <tag name="ATTIC_NONE_X.1" flags="196610" format="12288">X</tag> </section> </form> </data> <attachments> <attachment key="4ee42bad-65ec-4e70-8a88-b34904a946d5" type="photo" label=""> <image> <binary xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="bin.base64" format="jpeg">/9j/4AAQ</binary> </image> <thumbnail> <binary xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="bin.base64" format="jpeg">/9j/4AAQSk</binary> </thumbnail> <imagedata> <tag name="DATETIME" flags="0" format="0">02/13/2007 12:27:06 PM</tag> </imagedata> </attachment> </attachments> </appraisal> </ACI-Report> ********************************************* The order of the file isn't correct. Inside of the rows have primary = true are the addendums that are false: ************************************************ <?xml version="1.0" encoding="utf-8"?> <ACI-Report> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <sectionNumber>0</sectionNumber> <primary>true</primary> <formName>1025_05</formName> <tagName>CITY.1</tagName> <flags>1</flags> <format>4096</format> <value>DEARBORN</value> </form> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <sectionNumber>0</sectionNumber> <primary>true</primary> <formName>1025_05</formName> <tagName>STATE.1</tagName> <flags>1</flags> <format>4096</format> <value>MI</value> </form> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <sectionNumber>0</sectionNumber> <primary>true</primary> <formName>1025_05</formName> <tagName>COUNTY.1</tagName> <flags>1</flags> <format>4096</format> <value>WAYNE</value> </form> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <sectionNumber>1</sectionNumber> <primary>true</primary> <formName>1025_05</formName> <tagName>GR_AGE.1</tagName> <flags>1</flags> <format>4096</format> <value>1938</value> </form> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <sectionNumber>1</sectionNumber> <primary>true</primary> <formName>1025_05</formName> <tagName>GS_COMMENTS.1</tagName> <flags>4</flags> <format>4096</format> <value>SEE ADDENDUM </value> </form> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <sectionNumber>0</sectionNumber> <primary>false</primary> <formName>addendum</formName> <tagName>Comments on Sales Comparison</tagName> <flags>0</flags> <format>0</format> <value>VARYING WEIGHT AND CONSIDERATION WAS GIVEN</value> </form> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <sectionNumber>1</sectionNumber> <primary>true</primary> <formName>1025_05</formName> <tagName>GS_GBA_SQFT.1</tagName> <flags>8</flags> <format>4128</format> <value>2477</value> </form> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <sectionNumber>1</sectionNumber> <primary>true</primary> <formName>1025_05</formName> <tagName>GS_LOCATION.1</tagName> <flags>1</flags> <format>4096</format> <value>SUBURBAN</value> </form> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <sectionNumber>2</sectionNumber> <primary>true</primary> <formName>1025_05</formName> <tagName>ADDEND_CMNTS.2</tagName> <flags>6</flags> <format>4096</format> <value>See Attached Addendum </value> </form> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <sectionNumber>0</sectionNumber> <primary>false</primary> <formName>addendum</formName> <tagName>New Comparison</tagName> <flags>0</flags> <format>0</format> <value>IN MICHIGAN</value> </form> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <sectionNumber>2</sectionNumber> <primary>true</primary> <formName>1025_05</formName> <tagName>APPR_ADDRESS.1</tagName> <flags>3145729</flags> <format>4096</format> <value>494 MICHELLE ANN</value> </form> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <sectionNumber>2</sectionNumber> <primary>true</primary> <formName>1025_05</formName> <tagName>ATTIC_NONE_X.1</tagName> <flags>196610</flags> <format>12288</format> <value>X</value> </form> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <sectionNumber></sectionNumber> <primary>false</primary> <formName>title</formName> <tagName>APPR_NAME.1</tagName> <flags>1</flags> <format>12288</format> <value>FRED GINO</value> </form> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <sectionNumber></sectionNumber> <primary>false</primary> <formName>title</formName> <tagName>AS_OF_DATE.1</tagName> <flags>1</flags> <format>12288</format> <value>October 24, 2007</value> </form> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <sectionNumber></sectionNumber> <primary>false</primary> <formName>title</formName> <tagName>BORROWER_NAME.1</tagName> <flags>1</flags> <format>12288</format> <value>ALI SOUAN</value> </form> <attachment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <key>4ee42bad-65ec-4e70-8a88-b34904a946d5</key> <type>photo</type> <label></label> <imageFormat>jpeg</imageFormat> <imageText>/9j/4AAQ</imageText> <thumbnailFormat>jpeg</thumbnailFormat> <thumbnailImage>/9j/4AAQSk</thumbnailImage> <sourceFormat></sourceFormat> <sourceKey></sourceKey> <sourceText></sourceText> </attachment> </ACI-Report> ************************************************ An easier way to see them is how my dataset reads the new xml file: *************************************************** Row: 0: 0 true 1025_05 CITY.1 1 4096 DEARBORN Row: 1: 0 true 1025_05 STATE.1 1 4096 MI Row: 2: 0 true 1025_05 COUNTY.1 1 4096 WAYNE Row: 3: 1 true 1025_05 GR_AGE.1 1 4096 1938 Row: 4: 1 true 1025_05 GS_COMMENTS.1 4 4096 SEE ADDENDUM Row: 5: 0 false addendum Comments on Sales Comparison 0 0 VARYING WEIGHT AND CONSIDERATION WAS GIVEN Row: 6: 1 true 1025_05 GS_GBA_SQFT.1 8 4128 2477 Row: 7: 1 true 1025_05 GS_LOCATION.1 1 4096 SUBURBAN Row: 8: 2 true 1025_05 ADDEND_CMNTS.2 6 4096 See Attached Addendum Row: 9: 0 false addendum New Comparison 0 0 IN MICHIGAN Row: 10: 2 true 1025_05 APPR_ADDRESS.1 3145729 4096 494 MICHELLE ANN Row: 11: 2 true 1025_05 ATTIC_NONE_X.1 196610 12288 X Row: 12: false title APPR_NAME.1 1 12288 FRED GINO Row: 13: false title AS_OF_DATE.1 1 12288 October 24, 2007 Row: 14: false title BORROWER_NAME.1 1 12288 ALI SOUAN **************************************************** What I am trying to get is: *********************************************************** Row: 0: 0 true 1025_05 CITY.1 1 4096 DEARBORN Row: 1: 0 true 1025_05 STATE.1 1 4096 MI Row: 2: 0 true 1025_05 COUNTY.1 1 4096 WAYNE Row: 3: 1 true 1025_05 GR_AGE.1 1 4096 1938 Row: 4: 1 true 1025_05 GS_COMMENTS.1 4 4096 SEE ADDENDUM Row: 6: 1 true 1025_05 GS_GBA_SQFT.1 8 4128 2477 Row: 7: 1 true 1025_05 GS_LOCATION.1 1 4096 SUBURBAN Row: 8: 2 true 1025_05 ADDEND_CMNTS.2 6 4096 See Attached Addendum Row: 10: 2 true 1025_05 APPR_ADDRESS.1 3145729 4096 494 MICHELLE ANN Row: 11: 2 true 1025_05 ATTIC_NONE_X.1 196610 12288 X Row: 5: 0 false addendum Comments on Sales Comparison 0 0 VARYING WEIGHT AND CONSIDERATION WAS GIVEN Row: 9: 0 false addendum New Comparison 0 0 IN MICHIGAN Row: 12: false title APPR_NAME.1 1 12288 FRED GINO Row: 13: false title AS_OF_DATE.1 1 12288 October 24, 2007 Row: 14: false title BORROWER_NAME.1 1 12288 ALI SOUAN ************************************************************* Where all the rows that have primary as "true" are first and all together. And sorted by the Section numbers (1st column). Then all the ones that have the fields = "false". That would put the "addendum" rows after the 1025_05 rows. Why doesn't it sort correctly? Thanks, Tom