user3374953
user3374953

Reputation: 3

Basic XML/XSLT : applying template to several elements

I am trying to teach myself XSLT. This may be a very basic question but after a lot of searching here, and across other sites on the web, I have still been unable to solve this problem.

I have been trying to recreate the scenario described in this question: How can you deal with embedded XML tags in XSLT?

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.html.xsl"?>
<favoriteMovies>
  <favoriteMovie>the <i>Star Wars</i> saga</favoriteMovie>
</favoriteMovies>

Here is the XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <html>
    <head />
    <body>
     <xsl:apply-templates/>
    </body>
  </html>
 </xsl:template>

 <xsl:template match="/*">
  <ul>
   <xsl:apply-templates/>
  </ul>
 </xsl:template>

 <xsl:template match="favoriteMovie">
  <li><xsl:copy-of select="node()"/></li>
 </xsl:template>
</xsl:stylesheet>

This works fine for this particular example. But when I add another "favourite movie"...

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.html.xsl"?>
<favoriteMovies>
  <favoriteMovie>the <i>Star Wars</i> saga</favoriteMovie>
</favoriteMovies>
<favoriteMovies>
  <favoriteMovie>the <i>Godfather</i> trilogy</favoriteMovie>
</favoriteMovies>

all I get is an error...

XML Parsing Error: junk after document element Location: localhost:8888/test.xml Line Number 6, Column 1: ^

What would I need to change to make this work?

Upvotes: 0

Views: 52

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167516

Any XML document needs to have a single top level root element containing all other elements so you need e.g.

<root>
<favoriteMovies>
  <favoriteMovie>the <i>Star Wars</i> saga</favoriteMovie>
</favoriteMovies>
<favoriteMovies>
  <favoriteMovie>the <i>Godfather</i> trilogy</favoriteMovie>
</favoriteMovies>
</root>

or

<favoriteMovies>
 <favoriteMovie>the <i>Star Wars</i> saga</favoriteMovie>
 <favoriteMovie>the <i>Godfather</i> trilogy</favoriteMovie>
<favoriteMovies>

Upvotes: 1

Related Questions