XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 18th, 2003, 01:57 PM
janzik janzik is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 4 janzik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to janzik
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>

Reply With Quote
  #2  
Old September 18th, 2003, 04:43 PM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
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>

Reply With Quote
  #3  
Old September 18th, 2003, 06:02 PM
janzik janzik is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 4 janzik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to janzik
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.

Reply With Quote
  #4  
Old September 18th, 2003, 10:33 PM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
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>

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > XSL to select part of XML


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT