Reputation: 129
I have an issue where I need to loop through a lot of lines and format particular items in each line which correspond to the word item.
I am able to loop and figured out how to apply the formatting. But my issue is: the item appears at the end of the line when I apply the XSLT transform.
Below I have included my code.
I am using the XML functions for-each
, value-of
and if
.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL /Transform">
<xsl:template match="/">
<html>
<body>
<h2>Title</h2>
<xsl:for-each select="song/l">
<p>
<xsl:value-of select="current()"/>
<xsl:if test="verb">
<span style="color:#ff0000">
<i><xsl:value-of select="verb"/></i>
</span>
</xsl:if>
</p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
My input is:
<?xml version="1.0" encoding="UTF-8"?>
<song>
<l n="1">The fox <verb>jumps</verb> up high</l>
<l n="2">The frog is greener than a forest.</l>
</song>
What I am hoping to achieve is:
For example, in a line such as
"The Brown fox jumps up high"
The word jump
is labeled as a verb and should be in a different color and italicized.
My code right now displays
"The Brown fox jumps up high. jumps"
Formatted correctly.
I need it to keep jumps in the same position in the line as is.
Any help or advice appreciated.
Upvotes: 0
Views: 31
Reputation: 12075
This is really just a simple case of applying an appropriate template to your various elements, like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Title</h2>
<xsl:apply-templates select="song/l"/>
</body>
</html>
</xsl:template>
<xsl:template match="verb">
<span style="color:#ff0000; font-style:italic;">
<xsl:apply-templates/>
</span>
</xsl:template>
<xsl:template match="l">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
</xsl:stylesheet>
(I tweaked it slightly to use font-style:italic;
rather than the <i/>
element, but you could easily just put the <i>
just inside the span)
Upvotes: 1