asp.net - XSLT 1.0 Substring then select distinct -


i quite new xslt, appreciated. below sample xml file.

 <documentelement>      <records>        <date>2014-07-01 00:00</date>     </records>      <records>        <date>2014-08-03 00:00</date>     </records>      <records>        <date>2013-08-03 00:00</date>     </records>  <documentelement> 

what need select distinct years dates.

currently have below xslt brings duplicate years.

<?xml version="1.0" encoding="utf-8"?>  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:ms="urn:schemas-microsoft-com:xslt">  <xsl:output method="xml" indent="yes"/>    <xsl:template match="/">  <xsl:variable name="years" select="documentelement/records/date"/>  <ul>   <xsl:for-each select="$years">     <li>       <xsl:element name="a">          <xsl:attribute name="href">           <xsl:value-of select="concat('?archive=',substring( ., 1, 4))"/>         </xsl:attribute>          <xsl:value-of select="substring( ., 1, 4)"/>       </xsl:element>      </li>   </xsl:for-each> </ul>   </xsl:template>   </xsl:stylesheet> 

which gives me results below:

<ul>   <li>     <a href="?archive=2014">2014</a>  </li>  <li>   <a href="?archive=2014">2014</a>  </li>  <li>    <a href="?archive=2014">2013</a>   </li>  </ul> 

but expected result should be

  <ul>   <li>     <a href="?archive=2014">2014</a>  </li>   <li>    <a href="?archive=2014">2013</a>   </li>  </ul> 

i tried following below empty output

   <?xml version="1.0" encoding="utf-8"?>       <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:ms="urn:schemas-microsoft-com:xslt">   <xsl:output method="xml" indent="yes"/>     <xsl:template match="/">     <xsl:variable name="years" select="substring(documentelement/records/date, 1, 4)"/>     <ul>   <xsl:for-each select="$years[not(.=preceding::*)]">     <li>       <xsl:element name="a">          <xsl:attribute name="href">           <xsl:value-of select="concat('?archive=',substring( ., 1, 4))"/>         </xsl:attribute>          <xsl:value-of select="substring( ., 1, 4)"/>       </xsl:element>      </li>   </xsl:for-each> </ul> </xsl:template>     </xsl:stylesheet> 

any appreciated. thanks.

muenchian grouping more effective way of grouping in xslt1.0 using preceding/following/preceding-sibling/following-sibling axis. try this:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" > <xsl:output method="xml" indent="yes"/> <xsl:key name="date" match="documentelement/records/date" use="substring(.,1,4)"/> <xsl:template match="/">     <ul>         <xsl:for-each select="documentelement/records/date[generate-id() = generate-id(key('date', substring(.,1,4))[1])]">             <li>                 <a href="{concat('?archive=',substring(.,1,4))}">                     <xsl:value-of select="substring( ., 1, 4)"/>                 </a>             </li>         </xsl:for-each>     </ul> </xsl:template> </xsl:stylesheet> 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -