|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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. |
|
#2
|
|||
|
|||
|
Code:
<xsl:apply-templates select="body"/> <xsl:template match="para"> <p><xsl:value-of select="."/></p> </xsl:template> Give that a try. |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > XSL transformation isn't going well |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|