View Single Post
  #5 (permalink)  
Old March 2nd, 2010, 09:50 AM
anil_yadav26@hotmail.com anil_yadav26@hotmail.com is offline
Authorized User
 
Join Date: Feb 2009
Location: Gurgaon
Posts: 31
Thanks: 6
Thanked 1 Time in 1 Post
Send a message via MSN to anil_yadav26@hotmail.com
Thumbs up

Hi Martin,

Thanks for your reply. Now, its working perfectly. Thanks a lot!!!

I have one more query regarding the same input XML. Actually I have to filter the XML with following steps:

1. Sorting the <link> nodes
2. Removing the content of duplicate nodes
3. Making range

Now I have three different XSLT for all the TASK. Is it possible to merge all these XSLT into one?

Thanks in advance.


Thanks,
Anil Yadav



1. Sorting the <link> nodes
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" 
    version="2.0">
 
 <xsl:template match="@*|node()">
   <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
 </xsl:template>
 
<xsl:template match="index-item">
 <index-item>
  <xsl:copy-of select="@*"/>
  <xsl:if test="index-entry">
    <xsl:apply-templates select="index-entry"/>
   </xsl:if>
  <xsl:for-each select="link">
   <xsl:sort select="."/>
   <link>
    <xsl:copy-of select="@*"/>
    <xsl:value-of select="."/>
   </link>
  </xsl:for-each>
 </index-item>
 </xsl:template>
</xsl:stylesheet>

2. Removing the content of duplicate nodes

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" 
    version="2.0">
 <xsl:template match="index-item">
  <index-item>
   <xsl:copy-of select="@*"/>
   <xsl:if test="index-entry">
    <xsl:apply-templates select="index-entry"/>
   </xsl:if>
   <xsl:for-each select="link">
    <xsl:sort select="."/>
    <link>
     <xsl:copy-of select="@*"/>
     <xsl:if test="not(node()) or not(preceding-sibling::node()[.=string(current())])">
      <xsl:if test="position()!=last()">
       <xsl:value-of select="."/><xsl:text>, </xsl:text>
     </xsl:if>
      <xsl:if test="position()=last()">
       <xsl:value-of select="."/>
      </xsl:if>
     </xsl:if>
    </link>
   </xsl:for-each>
  </index-item>
 </xsl:template>
</xsl:stylesheet>

3. Making range

Code:
<xsl:template match="@* | node()">
  <xsl:copy>
   <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
 </xsl:template>
 
 <xsl:template match="index-item">
  <xsl:copy>
   <xsl:for-each-group select="node()" group-adjacent="boolean(self::link[matches(., '^[0-9]+$')])">
    <xsl:choose>
     <xsl:when test="current-grouping-key()">
      <xsl:for-each-group select="current-group()" group-adjacent="number(.) - position()">
       <xsl:for-each select="current-group()">
        <xsl:choose>
         <xsl:when test="position() eq 1 and count(current-group()) eq 1">
          <xsl:copy-of select="current-group()"/>
         </xsl:when>
         <xsl:when test="position() eq 1">
          <link>
           <xsl:copy-of select="@*"/>
           <xsl:value-of select="concat(current-group()[1], '--', current-group()[last()])"/>
          </link>
         </xsl:when>
         <xsl:otherwise>
          <link>
           <xsl:copy-of select="@*"/>
          </link>
         </xsl:otherwise>
        </xsl:choose>
       </xsl:for-each>
      </xsl:for-each-group>
     </xsl:when>
     <xsl:otherwise>
      <xsl:copy-of select="current-group()"/>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:for-each-group>
  </xsl:copy>
 </xsl:template>
Reply With Quote