Skip to content

Instantly share code, notes, and snippets.

@redknitin
Created October 5, 2015 06:11
Show Gist options
  • Select an option

  • Save redknitin/b89eb6292b04826c0cad to your computer and use it in GitHub Desktop.

Select an option

Save redknitin/b89eb6292b04826c0cad to your computer and use it in GitHub Desktop.
XSLT test
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="movies.xsl"?>
<movies>
<movie name="Pirates of the Caribbean" />
<movie name="Lord of the Rings" />
</movies>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html><head>
</head><body>
<xsl:for-each select="movies/movie">
<div>Name: <xsl:value-of select="@name" /></div>
<!-- xsl:if test="price>10" -->
<!-- xsl:choose xsl:when xsl:otherwise -->
</xsl:for-each>
</body></html>
</xsl:template>
</xsl:stylesheet>
@indiamcq
Copy link
Copy Markdown

indiamcq commented Nov 5, 2015

Here are my suggested changes. Tested with Saxon9.XSLT 2 not 1. HTML5 output.

What you have works. But a second template for movies is preferable over for-each.

Ian

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="5.0" encoding="utf-8" omit-xml-declaration="no" indent="yes" />
<xsl:strip-space elements="*"/>
      <xsl:template match="/*">
            <!-- I prefer the generic * for the root element instead of <xsl:template match="/movies">, then you are never wrong. Harder to misspell * than a word.  -->
            <html>
                  <head>
                        <title>Movies</title>
                  </head>
                  <body>
                        <!-- I am not a XSLT pro, but believe this is the preferred way of getting child elements, it is much more modular and easy to expand. -->
                       <xsl:apply-templates /> 
                        <!-- or  <xsl:apply-templates select="movie"/> -->
                  </body>
            </html>
      </xsl:template>
      <xsl:template match="movie">
            <div>Name: <xsl:value-of select="@name" /></div>
            <!-- xsl:if test="price>10" -->
            <!-- xsl:choose xsl:when xsl:otherwise -->
      </xsl:template>
</xsl:stylesheet>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment