for-each query won´t work

Questions about XML that are not covered by the other forums should go here.
tobi9595
Posts: 4
Joined: Fri Dec 13, 2019 1:32 am

for-each query won´t work

Post by tobi9595 » Sat Jan 04, 2020 8:35 pm

Hello together,

I want to make a query, that shows me every informations about all highshools in one country.
In the following you can see what I´ve tried, but for some reason I get the following error:

Beschreibung: Leading '/' selects nothing: the context item is not a node

Furthermore, if I want to sort the highschools by name, I get the following error too:

A sequence of more than one item is not allowed as the @select attribute of xsl:sort (text("Havard"), text("Oxford University"), text("Stanford University")).

AuslandVKoenigreich.xsl

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    
    <xsl:output method="html"/>   
    <xsl:template match="/">
        <html>
            <head>
                <title>Kontrollblatt Nummer 3</title>
            </head>
            <body> 		
                <h1><xsl:value-of select="./titel"/></h1>
                <h2>Alle Studiengänge</h2>
                <!--Abfrage Nummer 1-->
                <table border="1">
           <tr><th>Auslandshochschule ID</th><th>Name</th><th>Land</th><th>Adresse</th><th>Ort</th><th>URL</th></tr>
                    
               <xsl:for-each select="//AuslandsHochschulen/Auslandshochschule/[land='Vereinigtes Königreich']">
                        <xsl:sort select="//AuslandsHochschulen/name/text()"/>
                          <tr><td><xsl:value-of select="//AuslandsHochschulen/AuslandsHochschulID/text()"/></td>
                              <td> <xsl:value-of select="//AuslandsHochschulen/name/text()"/></td>
				<td> <xsl:value-of select="//AuslandsHochschulen/land/text()"/></td>
				<td> <xsl:value-of select="//AuslandsHochschulen/adresse/text()"/></td>
				<td> <xsl:value-of select="//AuslandsHochschulen/ort/text()"/></td>
				<td> <xsl:value-of select="//AuslandsHochschulen/url/text()"/></td>
                          </tr>
                        </xsl:for-each>
                </table>             
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
Part of the associated XML file:

Code: Select all

<studentenverwaltung
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="StudentenverwaltungXSD2.xsd">
    
    	<AuslandsHochschulen>
            <AuslandsHochschulID>H01</AuslandsHochschulID>
            <name>Havard</name>
            <land>Vereinigte Staaten</land>
            <adresse>MA</adresse>
            <ort>Cambridge</ort>
            <url>https://www.harvard.edu/</url>
     </AuslandsHochschulen> 
    <AuslandsHochschulen>
            <AuslandsHochschulID>H02</AuslandsHochschulID>
            <name>Oxford University</name>
            <land>Vereinigtes Königreich</land>
            <adresse>Oxford OX1 2JD</adresse>
            <ort>Oxford</ort>
            <url>http://www.ox.ac.uk//</url>
    </AuslandsHochschulen>  
        <AuslandsHochschulen>
            <AuslandsHochschulID>H03</AuslandsHochschulID>
            <name>Stanford University</name>
            <land>Vereinigtes Königreich</land>
            <adresse>450 Serra Mall</adresse>
            <ort>Stanford</ort>
            <url>https://www.stanford.edu/</url>
    </AuslandsHochschulen>

</studentenverwaltung>
What am I doing wrong, what do I need to change so that it works fine?

Best regards,
Tobi.

adrian
Posts: 2655
Joined: Tue May 17, 2005 4:01 pm

Re: for-each query won´t work

Post by adrian » Mon Jan 06, 2020 2:51 pm

Hi,

Try:

Code: Select all

<xsl:for-each select="//AuslandsHochschulen[land='Vereinigtes Königreich']">
You'll also want to remove the "//AuslandsHochschulen/" part of the XPath from the inner xsl:value-of/@select.
e.g.

Code: Select all

<xsl:value-of select="AuslandsHochschulID/text()"/>
Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

tobi9595
Posts: 4
Joined: Fri Dec 13, 2019 1:32 am

Re: for-each query won´t work

Post by tobi9595 » Tue Jan 07, 2020 8:35 pm

Hi Adrian,

many thanks to you, another time you saved my a** !! :D

Best regards,
Tobias!

Post Reply