0

Here is my XML: (it's in Portuguese but I guess it really doesn't matter)

    <xml_tube>
    <utilizadores>
        <utilizador username="Guicky" password="futurama96">
            <nome>Guilherme Luís</nome>
            <birthday>1996-11-28</birthday>
            <foto href="guilhas.jpg"/>
        </utilizador>
        <utilizador username="Daisy" password="woofwoof11">
            <nome>Daisy Luís</nome>
            <birthday>2011-04-04</birthday>
            <foto href="daisy.png"/>
        </utilizador>
        <utilizador username="Anabela65" password="crumble65">
            <nome>Anabela Ribeiro</nome>
            <birthday>1965-04-02</birthday>
            <foto href="belinha.jpg"/>
        </utilizador>
        <utilizador username="Izzie" password="lagartixa">
            <nome>Isadora Luís</nome>
            <birthday>1988-06-27</birthday>
            <foto href="izzie.jpg"/>
        </utilizador>
    </utilizadores>
    <videos_pub>
        <video id="Vid1" publisher="Daisy" duracao="3:37" data="2016-02-29">
            <título>Who let the dogs out? - Baha Men</título>
            <thumb>http://i3.ytimg.com/vi/Qkuu0Lwb5EM/hqdefault.jpg</thumb>
            <descrição>My favorite song, lol.</descrição>
            <hyperlink>https://www.youtube.com/watch?v=Qkuu0Lwb5EM</hyperlink>
            <qualidade pixelization="1080p"/>
            <qualidade pixelization="480p"/>
            <likes liked="Guicky"/>
            <related></related>
        </video>
        <video id="Vid2" publisher="Guicky" duracao="4:01" data="2016-02-29">
            <título>Desiigner - Timmy Turner</título>
            <thumb>http://i3.ytimg.com/vi/JzmRt2VgKYQ/hqdefault.jpg</thumb>
            <descrição>Timmy Timmy Timmy Turner.</descrição>
            <hyperlink>https://www.youtube.com/embed/ProbPpO_8oo</hyperlink>
            <qualidade pixelization="720p"/>
            <qualidade pixelization="1080p"/>
            <likes liked="Anabela65"/>
            <likes liked="Daisy"/>
            <related></related>
        </video>
        <video id="Vid3" publisher="Guicky" duracao="3:47" data="2016-02-29">
            <título>I'm all the way up - Fat Joe</título>
            <thumb>https://www.youtube.com/watch?v=y2ak_oBeC-I</thumb>
            <descrição>Nothing can stop me, I'm all the way up.</descrição>
            <hyperlink>https://www.youtube.com/embed/y2ak_oBeC-I</hyperlink>
            <qualidade pixelization="480p"/>
            <qualidade pixelization="720p"/>
            <related></related>
        </video>
        <video id="Vid4" publisher="Anabela65" duracao="3:21" data="2016-02-29">
            <título>Putzgrilla - Sentadinha</título>
            <thumb>http://i3.ytimg.com/vi/QC4JbIvIhdI/hqdefault.jpg</thumb>
            <descrição>Dá uma sentadinha.</descrição>
            <hyperlink>https://www.youtube.com/watch?v=QC4JbIvIhdI</hyperlink>
            <qualidade pixelization="720p"/>
            <likes liked="Guicky"/>
            <likes liked="Daisy"/>
            <related></related>
        </video>
        <video data="2017-01-13" duracao="02:16" id="Vid5" publisher="Izzie">
            <título>Yann Tiersen - La Valse d'Amelie</título>
            <thumb>http://i3.ytimg.com/vi/uzurqBnALkw/hqdefault.jpg</thumb>
            <descrição>A minha música preferida.</descrição>
            <hyperlink>https://www.youtube.com/watch?v=uzurqBnALkw</hyperlink>
            <qualidade pixelization="720p"/>
            <likes liked="Guicky"/>
            <likes liked="Anabela65"/>
            <related></related>
        </video>
    </videos_pub>
    <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>
            <thumbnail link="http://i3.ytimg.com/vi/QC4JbIvIhdI/hqdefault.jpg"/>
        </lista>
        <lista owner="Anabela65" dataIns="2016-02-29" id="PV2">
            <titulo>Sentadinha!</titulo>
            <descricao>Siting 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>
            <thumbnail link="http://i3.ytimg.com/vi/uMK0prafzw0/hqdefault.jpg"/>
        </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>

Here is my XSLT: (version 1.0)

    <?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"/>
                <a href="https://www.youtube.com">
                    <img id="img" src="img/logos/logo.png"/>
                </a>
                <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>Lista de Utilizadores</h1>
                <xsl:for-each select="xml_tube/utilizadores">
                    <xsl:for-each select="utilizador">
                        <xsl:for-each select="foto">
                            <xsl:element name="img">
                                <xsl:attribute name="src">
                                    <xsl:value-of select="@href"/>
                                </xsl:attribute>
                                <xsl:attribute name="width">20%</xsl:attribute>
                                <xsl:attribute name="height">auto</xsl:attribute>
                            </xsl:element>
                        </xsl:for-each>
                    <xsl:variable name="utilizadores" select="@username"/>
                    <p><strong>Username: </strong><xsl:value-of select="@username"/></p>
                    <p><strong>Nome: </strong> <xsl:value-of select="nome"/></p>
                    <p><strong>Birthday: </strong> <xsl:value-of select="birthday"/></p>
                        <p><strong>Vídeos Publicados:</strong> <xsl:value-of select="xml_tube/videos_pub/video[@publisher=$utilizadores]/título"/></p>
                    </xsl:for-each>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

I'm having a problem with this specific part:

<xsl:variable name="utilizadores" select="@username"/>
        <p><strong>Username: </strong><xsl:value-of select="@username"/></p>
        <p><strong>Nome: </strong> <xsl:value-of select="nome"/></p>
        <p><strong>Birthday: </strong> <xsl:value-of select="birthday"/></p>
            <p><strong>Vídeos Publicados:</strong> <xsl:value-of select="xml_tube/videos_pub/video[@publisher=$utilizadores]/título"/></p>
        </xsl:for-each>

I want to use the @username of the users that the xsl:for-each iterated on and use it to to search for the published vids of that @username.

Expected output:

Videos publicados: Vid1Vid3

My question is: How can i use the xsl:for-each to get me every username and then get me the videos of that specific username

2
  • What isn't working? What is the actual output? What is your question? Commented Jan 13, 2017 at 18:41
  • It doesn't output anything just the <p> part ("Vídeos publicados:"). I just added the question to the post. Commented Jan 13, 2017 at 18:43

1 Answer 1

1

The problem is not with your variable, but with your XPath.. You are in the context of utilizador and from this context the instruction:

<xsl:value-of select="xml_tube/videos_pub/video[@publisher=$utilizadores]/título"/>

selects nothing, because xml_tube is not a child of utilizador.

You should be using an absolute path like:

/xml_tube/videos_pub/video[@publisher=$utilizadores]/título

and you should be using this within an xsl:for-each instruction - unless each utilizador has at most one video.

Even better, use a key to resolve cross-references.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer. Yeah I should've used the absolute path, it's working now though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.