有没有办法在XSLT的for-each循环中使用{}?

时间:2018-12-29 00:02:29

标签: html xml xslt xpath xslt-2.0

我正在尝试使用xslt创建html页面,现在我遇到了使用for-each元素的包版广告。问题所在的行如下

<xsl:for-each select="../../*:subscritores/subscritor[@userID = '{@userID}']/*:video">

当代码的前面部分清楚地标记了用户时,有人知道我如何让for-each循环针对不同的用户运行吗?

如果我尝试简单地打印出@userID,我会得到正确的结果,这意味着它应该可以正常工作。

我尝试解析xpath,它返回了节点列表(如前所述)。我还尝试通过替换{@userID}和有效的用户ID来强行使用它,并且效果很好,告诉我问题确实出在那部分。

完整代码在这里:

XML-https://pastebin.com/CBhmUPRQ

XSL-https://pastebin.com/Jzb3yG3N

XSL(导入的文件)-https://pastebin.com/NfVGxwy4

此外,大多数数据都是用葡萄牙语编写的,其余数据完全是乱码。如有需要,无论您有什么需要,我都会很乐意翻译。

编辑-如有必要,DTD在此处https://pastebin.com/6fHdTset

1 个答案:

答案 0 :(得分:1)

通常,您似乎具有与属性值相关的变量元素,并且希望遵循基于属性值的交叉引用,因此XSLT中最有效的方法是使用xsl:key声明键,然后使用key函数遵循交叉引用,例如

<xsl:key name="subscritor-por-id" match="subscritor" use="@userID"/>
<xsl:key name="video-por-id" match="video" use="@videoID"/>
<xsl:key name="comentario-por-user" match="comentario" use="de/@userID"/>

然后代码(没有包含,但应显示原理方法)变为

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

    <xsl:output method="xhtml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:output doctype-public="-//W3C//DTD XHTML 1.1//EN" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <html xml:lang="pt">
            <head>
                <title>Pagina Utilizador - Vista Longa</title>
                <link rel="stylesheet" type="text/css" href="../CSS.css" charset="UTF-8"/>
            </head>
            <body>
                <div id="barra_de_navegacao">
                    <a href="../main.xhtml" class="button1">Pagina Inicial</a>
                    <a href="../listas/videos.html" class="button1">Vídeos</a>
                    <a href="../listas/lista_videos.html" class="button1">Lista de videos</a>
                    <a href="../listas/lista_users.html" class="button1">Lista de utilizadores</a>
                </div>
                <div class="display" id="user">
                    <xsl:apply-templates select="//user[@userID = 'user001']"/>
                    <!-- Para mudar utilizador, mudar aqui-->
                </div>
            </body>
        </html>
    </xsl:template>

    <xsl:key name="subscritor-por-id" match="subscritor" use="@userID"/>
    <xsl:key name="video-por-id" match="video" use="@videoID"/>
    <xsl:key name="comentario-por-user" match="comentario" use="de/@userID"/>

    <xsl:template match="user">
        <xsl:apply-imports/>
        <div class="user_subscricoes">
            <h1>
                <xsl:value-of select="@userID"/>
            </h1>
            <h3>Subscrições</h3>
            <h3>Videos</h3>
            <ul>
                <!-- Comecando em user, precisamos subir na arvore, até á raiz para depois poder descer em direção aos subscritores-->
                <xsl:for-each select="key('subscritor-por-id', @userID)/*:video">
                    <li>
                        <a href="../videos/{@videoID}.xhtml" class="button2">
                            <xsl:value-of select="key('video-por-id', @videoID)/nome"/>
                        </a>
                    </li>
                </xsl:for-each>
            </ul>
        </div>
        <div id="user_comentarios">
            <!---->
            <h1>
                <xsl:value-of select="@userID"/>
            </h1>
            <!---->
            <h3>Comentários</h3>
            <ul>
                <xsl:for-each select="key('comentario-por-user', @userID)">
                    <li>
                        <div class="button2">
                            <a href="../Vídeos/socialtube-vid001.xhtml"> <xsl:value-of select="/texto"/></a>
                        </div>
                    </li>
                </xsl:for-each>
            </ul>
        </div>
    </xsl:template>
</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/6qVRKxn/1