XSLT - Iterate xml and output html li links of child nodes -


i new xslt, me in fixing issue xslt code. trying loop through each xml record , generate li links based on multiple bu , primary metadata of every record xml entry. trying output in html below, document , market bu/primary lines being hyperlinks.

required output:

            title 1 - document bu , primary sample links               document primary title 1               document bu title 1               document bu title 2               document bu title 3               document bu title 4              title 2 - market bu , primary sample links               market primary title 1               market bu title 1               market bu title 2               market bu title 3               market bu title 4 

xslt code:

        <xsl:template match="r">             <tr>               <td>                 <xsl:choose>                         <xsl:when test="starts-with(u, 'http://abc.domain.com/') ">                         <xsl:variable name="count">                             <xsl:value-of select="count(meta[@name='primary' or @name='bu'])"/>                         </xsl:variable>                         <ul class="policy_list">                                 <xsl:call-template name="more_manuals">                                     <xsl:with-param name="baseurl" select="u" />                                     <xsl:with-param name="count" select="$count" />                                 </xsl:call-template>                         </ul>                         <p>                             <xsl:value-of select="$snip" disable-output-escaping='yes' />                         </p>                     </xsl:when>                      <xsl:otherwise>                          <!-- other logic -->                     </xsl:otherwise>                 </xsl:choose>               </td>             </tr>         </xsl:template>           <xsl:template name="other_manuals">                 <xsl:param name="baseurl" />                 <xsl:param name="count" />                  <xsl:call-template name="iterate_meta_data">                     <xsl:with-param name="counter" select="1" />                     <xsl:with-param name="meta" select="meta[@name='primary' or @name='bu'][1]" />                     <xsl:with-param name="baseurl" select="$baseurl" />                     <xsl:with-param name="count" select="$count" />                 </xsl:call-template>         </xsl:template>           <xsl:template name="iterate_meta_data">                 <xsl:param name="counter" />                 <xsl:param name="meta" />                 <xsl:param name="baseurl" />                 <xsl:param name="count" />                  <xsl:choose>                     <xsl:when test="$meta">                         <xsl:variable name="meta_name_1"><xsl:value-of select="$meta/@value"/>_name</xsl:variable>                          <xsl:variable name="meta_name_2"><xsl:value-of select="$meta/@value"/></xsl:variable>                           <xsl:variable name="cs" select="$meta/following-sibling::meta[@name=$meta_name_2]/@value" />                         <xsl:variable name="manualname" select="$meta/following-sibling::meta[@name=$meta_name_1]/@value" />                          <xsl:variable name="ch" select="substring-before($cs, '|')" />                         <xsl:variable name="dns" select="substring-after($cs, '|')" />                          <xsl:variable name="sitename">                             <xsl:call-template name="get_site_name">                                 <xsl:with-param name="dns" select="$dns"/>                             </xsl:call-template>                         </xsl:variable>                          <xsl:variable name="manurl">                             <xsl:variable name="bu">                                 <xsl:if test="$meta/@name = 'bu'">                                     <xsl:value-of select="concat('&amp;bu=', $meta/@value)" disable-output-escaping='yes'/>                                 </xsl:if>                             </xsl:variable>                             <xsl:value-of select="concat($baseurl, '&amp;site=', $dns, '&amp;ch=', $ch, $bu)" disable-output-escaping='yes'/>                         </xsl:variable>                          <li><a href="{$manurl}" onclick="backbuttonsave(this)"><xsl:value-of select="$sitename"/>: <xsl:value-of select="$manualname"/></a></li>                          <xsl:call-template name="iterate_meta_data">                             <xsl:with-param name="counter" select="$counter + 1" />                             <xsl:with-param name="meta" select="$meta/following-sibling::meta[@name='primary' or @name='bu']" />                             <xsl:with-param name="baseurl" select="$baseurl"/>                             <xsl:with-param name="count" select="$count"/>                         </xsl:call-template>                     </xsl:when>                     <xsl:otherwise>                      </xsl:otherwise>                 </xsl:choose>         </xsl:template> 

