xsl:for-each仅返回第一个节点

时间:2017-01-09 18:46:00

标签: xml xslt

我在使用XSLT时遇到了一些麻烦,这是我的XML代码:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE xml_tube SYSTEM "C:\Users\Asus\Desktop\LEIM\tps\tp2\Um utilizador\Utilizador\xml__tube.dtd">

<?xml-stylesheet type="text/xsl" href="utilizadorG.xsl" ?> 

<xml_tube>
    <playlist>
        <lista owner="Guicky" dataIns="2016-10-24" id="PV1">
            <titulo>BEST MUSIC.</titulo>
            <descricao>Compilation of my favourite music videos.</descricao>
            <gostosL gostouL="Guicky"/>
            <links_vid vid="Vid2"/>
            <links_vid vid="Vid3"/>
            <administradores>
                <admin ref="Guicky"/>
                <admin ref="Daisy"/>
            </administradores>
            <editores>
                <editor ref="Guicky"/>
            </editores>
            <subscritores>
                <subs ref="Daisy"/>
                <subs ref="Anabela65"/>
            </subscritores>
        </lista>
        <lista owner="Anabela65" dataIns="2016-02-29" id="PV2">
            <titulo>Cooking Lessons!</titulo>
            <descricao>Cooking lesson's with Guicky's mom!</descricao>
            <links_vid vid="Vid4"/>
            <administradores>
                <admin ref="Anabela65"/>
                <admin ref="Guicky"/>
            </administradores>
            <editores>
                <editor ref="Anabela65"/>
            </editores>
            <subscritores>
                <subs ref="Guicky"/>
                <subs ref="Daisy"/>
            </subscritores>
        </lista>
    </playlist>
    <comentarios>
        <comentario id="C1" refV="Vid1" user="Guicky" data="2016-10-23">
            <text>AHAHAHAHA, bom vídeo.</text>
            <gosto gostou="Daisy"/>
            <respostas>
                <texto autor="Daisy">Grande clássico!</texto>
            </respostas>
        </comentario>
        <comentario id="C2" refL="Vid2" user="Anabela65" data="2016-10-22">
            <text>Timmy timmy timmy turner...</text>
            <gosto gostou="Guicky"/>
            <gosto gostou="Daisy"/>
            <respostas>
                <texto autor="Guicky">U know it.</texto>
                <resposta autor="Daisy">LOL!</resposta>
            </respostas>
        </comentario>
    </comentarios>
</xml_tube>`

这是我的XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" 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:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="eng">
            <head>
                <title>My XML Tube</title>
                <link rel="stylesheet" type="text/css" href="XML_Tube.css" charset="UTF-8"/>
                <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"/>
                <link rel="icon" 
                      type="image.png"
                      href="favicon-16.png"/>   
            </head>
            <body>
                <div class="spacer"/>
                <img id="img" src="img/logos/logo.png"/>
                <nav>
                    <ul>
                        <li><a href="home.html">Playlists</a>
                            <ul>
                                <li><a href="../XMLTube.xml#body">Playlist</a></li>
                                <li><a href="">Playlists</a></li>
                            </ul>
                        </li>
                        <li><a href="portfolio.html">Utilizadores</a>
                            <ul>
                                <li><a href="Portfolio.html#NOSLeague">Um utilizador</a></li>
                                <li><a href="Portfolio.html#PremierLeague">Lista geral de utilizadores</a></li>
                            </ul>
                        </li>
                        <li><a href="contacts.html">Vídeos</a>
                            <ul>
                                <li><a href="Contacts.html#gui">Vídeo</a></li>
                                <li><a href="Contacts.html#rodri">Lista geral de vídeos</a></li>
                            </ul>
                        </li>
                    </ul>
                </nav>
                <h1 id="lala">Playlists</h1>
                <section>
                <xsl:for-each select="xml_tube/playlist/lista">
                <article class="list">
                    <figure>
                        <xsl:element name="img">
                            <xsl:attribute name="src">
                                <xsl:value-of select="thumbnail/@link"/>
                            </xsl:attribute>
                            <xsl:attribute name="width">50%</xsl:attribute>
                            <xsl:attribute name="height">auto</xsl:attribute>
                        </xsl:element>
                        <figcaption>
                            <a href="https://www.google.com"><h1><xsl:value-of select="titulo"/></h1></a>
                            <p><strong>Nome de utilizador: </strong>  <xsl:value-of select="@owner"/></p>
                            <p><strong>Descrição: </strong>  <xsl:value-of select="descricao"/></p>
                            <p><strong>Administradores:</strong>  <xsl:value-of select="administradores/admin/@ref"/></p>
                            <p><strong>Editores: </strong>  <xsl:value-of select="editores/editor/@ref"/></p>
                            <p><strong>Subscritores: </strong> <xsl:value-of select="subscritores/subs/@ref"/>
                            <xsl:text></xsl:text>
                            </p>


                        </figcaption>
                    </figure>
                </article>
                </xsl:for-each>
                </section>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

基本上我的问题是xsl:for-each只给我第一个属性@subs。我对administradores/@ref也有同样的问题。我是葡萄牙语,这是学校工作的一部分,我没有时间翻译每个节点,但我认为这应该不是问题。无论如何,提前谢谢!

我的预期输出是:

Subscritores: Daisy
Subscritores: Anabela

1 个答案:

答案 0 :(得分:1)

在XSLT 1.0中 - 显然是您正在使用的 - xsl:value-of指令仅返回所选节点集中第一个节点的值。

如果要列出所有值,您需要逐个获取它们:例如而不是:

<xsl:value-of select="administradores/admin/@ref"/>

使用:

<xsl:for-each select="administradores/admin">
    <xsl:value-of select="@ref" />
    <xsl:text> </xsl:text>
</xsl:for-each> 
相关问题