PDA

View Full Version : XSLT: Transforming variable numbers of paragraphs


ppeter
11-08-2004, 12:37 PM
Hi folks,

This will probably sound very very newbie, but I am an XSLT newb...

Say I have my XML file like so:
<myxml>
<content>
<heading>Heading 1</heading>
<subheading>Heading 2</subheading>
<paragraph>This is some content. Foo bar, foo bar, foo bar...</paragraph>
<paragraph>Wooo another paragraph! This is getting repetitive...</paragraph>
<heading>Another Heading 1</heading>
<paragraph>YEAH!!!!!!!!!! Oh, right...</paragraph>
</content>
</myxml>

and I have an XSL template something like this....

<xsl:for-each select="heading">
<h1><xsl:value-of select="heading"></h1>
</xsl:for-each>
<xsl:for-each select="subheading">
<h2><xsl:value-of select="subheading"></h2>
</xsl:for-each>
<xsl:for-each select="paragraph">
<p><xsl:value-of select="paragraph"></p>
</xsl:for-each>

Now, I realise that will chuck out 2 <h1> tags followed by one <h2> tag and then my 3 <p> tags.... but how do I get them 'chucked out' in the order in which they appear in the XML file??

Hope that makes sense... sorry for the poor example.

mpjbrennan
11-14-2004, 02:23 PM
Use this stylesheet. I have added some styling to the output to make things more clear (I hope).

patrick

XSL file
--------------
<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html"/>

<xsl:template match="/">

<html style="background:#ffffc6">

<body>

<xsl:apply-templates select="*"/>

</body>

</html>

</xsl:template>

<xsl:template match="heading">

<h1 style="font-weight:bold; font-size:14pt; font-family:verdana, helvetica, arial, sans-serif;; color:blue; text-align:center">

<xsl:value-of select="."/>

</h1>

</xsl:template>

<xsl:template match="subheading">

<h2 style="font-weight:bold; font-size:12pt; font-family:verdana, helvetica; color:maroon; text-align:left">

<xsl:value-of select="."/>

</h2>

</xsl:template>

<xsl:template match="paragraph">

<p style="color:red; font-weight:normal; font-size:10pt; font-family:verdana, helvetica; text-align:left">

<xsl:value-of select="."/>

</p>

</xsl:template>

</xsl:stylesheet>

ppeter
11-14-2004, 06:26 PM
Thanks a lot! :thumbsup:
I'll try it out