sample xml entry:

        -          <xml>             <r n="1" l="1" mime="text/html">               <u>http://abc.domain.com/sample</u>                <t>title 1 - document bu , primary sample links</t>                <mt n="ct" v="market" />                <mt n="sites" v="abc" />                <mt n="author" v="amarnath, koduru" />                <mt n="primary" v="asdwer12332334234123" />                <mt n="asdwer12332334234123" v="123456789|abc" />                <mt n="asdwer12332334234123_name" v="document primary title one" />                <mt n="bu" v="werou2309weir23940293" />                <mt n="werou2309weir23940293" v="0909898werweiuouw|cde" />                <mt n="werou2309weir23940293_name" v="document bu title one" />                <mt n="bu" v="xvxddc2309wei345weeffw" />                <mt n="xvxddc2309wei345weeffw" v="ue983479573hww5|cde" />                <mt n="xvxddc2309wei345weeffw_name" v="document bu title two" />                <mt n="bu" v="jljwerwoi798872334oweor" />                <mt n="jljwerwoi798872334oweor" v="hrei9873495739uwe|cde" />                <mt n="jljwerwoi798872334oweor_name" v="document bu title three" />               <mt n="bu" v="nnm23334hweiuroweir2" />                <mt n="nnm23334hweiuroweir2" v="iuw3242ouwejlkeo|cde" />                <mt n="nnm23334hweiuroweir2_name" v="document bu title four" />                  <mt n="tid" v="nt" />                <s><b>...</b>document provide details. share research <b>...</b></s>                <lang>en</lang>            </r>           <r n="2" l="1" mime="text/html">               <u>http://abc.domain.com/sample</u>                <t>title 2 - market bu , primary sample links</t>                <mt n="ct" v="market" />                <mt n="sites" v="abc" />                <mt n="author" v="amarnath, koduru" />                <mt n="primary" v="wwasdwer12332334234123" />                <mt n="wwasdwer12332334234123" v="123456789|abc" />                <mt n="wwasdwer12332334234123_name" v="market primary title one" />                <mt n="bu" v="ddwerou2309weir23940293" />                <mt n="ddwerou2309weir23940293" v="0909898werweiuouw|cde" />                <mt n="ddwerou2309weir23940293_name" v="market bu title one" />                <mt n="bu" v="ggxvxddc2309wei345weeffw" />                <mt n="ggxvxddc2309wei345weeffw" v="ue983479573hww5|cde" />                <mt n="ggxvxddc2309wei345weeffw_name" v="market bu title two" />                <mt n="bu" v="bbjljwerwoi798872334oweor" />                <mt n="bbjljwerwoi798872334oweor" v="hrei9873495739uwe|cde" />                <mt n="bbjljwerwoi798872334oweor_name" v="market bu title three" />               <mt n="bu" v="ssnnm23334hweiuroweir2" />                <mt n="ssnnm23334hweiuroweir2" v="iuw3242ouwejlkeo|cde" />                <mt n="ssnnm23334hweiuroweir2_name" v="market bu title four" />                  <mt n="tid" v="nt" />                <s><b>...</b> market provide details. share research <b>...</b></s>                <lang>en</lang>            </r>         </xml> 

one thing note when using xslt variables immutable. xslt declarative, functional programming language , functional language, variables immutable.

passing in parameters allowed , works setting count variable in first template won't work if relying on value change through iterations.

code sample below provide output below can formatted suit needs css.

title 1 - document bu , primary sample links     primary : asdwer12332334234123     bu : werou2309weir23940293     bu : xvxddc2309wei345weeffw     bu : jljwerwoi798872334oweor     bu : nnm23334hweiuroweir2     s : ...document provide details. share research ...     lang : en  title 2 - market bu , primary sample links     primary : wwasdwer12332334234123     bu : ddwerou2309weir23940293     bu : ggxvxddc2309wei345weeffw     bu : bbjljwerwoi798872334oweor     bu : ssnnm23334hweiuroweir2     s : ... market provide details. share research ...     lang : en 

xml altered style xsl

