2014-12-05 83 views
0

我有一个XSLT文件,显示CD的标题和艺术家名称。我有3个模板,第一个模板显示标题,第二个模板显示艺术家名称,第三个模板包含隐藏表格。隐藏表格使用复选框显示。我想重用标题和艺术家模板,以便在第三个模板可见时将其显示为表格数据。XSLT:在另一个模板中重新使用模板

问题是第三个模板没有显示在浏览器上,因为我不知道要分配给匹配属性。任何帮助,高度赞赏。谢谢。

预期结果: 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 for IE 
if (window.ActiveXObject || xhttp.responseType == "msxml-document") 
    { 
    ex = xml.transformNode(xsl); 
    document.getElementById("example").innerHTML = ex; 
    } 
// code for 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 your 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"><!-- I don't know what to put in the 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> 
+0

这里的预期结果是什么? – 2014-12-05 17:52:55

+0

嗨michael.hor257k,我已经更新了我的帖子,预期结果。所以当复选框被选中时,表格变得可见。 – user2994263 2014-12-05 18:05:52

+2

而不是在浏览器中显示HTML的视图,你可以告诉我们预期的输出源代码? – 2014-12-05 18:22:32

回答

0

你不能有两个模板完全匹配相同的元素而没有一些吸引力因此你不能在你的第三个模板中匹配“cd”。

什么,你可以这样虽然是给你的第三个模板“mode”属性

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

但是,你则需要有在您指定的模式的第二xsl:apply-templates

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

或许你可以把它在符合“CD”当前模板:

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

注意,在这个“隐藏”的模板,你并不真的需要做xsl:call-templates你目前正在做。您可以在第一个模板中再次执行<xsl:apply-templates select="title"/>

或者,您可以删除第一个模板,并使用xsl:for-each而不是第一个<xsl:apply-templates select="cd">。这将消除对“模式”属性的需求。

<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> 

当然,你真的需要两个模板吗?你不能把它们合并成一个吗?

<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>