#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    XML XSLT Sorting Issue


    When I try this, my data sorts, but the XML tags are stripped out.
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>
     <xsl:template match="searchResults/searchResult">
      <xsl:copy>
       <xsl:apply-templates>
         <xsl:sort select="//TSN" order="descending"/>
       </xsl:apply-templates>
     </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>
    When I do this, the XML output format is correct, but it does not sort.
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>
     <xsl:template match="node()">
      <xsl:copy>
       <xsl:apply-templates>
         <xsl:sort select="//TSN" order="descending"/>
       </xsl:apply-templates>
     </xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
    Here is a sample of my XML:
    Code:
    <?xml version="1.0" encoding="UTF-8"?> <response> <info><statusCode>0</statusCode><messages/></info> <search> <options><maxMatches>100</maxMatches><units>m</units></options> <resultsCount>3</resultsCount> <totalPages>1</totalPages> <searchResults> <searchResult> <resultNumber>1</resultNumber> <fields> <LocationState>OK</LocationState><TSN>N</TSN><LocationCity>TULSA</LocationCity> </fields> </searchResult> <searchResult> <resultNumber>2</resultNumber> <fields> <LocationState>OK</LocationState><TSN>Y</TSN><LocationCity>BIXBY</LocationCity> </fields> </searchResult> <searchResult> <resultNumber>3</resultNumber> <fields> <LocationState>OK</LocationState><TSN>N</TSN><LocationCity>COWETA</LocationCity> </fields> </searchResult> </searchResults> <origin><adminArea3 type="State">OK</adminArea3></origin> </search> </response>
    Any ideas? I am new to XSLT and am struggling. I need to get this issue resolved ASAP. Please help!

    Thanks for any assistance.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2006
    Posts
    24
    Rep Power
    0
    assuming you want sorted by searchResults/searchResult/fields/TSN

    input.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <response>
      <info>
        <statusCode>0</statusCode>
        <messages/>
      </info>
      <search>
        <options>
          <maxMatches>100</maxMatches>
          <units>m</units>
        </options>
        <resultsCount>3</resultsCount>
        <totalPages>1</totalPages>
        <searchResults>
          <searchResult>
            <resultNumber>1</resultNumber>
            <fields>
              <LocationState>OK</LocationState>
              <TSN>N</TSN>
              <LocationCity>TULSA</LocationCity>
            </fields>
          </searchResult>
          <searchResult>
            <resultNumber>2</resultNumber>
            <fields>
              <LocationState>OK</LocationState>
              <TSN>Y</TSN>
              <LocationCity>BIXBY</LocationCity>
            </fields>
          </searchResult>
          <searchResult>
            <resultNumber>3</resultNumber>
            <fields>
              <LocationState>OK</LocationState>
              <TSN>N</TSN>
              <LocationCity>COWETA</LocationCity>
            </fields>
          </searchResult>
        </searchResults>
        <origin>
          <adminArea3 type="State">OK</adminArea3>
        </origin>
      </search>
    </response>

    using xsl
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="/">
        <xsl:element name="root">
          <xsl:apply-templates select="//searchResult">
            <xsl:sort select="./fields/TSN" order="descending"/>
          </xsl:apply-templates>
        </xsl:element>
      </xsl:template>
      
      <xsl:template match="*|@*|text()">
        <xsl:apply-templates select="node()|@*"/>
      </xsl:template>
      
      <xsl:template match="searchResult">
        <xsl:copy-of select="."/>
      </xsl:template>
    </xsl:stylesheet>
    result
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    	<searchResult>
    		<resultNumber>2</resultNumber>
    		<fields>
    			<LocationState>OK</LocationState>
    			<TSN>Y</TSN>
    			<LocationCity>BIXBY</LocationCity>
    		</fields>
    	</searchResult>
    	<searchResult>
    		<resultNumber>1</resultNumber>
    		<fields>
    			<LocationState>OK</LocationState>
    			<TSN>N</TSN>
    			<LocationCity>TULSA</LocationCity>
    		</fields>
    	</searchResult>
    	<searchResult>
    		<resultNumber>3</resultNumber>
    		<fields>
    			<LocationState>OK</LocationState>
    			<TSN>N</TSN>
    			<LocationCity>COWETA</LocationCity>
    		</fields>
    	</searchResult>
    </root>
    note if wanted, can also sort with multiple criteria, as

    Code:
          <xsl:apply-templates select="//searchResult">
            <xsl:sort select="./fields/TSN" order="descending"/>
            <xsl:sort select="./fields/LocationState" order="ascending"/>
            <xsl:sort select="./fields/LocationCity" order="ascending"/>
          </xsl:apply-templates>

IMN logo majestic logo threadwatch logo seochat tools logo