<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="test.xsl"?>     <xml>       <r n="1" l="1" mime="text/html">         <u>http://abc.domain.com/sample</u>          <t>title 1 - document bu , primary sample links</t>          <mt n="ct" v="market" />          <mt n="sites" v="abc" />          <mt n="author" v="amarnath, koduru" />          <mt n="primary" v="asdwer12332334234123" />          <mt n="asdwer12332334234123" v="123456789|abc" />          <mt n="asdwer12332334234123_name" v="document primary title one" />          <mt n="bu" v="werou2309weir23940293" />          <mt n="werou2309weir23940293" v="0909898werweiuouw|cde" />          <mt n="werou2309weir23940293_name" v="document bu title one" />          <mt n="bu" v="xvxddc2309wei345weeffw" />          <mt n="xvxddc2309wei345weeffw" v="ue983479573hww5|cde" />          <mt n="xvxddc2309wei345weeffw_name" v="document bu title two" />          <mt n="bu" v="jljwerwoi798872334oweor" />          <mt n="jljwerwoi798872334oweor" v="hrei9873495739uwe|cde" />          <mt n="jljwerwoi798872334oweor_name" v="document bu title three" />         <mt n="bu" v="nnm23334hweiuroweir2" />          <mt n="nnm23334hweiuroweir2" v="iuw3242ouwejlkeo|cde" />          <mt n="nnm23334hweiuroweir2_name" v="document bu title four" />            <mt n="tid" v="nt" />          <s><b>...</b>document provide details. share research <b>...</b>  </s>          <lang>en</lang>        </r>        <r n="2" l="1" mime="text/html">         <u>http://abc.domain.com/sample</u>          <t>title 2 - market bu , primary sample links</t>          <mt n="ct" v="market" />          <mt n="sites" v="abc" />          <mt n="author" v="amarnath, koduru" />          <mt n="primary" v="wwasdwer12332334234123" />          <mt n="wwasdwer12332334234123" v="123456789|abc" />          <mt n="wwasdwer12332334234123_name" v="market primary title one" />          <mt n="bu" v="ddwerou2309weir23940293" />          <mt n="ddwerou2309weir23940293" v="0909898werweiuouw|cde" />          <mt n="ddwerou2309weir23940293_name" v="market bu title one" />          <mt n="bu" v="ggxvxddc2309wei345weeffw" />          <mt n="ggxvxddc2309wei345weeffw" v="ue983479573hww5|cde" />          <mt n="ggxvxddc2309wei345weeffw_name" v="market bu title two" />          <mt n="bu" v="bbjljwerwoi798872334oweor" />          <mt n="bbjljwerwoi798872334oweor" v="hrei9873495739uwe|cde" />          <mt n="bbjljwerwoi798872334oweor_name" v="market bu title three" />         <mt n="bu" v="ssnnm23334hweiuroweir2" />          <mt n="ssnnm23334hweiuroweir2" v="iuw3242ouwejlkeo|cde" />          <mt n="ssnnm23334hweiuroweir2_name" v="market bu title four" />            <mt n="tid" v="nt" />          <s><b>...</b> market provide details. share research <b>...</b></s>          <lang>en</lang>        </r>    </xml> 

xslt used create output above using html/css output take data , output in plain text or formatted xml doc.

<?xml version="1.0" encoding="utf-8"?>  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">  <xsl:template match="/"> <html>     <head>         <style>             ul li {                 list-style-type: none;             }         </style>     </head>     <body>                     <xsl:for-each select="xml/r">             <xsl:value-of select="t" />               <ul>                                         <xsl:for-each select="mt">                       <xsl:if test="boolean(@n = 'primary' or @n ='bu')">                         <li>                             <xsl:value-of select="@n" /> : <xsl:value-of select="@v" />                         </li>                       </xsl:if>                   </xsl:for-each>                    <li>s : <xsl:value-of select="s" /></li>                   <li>lang : <xsl:value-of select="lang" /></li>               </ul>                       </xsl:for-each>         </body>   </html> </xsl:template> </xsl:stylesheet> 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -