Announcement

Collapse
No announcement yet.

apply-templates klappt nicht, wie es soll

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • apply-templates klappt nicht, wie es soll

    Hallo!

    Ich lerne gerade anhand des w3school-Beispiels XSLT.

    Der XML-Code:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Edited by XMLSpy -->
    <catalog>
    	<cd>
    		<title>Empire Burlesque</title>
    		<artist>Bob Dylan</artist>
    		<country>USA</country>
    		<company>Columbia</company>
    		<price>10.90</price>
    		<year>1985</year>
    	</cd>
    usw...
    </catalog>
    Mein XSLT 2.0-Code

    Code:
    <?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" 
    xmlns:fn="http://www.w3.org/2005/xpath-functions" 
    xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" 
    xmlns:err="http://www.w3.org/2005/xqt-errors" 
    version="2.0"
    exclude-result-prefixes="xs xdt err fn">
    <xsl:template match="/">
      <html>
      <body>
      <h2>My CD Collection</h2>  
      <xsl:apply-templates select="cd[1]"/>  
      </body>
      </html>
    </xsl:template>
    
    <xsl:template match="cd[1]">
      <p>
    <span><xsl:value-of select="title"/></span>
      </p>
    </xsl:template>
    
    </xsl:stylesheet>
    Output:

    Code:
    <html>
    	<body>
    		<h2>My CD Collection</h2>
    	</body>
    </html>
    Wieso wird da nicht der Titel der ersten CD angezeigt?

    So wie ich es verstehe, steht das erste
    Code:
    template match="/"
    für
    Code:
    <catalog>
    und bildet den Kontext für das
    Code:
    template match="cd[1]"
    , d.h. das
    Code:
    template match="cd[1]"
    sollte als
    Code:
    match="catalog/cd[1]"
    interpretiert werden.

    Dann sollte der prozessor das template für
    Code:
    cd[1]
    finden und dann relativ dazu den
    Code:
    title
    auswählen, sodass die gesamte Query folgendermaßen lautet:
    Code:
    catalog/cd[1]/title
    .

    Wieso funktioniert das nicht?

  • #2
    Mach aus
    Code:
    <xsl:template match="/">
    Code:
    <xsl:template match="/catalog">
    dann wird das Wurzelelement auch richtig verarbeitet. Nur match="/" vearbeitet den Dokumentknoten, der den Kommentar und das "catalog"-Element enthält, aber eben nicht die "cd"-Elemente.

    Comment


    • #3
      aaah okay... das stellt den Wurzelknoten dar. Danke!!

      Comment

      Working...
      X