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 October 21st, 2003, 09:55 AM
webgeekinva's Avatar
webgeekinva webgeekinva is offline
$ignorance == $bliss
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Southeast Virginia
Posts: 41 webgeekinva User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via AIM to webgeekinva Send a message via Yahoo to webgeekinva
XSL transformation isn't going well

I'm just getting started with XML and XSL and I'm having a bit of trouble with it.

Here's my XML file:

Code:
<entry>
  <author>michael</author>
  <date>20031020</date>
  <title>Lorem Ipsum</title>
  <body>
    <para>...</para>
    <para>...</para>
    <para>...</para>
  </body>
</entry>


Simple, right? No attributes even. What I'm basically looking to do is set each different element into it's own div, except for the <para>'s, which I want in <p> tags. Here's what I tried:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0"
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
  
  <xsl:template match="/">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
      <head>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
  
        <title>XML Journal</title>
      </head>
      <body>
        <div><xsl:value-of select="title"/></div>
        <div><xsl:value-of select="date"/></div>
        <div><xsl:apply-templates /></div>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="body">
    <xsl:for-each select="entry/body">
      <p><xsl:value-of select="para"/></p>
    </xsl:for-each>
  </xsl:template>
</xsl:transform>


However, it's not working out quite as I planned. Here's the output (via PHP):

Code:
<?xml version="1.0" encoding="UTF-8"?><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/><title>XML Journal</title></head><body><div></div><div></div><div>
  michael
  20031020
  Lorem Ipsum
  <p xmlns="">
    ... ... ...
  </p>
</div></body></html>


I'm not exactly sure what's going on here and any advice would be appreciated.
__________________
In the beginning, the universe was created. This made a lot of people very angry, and has been widely regarded as a bad idea.

-- Douglas Adams, The Hitchhiker's Guide to the Universe.

Reply With Quote
  #2  
Old October 22nd, 2003, 08:30 AM
imbrokn imbrokn is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: NJ
Posts: 428 imbrokn User rank is Corporal (100 - 500 Reputation Level)imbrokn User rank is Corporal (100 - 500 Reputation Level)imbrokn User rank is Corporal (100 - 500 Reputation Level)imbrokn User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 11 h 34 m 8 sec
Reputation Power: 11
Send a message via AIM to imbrokn
Code:
<xsl:apply-templates select="body"/>
<xsl:template match="para">
   <p><xsl:value-of select="."/></p>
</xsl:template>


Give that a try.

Reply With Quote
  #3  
Old October 22nd, 2003, 09:47 AM
webgeekinva's Avatar
webgeekinva webgeekinva is offline
$ignorance == $bliss
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Southeast Virginia
Posts: 41 webgeekinva User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via AIM to webgeekinva Send a message via Yahoo to webgeekinva
Strange, when I add that, it doesn't work at all. Nothing is returned. When I comment it out, it runs like it did before.

And I still don't understand why the first few fields aren't being put between divs, and those divs are stacking up empty at the beginning of the page. Argh.

Reply With Quote
  #4  
Old December 5th, 2003, 05:44 PM
tsprings tsprings is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Seattle, WA
Posts: 55 tsprings User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 20 sec
Reputation Power: 5
Hello webgeekinva. I believe this should help you. Notice the comments I have included in the XSL document - they should explain everything I did and why. If you have further questions, please post them.

webgeekinva.xml
Code:
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="webgeekinva.xsl" ?>
<entry>
  <author>michael</author>
  <date>20031020</date>
  <title>Lorem Ipsum</title>
  <body>
    <para>blah blah blah</para>
    <para>blah blah blah 2</para>
    <para>blah blah blah 3</para>
  </body>
</entry>


webgeekinva.xsl
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0"
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>  
  <xsl:template match="/entry">    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
      <head>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
        <!-- I added some style just to make sure the divs were being
             implemented. -->
        <style type="text/css">
          #first {color: blue;}
          #second {color: red;}
          #third {color: green;}
          <!--P{background-color: #808080;color: white;}-->
        </style>
        <title>XML Journal</title>
      </head>
      <body>  
        <!-- Notice for the template match I used "/entry".  You had
             only "/" in which case you should have used "/entry/title", 
             "/entry/date", etc. -->   
        <div id="first"><xsl:value-of select="title"/></div>
        <div id="second"><xsl:value-of select="date"/></div>
        <!-- We want to cycle through all of the paragraphs.  So we say 
             for each of the instances of para in body, choose all the 
             content (we do this with ".").-->
        <div id="third">
           <xsl:for-each select="body/para">
             <p><xsl:value-of select="."/></p>
           </xsl:for-each>
        </div>
      </body>
    </html>
  </xsl:template>
</xsl:transform>


Oh, and if you didn't glean it from the XSL, you were having problems because you really weren't grabbing any of the XML in your XSL. It displayed all the data automatically which is why your divs were "stacking up". The reason being that you needed to specifiy exactly where the elements were located in the document, /entry/title (since title was embedded inside entry). Just grabbing title didn't return anything (because it wasn't looking in the right place). If you need further clarification, don't hesitate to ask.

Last edited by tsprings : December 5th, 2003 at 05:48 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > XSL transformation isn't going well


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