PDA

View Full Version : Output nested elements in series


mindlessLemming
12-13-2004, 12:35 AM
Me again :)
Ok, first, here's an abridged version of my XML:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="text2.xsl" alternate="no" ?>
<texts>
<text id="t001">
<name>The Tortoise and the Hare</name>
<content>The <n>Hare</n> <v>ran</v> past the <adj>lazy</adj> <n>Tortoise</n>.</content>
</text>
</texts>


What I'm trying to do, is output the <content> element as a paragraph and each of the <n>,<v> and <adj> elements as spans within the <p>.
Here's my XSL:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="texts">
<html>
<head>
<title>Text Test</title>
</head>
<body>
<xsl:for-each select="text">
<div class="text" id="{@id}">
<h2><xsl:value-of select="name"/></h2>
<xsl:apply-templates select="content"/>
</div>
</xsl:for-each>

</body>
</html>
</xsl:template>

<xsl:template match="content">
<p><xsl:apply-templates/></p>
</xsl:template>

<xsl:template match="n">
<span class="noun"><xsl:value-of select="n"/></span>
</xsl:template>

<xsl:template match="v">
<span class="verb"><xsl:value-of select="v"/></span>
</xsl:template>

<xsl:template match="adj">
<span class="adj"><xsl:value-of select="adj"/></span>
</xsl:template>

</xsl:stylesheet>


Unfortunately, it doesn't output any of the tags nested within <content>.
Any help would be greatly appreciated (of course! :))

hemebond
12-13-2004, 01:00 AM
<xsl:template match="n">
<span class="noun"><xsl:value-of select="."/></span>
</xsl:template>

<xsl:template match="v">
<span class="verb"><xsl:value-of select="."/></span>
</xsl:template>

<xsl:template match="adj">
<span class="adj"><xsl:value-of select="."/></span>
</xsl:template>http://www.w3schools.com/xsl/xsl_apply_templates.asp

mindlessLemming
12-13-2004, 06:17 AM
Thanks hemebond :)
I found the answer, but I'm at work and got dragged off to a meeting before I could edit my post. That seems to be my technique with XSL problems - Search for answer, post on CF, find answer almost immedietly afterwards. :o

gsnedders
12-13-2004, 04:47 PM
Thanks hemebond :)
I found the answer, but I'm at work and got dragged off to a meeting before I could edit my post. That seems to be my technique with XSL problems - Search for answer, post on CF, find answer almost immedietly afterwards. :o

How about not posting at CF next time... See if you find you're answer anyway... :p

mindlessLemming
12-14-2004, 09:28 AM
No. The answer always comes directly after the act of posting. I could delete my posts, but by supplying the solution I'm leaving something for others who may be searching regarding a similar problem.
I hardly think you should be hassling anyone for excessive posting :p