Hi all,

I'm rather new to XSL (and this forum) and have searched everywhere to find
out how to order the output of XML to text using XSLT but haven't found the
answer.

The XML is similar to the following:

<table name="data">
 
      <row>
      <field column="groupA" type="java.lang.String">00000030</field>
      <field column="name" type="java.lang.String">Smith</field>
      <field column="name" type="java.lang.String">Smith</field>
      </row>
 
      <row>
      <field column="groupB" type="java.lang.String">00000040</field>
      <field column="name" type="java.lang.String">Smith</field>
      <field column="name" type="java.lang.String">Smith</field>
      <field column="name" type="java.lang.String">Smith</field>
      </row>
 
      <row>
      <field column="groupC" type="java.lang.String">00000050</field>
      <field column="name" type="java.lang.String">Smith</field>
      <field column="name" type="java.lang.String">Smith</field>
      <field column="name" type="java.lang.String">Smith</field>
      <field column="name" type="java.lang.String">Smith</field>
      </row>
 
      <row>
      <field column="clients" type="java.lang.String">00000020</field>
      <field column="number" type="java.lang.String">9</field>
      </row>
      <row>
      <field column="rows" type="java.lang.String">00000010</field>
      <field column="number" type="java.lang.String">3</field>
      </row>
 
      </table>

I won't know the number in the clients or rows until after the processing so
it will always appear on the bottom of the XML, however, I need to post
these at the top of the text output file. The sort function would be useful
except the order of all the other elements should be as written in the XML.
Or alternatively, is there a way of processing the xml as it appears but
using an xsl:if to output the text related to the clients & rows at the top
of the text file? The final output should be similar to the following:

000000103
000000209
00000030SmithSmith
00000040SmithSmithSmith
00000050SmithSmithSmithSmith

If anyone can point me in the right direction or suggest a solution I would
be very grateful. (Of course, if I have posted to the wrong area do let me
know.)

Many thanks

A

Member Avatar for gravyboat

I'm not entirely sure what you're trying to do, but this should output what you want at the top:

<xsl:stylesheet exclude-result-prefixes="" version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    	<xsl:for-each select="//row">
    		<xsl:sort select="count(./field)"/>
    		<xsl:value-of select="."/>
    	</xsl:for-each>
    
        <xsl:apply-templates select="table"/>
    </xsl:template>
    
    
    <xsl:template match="table">
    	(do whatever you normally do here to preserve "order of all the other elements should be as written in the XML"
    </xsl:template>
    
</xsl:stylesheet>

Just edit the 'template match="table"' portion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.