Click to See Complete Forum and Search --> : How to deal with an inline XML element.
Mr Initial Man
11-26-2006, 06:40 PM
I am using an element called <fraction> in my XML. For example, if the fraction is 7 1/2, the element would read <fraction whole="7" num="1" den="2" />. If it was in an element called <pgrph>, how would I use XSL to get it to show up within the text? Like this:
<pgrph>text text text <fraction whole="7" num="1" den="2" /> text text text</pgrph>
Resulting in:
text text text 7 1/2 text text text
Assuming you have a template to match <pgrph>, ensure it has an apply-templates element and thus process any child nodes of pgrph. Now create a template for fraction and output as desired:
<xsl:template match="pgrph">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="fraction">
<xsl:value-of select="concat(@whole,' ',@num,'/',@den)"/>
</xsl:template>
Mr Initial Man
11-27-2006, 11:01 AM
Okay, that works. But what if I want to add some markup? Here's what I had in mind:
<xsl:template match="fraction">
<xsl:value-of select="concat(@whole,' ')" />
<sup><xsl:value-of select="concat(@num,'')" /></sup>/
<sub><xsl:value-of select="concat(@den,'')" /></sub>
</xsl:template>
Adding the <sub> and <sup> tags make a complete bollocks of the whole thing. :(
Mr Initial Man
12-05-2006, 02:26 PM
Anyone have an answer?
Charles
12-09-2006, 08:36 AM
I'm not sure why that's not working for you but I would use something more like:<xsl:template match="fraction">
<xsl:value-of select="@whole" />
<xsl:text> </xsl:text>
<sup><xsl:value-of select="@num" /></sup>
<xsl:text>/</xsl:text>
<sub><xsl:value-of select="@den" /></sub>
</xsl:template>
Mr Initial Man
12-09-2006, 05:50 PM
Unfortunately, when I have the <sup>..</sup> and <sub>...</sub> elements in there, all I get is plain text.
Charles
12-11-2006, 05:56 AM
It's hard to tell just what's going on and as I've noted elsewhere, I'm too darn lazy to go and set up both files to run some tests. Please post a URL pointing to what you have so far.
Ultimater
12-13-2006, 02:34 AM
Mr Initial Man had the right idea so I expanded upon it.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="pgrph">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="fraction">
<xsl:value-of select="concat(@whole,' ')" />
<sup><xsl:value-of select="concat(@num,'')" /></sup>/
<sub><xsl:value-of select="concat(@den,'')" /></sub>
</xsl:template>
</xsl:stylesheet>