amarnath k
amarnath k

Reputation: 5

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

I am new to the XSLT, could you help me in fixing the issue with my xslt code. I am trying to loop through the each xml record and generate the li links based on the multiple bu and primary metadata of every Record xml entry. I am trying to output in HTML as below, document and market bu/primary lines being hyperlinks.

Required Output:

            Title 1 - Document BU and Primary sample links
              Document primary title one
              Document bu title one
              Document bu title two
              Document bu title three
              Document bu title four

            Title 2 - Market BU and Primary sample links
              Market primary title one
              Market bu title one
              Market bu title two
              Market bu title three
              Market bu title four

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> 
                        <!-- Some 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 and 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 your 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 and 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 your research <b>...</b></S> 
              <LANG>en</LANG> 
          </R>
        </xml>

Upvotes: 0

Views: 159

Answers (1)

rmeier
rmeier

Reputation: 67

One thing to note when using xslt is that variables are immutable. XSLT is a declarative, functional programming language and as for any functional language, variables are immutable.

Passing in parameters is allowed and works but setting a count variable in your first template won't work if you are relying on that value to change through iterations.

Code Sample Below to provide output below that can be formatted to suit your needs with CSS.

Title 1 - Document BU and Primary sample links
    primary : asdwer12332334234123
    bu : werou2309weir23940293
    bu : xvxddc2309wei345weeffw
    bu : jljwerwoi798872334oweor
    bu : nnm23334hweiuroweir2
    S : ...Document Provide details. Share your research ...
    LANG : en

Title 2 - Market BU and Primary sample links
    primary : wwasdwer12332334234123
    bu : ddwerou2309weir23940293
    bu : ggxvxddc2309wei345weeffw
    bu : bbjljwerwoi798872334oweor
    bu : ssnnm23334hweiuroweir2
    S : ... Market Provide details. Share your research ...
    LANG : en

XML altered to style with 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 and 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 your 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 and 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 your research <b>...</b></S> 
        <LANG>en</LANG> 
      </R>
   </xml>

XSLT used to create the output above using HTML/CSS for output you could easily take the data and just output it in plain text or formatted to another 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>

Upvotes: 2

Related Questions