xml - XSLT: Reuse a template within another template -


i have xslt file displays title , artist name of cd. have 3 templates, first template displays title, second template displays artist name, , third template contains hidden table. hidden table displayed using checkbox. want reuse title , artist templates in order show them table data inside third template when visible.

the problem third template doesn't show on browser because don't know assign match attribute. highly appreciated. thanks.

expected result: http://oi61.tinypic.com/6ibkur.jpg![1] index.html

<html> <head> <script> function loadxmldoc(filename) { if (window.activexobject)   {   xhttp = new activexobject("msxml2.xmlhttp");   } else    {   xhttp = new xmlhttprequest();   } xhttp.open("get", filename, false); try {xhttp.responsetype = "msxml-document"} catch(err) {} // helping ie11 xhttp.send(""); return xhttp.responsexml; }  function displayresult() { xml = loadxmldoc("cdcatalog.xml"); xsl = loadxmldoc("cdcatalog_apply.xsl"); // code ie if (window.activexobject || xhttp.responsetype == "msxml-document")   {   ex = xml.transformnode(xsl);   document.getelementbyid("example").innerhtml = ex;   } // code chrome, firefox, opera, etc. else if (document.implementation && document.implementation.createdocument)   {   xsltprocessor = new xsltprocessor();   xsltprocessor.importstylesheet(xsl);   resultdocument = xsltprocessor.transformtofragment(xml, document);   document.getelementbyid("example").appendchild(resultdocument);   } } </script> </head> <body onload="displayresult()"> <div id="example" /> </body> </html> 

cdcatalog.xml

<?xml version="1.0" encoding="utf-8"?>  <catalog>     <cd>         <title>empire burlesque</title>         <artist>bob dylan</artist>     </cd>     <cd>         <title>hide heart</title>         <artist>bonnie tyler</artist>     </cd>     <cd>         <title>greatest hits</title>         <artist>dolly parton</artist>     </cd> </catalog> 

cdcatalog_apply.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">      <xsl:template match="/">     <html>     <head>     <style type="text/css">       .pdesc{     display:none;     }      input[type=checkbox]:checked + .pdesc {       display: block;     }      </style>     </head>     <body>        <h1>cd collection</h1>       <xsl:apply-templates select="catalog/cd">       </xsl:apply-templates>      </body>     </html>     </xsl:template>       <xsl:template match="cd">     <tr >     <xsl:apply-templates select="title"/>     <xsl:apply-templates select="artist"/>     </tr>     </xsl:template>       <xsl:template match="title" name="title">     <p>     <xsl:value-of select="."/>     </p>     </xsl:template>       <xsl:template match="artist" name="artist">     <p>     <xsl:value-of select="."/>     </p>     </xsl:template>      <xsl:template match="x"><!-- don't know put in match -->     <label >show more</label>     <input type="checkbox" ></input>      <div class="pdesc">      <h2>cd details</h2>      <table border="1" >     <tr bgcolor="#9acd32">       <th>title</th>       <th>artist</th>     <tr>       <td> <xsl:call-template name="title"/></td>       <td> <xsl:call-template name="artist"/></td>     </tr>      </tr>     </table>      </div>     </xsl:template>   </xsl:stylesheet> 

you can't have 2 templates matching same element without sort of condition, can't match on "cd" in third template.

what though, give third template "mode" attribute

<xsl:template match="cd" mode="hidden"> 

however, need have second xsl:apply-templates specified mode.

 <xsl:apply-templates select="catalog/cd" />  <xsl:apply-templates select="catalog/cd" mode="hidden" /> 

or perhaps call in template matches "cd" currently:

<xsl:template match="cd">     <tr>         <xsl:apply-templates select="title"/>         <xsl:apply-templates select="artist"/>         <xsl:apply-templates select="." mode="hidden" />     </tr> </xsl:template> 

note, in "hidden" template, don't need xsl:call-templates doing. can <xsl:apply-templates select="title"/> again in first template.

alternatively, remove first template, , use xsl:for-each instead of first <xsl:apply-templates select="cd">. away need "mode" attribute.

<xsl:for-each select="catalog/cd">     <tr>          <xsl:apply-templates select="title"/>          <xsl:apply-templates select="artist"/>          <xsl:apply-templates select="."  />       </tr>  </xsl:for-each> 

of course, need 2 templates @ here? couldn't combine them one?

<xsl:template match="cd">     <p>         <xsl:apply-templates select="title"/>         <xsl:apply-templates select="artist"/>     </p>     <label>show more</label>     <input type="checkbox" />     <div class="pdesc">         <h2>cd details</h2>          <table border="1" >         <tr bgcolor="#9acd32">           <th>title</th>           <th>artist</th>         <tr>           <td><xsl:apply-templates select="title"/></td>           <td><xsl:apply-templates select="artist"/></td>         </tr>         </tr>         </table>     </div> </xsl:template> 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -