无法使用 xslt 文件在 xml 中放置 href 链接以扩展结果 [英] Unable to put href links to expand results in xml using xslt file

查看:13
本文介绍了无法使用 xslt 文件在 xml 中放置 href 链接以扩展结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个 xslt 文件,用于来自 xml 的房屋名称列表视图.当从列表中单击房屋名称时,它应该显示同一 xml 文件中房屋的完整详细信息.但就我而言,它正在重定向到另一个地方.有人可以帮我吗?

<房屋><搜索><id>1</id><name>恐怖</name><address>09, West Road</address><city>伦敦</city><pcode>se4 7jk</pcode><联系方式>020574110832</联系方式></搜索></房屋>

以及xslt文件的问题部分

<xsl:attribute name="href"><xsl:value-of select="//search/name"/></xsl:attribute></xsl:模板>

解决方案

我假设您希望为每个名称添加一个链接.在这种情况下,您将需要类似

<a><xsl:attribute name="href"><xsl:text>#</xsl:text><xsl:value-of select="."/></xsl:attribute><xsl:value-of select="."/></a></xsl:模板>

这将创建像 horror</a>

这样的元素

编辑

好的,下面是一个表格示例,其中 div 元素可通过单击按钮折叠/展开.我留给你做的漂亮的布局;它只是作为如何使用 xslt 创建 html 的示例,包括 javascript 切换隐藏/显示.

<头><title>试用</title><script type="text/javascript" language="javascript">//根据id展开和折叠功能函数 toggleById(IdStart) {var divs = document.getElementsByTagName('div');for (var i=0<xsl:text>;</xsl:text> i&lt;divs.length<xsl:text>;</xsl:text> i++) {if (divs[i].id.match("^"+IdStart) == IdStart) {if( divs[i].style.display == "none"){divs[i].style.display = "块"}别的    {divs[i].style.display = "无"}}}返回真;}<身体><表格边框="纯黑色 1pt;"style="border-collapse:collapse;padding:0;border-spacing:0"><tr><th>房屋名称</th><th>细节</th></tr><xsl:apply-templates/></html></xsl:模板><xsl:template match="name"><tr><td><输入><xsl:attribute name="type">button</xsl:attribute><xsl:attribute name="onclick">toggleById('<xsl:value-of select="."/>');return true;</xsl:attribute><xsl:attribute name="onMouseOver">this.style.cursor='hand'</xsl:attribute><xsl:attribute name="value"><xsl:value-of select="."/></xsl:attribute></input></td><td><div><xsl:attribute name="id"><xsl:value-of select="."/></xsl:attribute><xsl:attribute name="style">display:none</xsl:attribute><xsl:copy-of select=".."/>

</td></tr></xsl:模板><xsl:template match="text()"/></xsl:stylesheet>

适用于

这给了

<头><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>试用</title><script type="text/javascript" language="javascript">//根据id展开和折叠功能函数 toggleById(IdStart) {var divs = document.getElementsByTagName('div');for (var i=0; i

<身体><表格边框="纯黑色 1pt;"style="border-collapse:collapse;padding:0;border-spacing:0"><tr><th>房屋名称</th><th>细节</th></tr><tr><td><input type="button" onclick="toggleById('horror'); return true;"onMouseOver="this.style.cursor='hand'" value="horror"></td><td><div id="horror" style="display:none"><搜索><id>1</id><name>恐怖</name><address>09, West Road</address><city>伦敦</city><pcode>se4 7jk</pcode><联系方式>020574110832</联系方式></搜索>

</td></tr><tr><td><input type="button" onclick="toggleById('nice'); return true;"onMouseOver="this.style.cursor='hand'" value="nice"></td><td><div id="nice" style="display:none"><搜索><id>2</id><name>nice</name><address>05, East Road</address><city>伦敦</city><pcode>po4 3df</pcode><联系>无</联系></搜索>

</td></tr></html>

在浏览器中尝试一下,您将体验展开/折叠行为.

I designed a xslt file for a list view of house names from xml. And when a house name is clicked from the list, it should show full detail of the house which is in same xml file. But in my case it's redirecting to another place. Can someone help me?

<Houses>
    <search>
        <id>1</id>
        <name>horror</name>
        <address>09, west Road</address>
        <city>London</city>
        <pcode>se4 7jk</pcode>
        <contact>020574110832</contact>
        </search>
</Houses>

And the problem part of xslt file

<xsl:template match="name">
     <xsl:attribute name="href">
     <xsl:value-of select="//search/name"/>
     </xsl:attribute>
</xsl:template>

I assume you wish to add a link to each name. In that case you will need something like

<xsl:template match="name">
    <a>
        <xsl:attribute name="href">
            <xsl:text>#</xsl:text><xsl:value-of select="."/>
        </xsl:attribute>
        <xsl:value-of select="."/>
    </a>
</xsl:template>

This will create elements like <a href="#horror">horror</a>

EDIT

OK here's an example of a table with div elements that collapse/expand by clicking the button. Nice layout I left to do for you; it's just meant as an example of how to create an html using xslt including javascript toggle hide/show.

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

    <xsl:template match="/">
        <html>
            <head>
                <title>trial</title>
                <script type="text/javascript" language="javascript">
                    //expand and collapse functions based on id
                    function toggleById(IdStart) {
                    var divs = document.getElementsByTagName('div');
                    for (var i=0<xsl:text>;</xsl:text> i&lt;divs.length<xsl:text>;</xsl:text> i++) {
                    if (divs[i].id.match("^"+IdStart) == IdStart) { 
                        if( divs[i].style.display == "none")
                            {
                            divs[i].style.display = "block"
                            }
                        else    {
                            divs[i].style.display = "none"
                            }
                    }
                    }
                    return true;
                    }

                </script>
            </head>
            <body>
                <table border="solid black 1pt;" style="border-collapse:collapse;padding:0;border-spacing:0">
                    <tr>
                        <th>house name</th>
                        <th>details</th>
                    </tr>
                    <xsl:apply-templates/>
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="name">
        <tr>
            <td>
                <input>
                    <xsl:attribute name="type">button</xsl:attribute>
                    <xsl:attribute name="onclick">toggleById('<xsl:value-of select="."/>'); return true;</xsl:attribute>
                    <xsl:attribute name="onMouseOver">this.style.cursor='hand'</xsl:attribute>
                    <xsl:attribute name="value"><xsl:value-of select="."/></xsl:attribute>
                </input>
            </td>
            <td>
                <div>
                    <xsl:attribute name="id"><xsl:value-of select="."/></xsl:attribute>
                    <xsl:attribute name="style">display:none</xsl:attribute>
                    <xsl:copy-of select=".."/>
                </div>
            </td>
        </tr>
    </xsl:template>

    <xsl:template match="text()"/>

</xsl:stylesheet>

When applied to

<?xml version="1.0" encoding="UTF-8"?>
<Houses>
    <search>
        <id>1</id>
        <name>horror</name>
        <address>09, west Road</address>
        <city>London</city>
        <pcode>se4 7jk</pcode>
        <contact>020574110832</contact>
    </search>
    <search>
        <id>2</id>
        <name>nice</name>
        <address>05, East Road</address>
        <city>London</city>
        <pcode>po4 3df</pcode>
        <contact>none</contact>
    </search>
</Houses>

This gives

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>trial</title>
        <script type="text/javascript" language="javascript">
                    //expand and collapse functions based on id
                    function toggleById(IdStart) {
                    var divs = document.getElementsByTagName('div');
                    for (var i=0; i<divs.length; i++) {
                    if (divs[i].id.match("^"+IdStart) == IdStart) { 
                        if( divs[i].style.display == "none")
                            {
                            divs[i].style.display = "block"
                            }
                        else    {
                            divs[i].style.display = "none"
                            }
                    }
                    }
                    return true;
                    }

                </script>
    </head>
    <body>
        <table border="solid black 1pt;" style="border-collapse:collapse;padding:0;border-spacing:0">
            <tr>
                <th>house name</th>
                <th>details</th>
            </tr>
            <tr>
                <td><input type="button" onclick="toggleById('horror'); return true;" onMouseOver="this.style.cursor='hand'" value="horror"></td>
                <td>
                    <div id="horror" style="display:none">
                        <search>
                            <id>1</id>
                            <name>horror</name>
                            <address>09, west Road</address>
                            <city>London</city>
                            <pcode>se4 7jk</pcode>
                            <contact>020574110832</contact>
                        </search>
                    </div>
                </td>
            </tr>
            <tr>
                <td><input type="button" onclick="toggleById('nice'); return true;" onMouseOver="this.style.cursor='hand'" value="nice"></td>
                <td>
                    <div id="nice" style="display:none">
                        <search>
                            <id>2</id>
                            <name>nice</name>
                            <address>05, East Road</address>
                            <city>London</city>
                            <pcode>po4 3df</pcode>
                            <contact>none</contact>
                        </search>
                    </div>
                </td>
            </tr>
        </table>
    </body>
</html>

Try it out in a browser and you will experience the expand/collapse behavior.

这篇关于无法使用 xslt 文件在 xml 中放置 href 链接以扩展结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