Structuring the XML output.
Note: This post will be used as reference for several posts on XSLT.
If you just want to do a simple transformation where you do the same thing over and over again when a template hits, you don't really need to call templates.
Say you have a long list of records (the music kind) and you want to extract the ones by Michael Jackson, you can simply perform an if-statement inside the template.
- <!-- Given a structure like this one... -->
- < recordCollection >
- <record>
- <artist>Madonna</artist>
- <title>Like a prair</title>
- </record>
- <record>
- <artist>Gorillaz</artist>
- <title>Gorillaz</title>
- </record>
- <record>
- <artist>Madcon</artist>
- <title>Beggin</title>
- </record>
- <record>
- <artist>Michael Jackson</artist>
- <title>Bad</title>
- </record>
- <record>
- <artist>Queen</artist>
- <title>Jazz</title>
- </record>
- <record>
- <artist>Muse</artist>
- <title>The Resistance</title>
- </record>
- <record>
- <artist>Duffy</artist>
- <title>Rockferry</title>
- </record>
- </ recordCollection >
- <!-- Then you would want to have a simple template handling all records and filtering out the ones matching the artist you look for and list the artists name and album. -->
- <xsl:template match="record">
- <xsl:if test="./artist='Michael Jackson'">
- Artist: <xsl:value-of select="./artist" />
- Album: <xsl:value-of select="./title" />
- </xsl:if>
- </ xsl:template >
Comments
Post a Comment