1

I have XML(very huge file) and I would like to get output based on SORTING of attribute(column) value contact_name. Is this possible using some tool or with coding?

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<?xml-stylesheet type="text/xsl" href="sms.xsl"?>
<smses count="4">

 <sms address="+381642" subject="null" contact_name="C" />
 <sms address="+3816424" subject="null" contact_name="A" />
 <sms address="+3816427" subject="null" contact_name="B" />
</smses>

sms.xsl file

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:user="http://android.riteshsahu.com">
<xsl:template match="/">
  <html>
      <head>
          <style type="text/css">
          body 
          {
            font-family:arial,sans-serif;
            color:#000;
            font-size:13px;
            color:#333;
          }
          table 
          {
            font-size:1em;
            margin:0 0 1em;
            border-collapse:collapse;
            border-width:0;
            empty-cells:show;
          }
          td,th 
          {
            border:1px solid #ccc;
            padding:6px 12px;
            text-align:left;
            vertical-align:top;
            background-color:inherit;
          }
          th 
          {
            background-color:#dee8f1;
          }
          </style>
      </head>
      <body>
      <h2>SMS Messages</h2>
      <table>
        <tr>
          <th>Type</th>
          <th>Number</th>
          <th>Contact</th>
          <th>Date</th>
          <th>Message</th>
        </tr>
        <xsl:for-each select="smses/sms">
         <xsl:sort select="contact_name" data-type="text"/>
        <tr>
          <td>
            <xsl:if test="@type = 1">
            Received
            </xsl:if>
            <xsl:if test="@type = 2">
            Sent
            </xsl:if>
          </td>
          <td><xsl:value-of select="@address"/></td>
          <td><xsl:value-of select="@contact_name"/></td>
          <td><xsl:value-of select="@date"/><br/><xsl:value-of select="@readable_date"/></td>
          <td><xsl:value-of select="@body"/></td>
        </tr>
        </xsl:for-each>
      </table>
      </body>
  </html>
</xsl:template>
</xsl:stylesheet>

2 Answers 2

1

In your XSLT file, sms.xsl, you can do an <xsl:sort> using @ to reference an attribute, like this...

<xsl:sort select="@contact_name"/>

Note: positioning is important, it needs to be within an <xsl:for-each> or <xsl:apply-templates>. So just insert the above line directly after the start of the <xsl:for-each> in your sms.xsl file...

<xsl:for-each select="smses/sms">
  <xsl:sort select="@contact_name"/> <!-- new line -->
Sign up to request clarification or add additional context in comments.

6 Comments

please sorry but I never done with XSLT.. how to run that in what application? please can you give me the full description..sorry for this but really do not know anything about that...
The XML is already referencing an XSLT file: sms.xsl so this seems like a good place to do the sort. Can you post some or all of the that XSLT file?
I updated it. what exactly line should I change. thank you for efforts I suppose we are near of the solution?
yes I added you can see it in my new update. But when I reopen XML file nothing happens. Do I need some update,refresh...? both files are in the same folder
Are you opening the XML file in a web browser? And if yes, which one? Also, do you want the output file to also be XML or you just want them sorted in the HTML?
|
0

This is the syntax of sorting in XML

<xsl:sort select="expression"
lang="language-code"
data-type="text|number|qname"
order="ascending|descending"
case-order="upper-first|lower-first"/>

Before that you need to know the XSTL formatting . See example here

Secondly you have use <xsl:sort select="contact_name" data-type="text"/> in XSL file of XML

Finally here

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:user="http://android.riteshsahu.com">
<xsl:template match="/">
  <html>
      <head>
          <style type="text/css">
          body 
          {
            font-family:arial,sans-serif;
            color:#000;
            font-size:13px;
            color:#333;
          }
          table 
          {
            font-size:1em;
            margin:0 0 1em;
            border-collapse:collapse;
            border-width:0;
            empty-cells:show;
          }
          td,th 
          {
            border:1px solid #ccc;
            padding:6px 12px;
            text-align:left;
            vertical-align:top;
            background-color:inherit;
          }
          th 
          {
            background-color:#dee8f1;
          }
          </style>
      </head>
      <body>
      <h2>SMS Messages</h2>
      <table>
        <tr>
          <th>Type</th>
          <th>Number</th>
          <th>Contact</th>
          <th>Date</th>
          <th>Message</th>
        </tr>
        <xsl:for-each select="smses/sms">
        <!-- Sorting added here -->
       <xsl:sort select="contact_name" data-type="text"/>             
        <tr>
          <td>
            <xsl:if test="@type = 1">
            Received
            </xsl:if>
            <xsl:if test="@type = 2">
            Sent
            </xsl:if>
          </td>  
          <td><xsl:value-of select="@address"/></td>
          <td><xsl:value-of select="@contact_name"/></td>
          <td><xsl:value-of select="@date"/><br/><xsl:value-of select="@readable_date"/></td>
          <td><xsl:value-of select="@body"/></td>
        </tr>
        </xsl:for-each>
      </table>
      </body>
  </html>
</xsl:template>
</xsl:stylesheet>

3 Comments

thank you but where to put that code in XSL file? I updated my question with XSL file. please tell me where to put that code? thank you
I have already added the sorting in the above code. Just use this to generate the xml . The output the filed will be sorted.
Hi, I updated my question with new SMS.XSL file. I entered new row in XSL file. But when I reopen XML file nothing is changed? What am I doing wrong? Do I need some kind of refresh or...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.