|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
XSL to select part of XML
I'm sure this is probably simple and I'm just out of practice and lazy as well :), but here goes... I want to be able to display only an individual <class> based on what I specify in the XSL (eventually replaced by a parameter sent by some php (horse before carriage though). Anyway, here's a sample of xml and xsl. Hack and criticize away, I await your great wisdom :)
(I don't think this is going to paste well) So for example, say I *just* want to list the Sorceror spells... (As I think about it, I would probably want to list all the <class>'s spells in their own table (as a seperate thing as well). Thanks! <?xml version='1.0'?> <classes> <class> <name>Sorceror</name> <spells> <spell> <name>Komiza</name> <desc>Dart of Ice</desc> <mana>1</mana> <price>10 gold</price> <level>1st</level> <rlevel>1st</rlevel> </spell> <spell> <name>Toduza</name> <desc>Dart of Flame</desc> <mana>2</mana> <price>20 gold</price> <level>3rd</level> <rlevel>5th</rlevel> </spell> </spells> </class> <class> <name>Acolyte</name> <spells> <spell> <name>Motu</name> <desc>Minor Healing</desc> <mana>1</mana> <price>10 gold</price> <level>1st</level> <rlevel>1st</rlevel> </spell> </spells> </class> </classes> ------ <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'> <xsl:output method="html"/> <xsl:template match="/"> <table border="1"> <tr><th colspan="5"><xsl:value-of select="classes/class/name" /></th></tr> <xsl:apply-templates select="classes/class/spells" /> <tr><th colspan="5"><xsl:value-of select="classes/class/name" /></th></tr> </table> </xsl:template> <xsl:template match="class/spells/spell"> <tr> <td><xsl:value-of select="name" /></td> <td><xsl:value-of select="desc" /></td> <td><xsl:value-of select="price" /></td> <td><xsl:value-of select="level" /></td> <td><xsl:value-of select="rlevel" /></td> </tr> </xsl:template> </xsl:stylesheet> |
|
#2
|
|||
|
|||
|
Well, you can add an <xsl:if>. Substitue with your parameter when you get that far.
Code:
<xsl:template match="/">
<xsl:if test="classes/class/name = 'Sorceror'">
<table border="1">
<tr>
<th colspan="5">
<xsl:value-of select="classes/class/name" />
</th>
</tr>
<xsl:apply-templates select="classes/class/spells" />
<tr>
<th colspan="5">
<xsl:value-of select="classes/class/name" />
</th>
</tr>
</table>
</xsl:if>
</xsl:template>
|
|
#3
|
|||
|
|||
|
Thanks for replying. I tried that, and it still displays the Acolyte spells as well.
Before I read your reply, I tried this... (both give similar results (minus the formatting). <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'> <xsl:output method="html"/> <xsl:template match="/"> <xsl:if test="classes/class/name = 'Sorceror'"> <table border="1"> <tr> <th colspan="5"> <xsl:value-of select="classes/class/name" /> </th> </tr> <xsl:for-each select="classes/class/spells/spell"> <tr><td> <xsl:value-of select="name" /> </td></tr> </xsl:for-each> <tr> <th colspan="5"> <xsl:value-of select="classes/class/name" /> </th> </tr> </table> </xsl:if> </xsl:template> </xsl:stylesheet> Last edited by janzik : September 18th, 2003 at 06:04 PM. |
|
#4
|
|||
|
|||
|
Ok, here you go. I actually paid attention to the output this time ;).
Code:
<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'> <xsl:output method="html"/> <xsl:template match="/"> <xsl:apply-templates select="classes/class[name='Sorceror']"/> </xsl:template> <xsl:template match="/classes/class"> <table border="1"> <tr><th colspan="5"><xsl:value-of select="./name" /></th></tr> <xsl:apply-templates select="./spells" /> <tr><th colspan="5"><xsl:value-of select="./name" /></th></tr> </table> </xsl:template> <xsl:template match="class/spells/spell"> <tr> <td><xsl:value-of select="name" /></td> <td><xsl:value-of select="desc" /></td> <td><xsl:value-of select="price" /></td> <td><xsl:value-of select="level" /></td> <td><xsl:value-of select="rlevel" /></td> </tr> </xsl:template> </xsl:stylesheet> |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > XSL to select part of XML |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|