使用单个xsl文件显示不同的元素

时间:2011-05-15 20:25:00

标签: javascript xml xslt

我有一个包含以下数据的xml文件:

<BOOKS>
<BOOK>
    <TITLE image="./images/govnahDesign.jpg">Bible</TITLE>
    <CATEGORY>Book</CATEGORY>
    <GENRE>Gospel</GENRE>
    <PRICE>0.00</PRICE>
    <SUMMARY>This is the BOOK of life. The truth and the light.</SUMMARY>
    <REVIEW>This indeed the truth and the light. This BOOK is pure fact no fiction.</REVIEW>
</BOOK> 
<BOOK>
    <TITLE image="./images/govnahDesign.jpg">Scrap Book</TITLE>
    <CATEGORY>Novel</CATEGORY>
    <GENRE>Ghetto Gospel</GENRE>
    <PRICE>19.00</PRICE>
    <SUMMARY>This a message from Guvnah B. UK gospel artist doing it big no long things</SUMMARY>
    <REVIEW>I love this message. It gives life a fresh air and makes you love life.</REVIEW>
</BOOK> 
<BOOK>
    <TITLE image="./images/govnahDesign.jpg">Daddy's Boy</TITLE>
    <CATEGORY>Magazine</CATEGORY>
    <GENRE>Gospel</GENRE>
    <PRICE>2.00</PRICE>
    <SUMMARY>This is for Christians who has a father up in Heaven. He really loves his children.</SUMMARY>
    <REVIEW>This makes me want a father like they talking about. He must be a great father. Great magazine.</REVIEW>
</BOOK>

</BOOKS>

我目前正在单独的xsl文件中显示每个文件。 例如:这将是typess ='Book“

的xsl文件
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
    <xsl:for-each select="BOOKS/BOOK">
    <xsl:if test="CATEGORY='Book'"><!-- Show category NOVEL only -->
    <div class="item">
        <xsl:value-of select="TITLE"/>
        <img>
            <xsl:attribute name="src">
                <xsl:value-of select="TITLE//@image"/>
            </xsl:attribute>
        </img>
        Price: <xsl:value-of select="PRICE"/>
        <button id="view" onclick="javascript:viewBook()">View</button>
    </div>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

我的问题是,是否可以只有一个xsl文件,当xsl文件打开时,我可以在if语句中更改类别的值?

<xsl:if test="CATEGORY='Book'">

我用javascript打开xsl文件到主页面上的div。

我已经搜索了几天,现在尝试使用javascript传递值,但无济于事。任何指示,帮助和建议将不胜感激。

谢谢

增加:

根据类别名称打开所需xsl文件的java脚本:

var root=xmlDoc.getElementsByTagName("CATEGORY");
    strVal = new Array();
    for (i=0;i<root.length;++i) {
        var title=(root[i].childNodes[0].nodeValue);
            strVal[i]='<li><a href="#" onClick="javascript:Navigate(\''+xslCatFileName+'\')">'+xslCatFileName+'</a></li>';
                    string = string + strVal[i];
    }

    document.getElementById("category").innerHTML+=string;

以下是上面调用的Navigation()函数:

function Navigate(catName) {
//create an instance of the DOMDocument object.
var xmlDoc=new ActiveXObject ("Msxml2.DOMDocument.6.0");
var xslDoc=new ActiveXObject("Msxml2.DOMDocument.6.0");
var i;
xmlDoc.async=false;
xslDoc.async=false;
//stores the category passed
var xslCatName = catName;
xmlDoc.load("./xml/books.xml");
xslDoc.load("./xml/"+xslCatName+".xsl");
//check wether the process of loading is complete and that there are no errors in the XML document.
if (xmlDoc.readyState == 4 && xmlDoc.parseError.errorCode == 0) {

    var output=xmlDoc.transformNode(xslDoc); //transforms the xml sheet to xslt

    //this clears the div before it loads 
        var y = document.getElementById('showBOOKS'), child;
        while(child=y.firstChild)
        y.removeChild(child);

    document.getElementById("showBOOKS").innerHTML+=output;
}
//if the XML document contains an error, display an error message.
else {
    alert("Failed to load the document. Check wether your XML document is well-formed");
}
}

2 个答案:

答案 0 :(得分:0)

您可以包含一个包含值的小xsl文件:

<xsl:variable name="category" select="'Book'"/>

并在if语句中使用它:

<xsl:if test="CATEGORY=$category">

答案 1 :(得分:0)

如果您使用的函数支持将参数传递给xsl变换,您只需为其定义一个参数,并使用不同的参数值调用相同的xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="CATEGORY" select="Book"/>

<xsl:template match="/">
<xsl:for-each select="BOOKS/BOOK">
<xsl:if test="CATEGORY=$CATEGORY"><!-- Show category NOVEL only -->
<div class="item">
       <xsl:value-of select="TITLE"/>
        <img>
            <xsl:attribute name="src">
                <xsl:value-of select="TITLE//@image"/>
            </xsl:attribute>
        </img>
        Price: <xsl:value-of select="PRICE"/>
        <button id="view" onclick="javascript:viewBook()">View</button>
    </div>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

这是 XSLT 方面。你在找这个吗?


在javascript方面,您需要在调用转换之前添加参数,例如:

xslDoc.addParameter("CATEGORY", "Novel");

我认为您的xslDoc也使用了错误的API。请参阅此example