Hi,

I'm trying to extract an inner node and place it at the level
as the element <para>. However I'm unable to figure it out.

Source XML:

<para>
	Lorem ipsum dolor sit amet, <i>consectetur</i>
	adipiscing elit. Aenean vestibulum pharetra metus.
	<formula altimg="fig1">E = mc^2</formula>
	Curabitur ac nibh purus, eget molestie dui. Integer
	vulputate porta volutpat.
</para>

Expected result:

<p>
	Lorem ipsum dolor sit amet, <i>consectetur</i>
	adipiscing elit. Aenean vestibulum pharetra metus.
</p>
<p class="formula">
	<img src="fig1.png" alt="E = mc^2" />
</p>
<p>
	Curabitur ac nibh purus, eget molestie dui. Integer
	vulputate porta volutpat.
</p>

I tried something like the following, but this will move the formules to the
end of the paragraph and thats not what I want. I also tried to loop over all the elements in the <para>, but then I can't figure out how to group the text nodes to one paragraph.

Source XSLT:

<xsl:template match="para">
	<p>
		<xsl:apply-templates select="node()[not(name() = 'formula')]" />
	</p>
	<xsl:apply-templates select="node()[name() = 'formula']" />
</xsl:template>

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

<xsl:template match="formula">
	<p class="formula">
		<img>
			<xsl:attribute name="src">
				<xsl:value-of select="@altimg" />
				<xsl:text>.png</xsl:text>
			</xsl:attribute>
			<xsl:attribute name="alt">
				<xsl:apply-templates />
			</xsl:attribute>
		</img>
	</p>
</xsl:template>

--
Stefan Kuip

Recommended Answers

All 4 Replies

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   
    version="1.0">
    <xsl:template match="/para">
        <p>
            <xsl:value-of select="text()[1]"/>
            <xsl:apply-templates select="i"></xsl:apply-templates>
            <xsl:value-of select="text()[2]"/>
        </p>
        <xsl:apply-templates select="formula"></xsl:apply-templates>
        <p>
            <xsl:value-of select="text()[3]"/>
        </p>
    </xsl:template>
    <xsl:template match="node()"><xsl:copy-of select="."/></xsl:template>
    <xsl:template match="formula">
        <p class="formula">
            <img src="{concat(@altimg,'.png')}" alt="{.}" />
        </p>
    </xsl:template>
</xsl:stylesheet>

Yep, that's working. But I probably should have mentioned that the content is dynamic and could have multiple sentences with formatting with zero or more formulas. So any xslt which knows how many text nodes there are will not work.

Basically I want to pull out the formula's and put them in their own block. Thanks anyway I think I have to go for the following solution for now.

<p>
	Lorem ipsum dolor sit amet, <i>consectetur</i>
	adipiscing elit. Aenean vestibulum pharetra metus.

	<span class="formula" style="display:block">
		<img src="fig1.png" alt="E = mc^2" />
	</span>

	Curabitur ac nibh purus, eget molestie dui. Integer
	vulputate porta volutpat.
</p>

hey, if you please could help me with a lil prob here . if we can chat online, or take full control on my desktop. I'll be thankful man

Hi,

I'm trying to extract an inner node and place it at the level
as the element <para>. However I'm unable to figure it out.

Source XML:

<para>
	Lorem ipsum dolor sit amet, <i>consectetur</i>
	adipiscing elit. Aenean vestibulum pharetra metus.
	<formula altimg="fig1">E = mc^2</formula>
	Curabitur ac nibh purus, eget molestie dui. Integer
	vulputate porta volutpat.
</para>

Expected result:

<p>
	Lorem ipsum dolor sit amet, <i>consectetur</i>
	adipiscing elit. Aenean vestibulum pharetra metus.
</p>
<p class="formula">
	<img src="fig1.png" alt="E = mc^2" />
</p>
<p>
	Curabitur ac nibh purus, eget molestie dui. Integer
	vulputate porta volutpat.
</p>

I tried something like the following, but this will move the formules to the
end of the paragraph and thats not what I want. I also tried to loop over all the elements in the <para>, but then I can't figure out how to group the text nodes to one paragraph.

Source XSLT:

<xsl:template match="para">
	<p>
		<xsl:apply-templates select="node()[not(name() = 'formula')]" />
	</p>
	<xsl:apply-templates select="node()[name() = 'formula']" />
</xsl:template>

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

<xsl:template match="formula">
	<p class="formula">
		<img>
			<xsl:attribute name="src">
				<xsl:value-of select="@altimg" />
				<xsl:text>.png</xsl:text>
			</xsl:attribute>
			<xsl:attribute name="alt">
				<xsl:apply-templates />
			</xsl:attribute>
		</img>
	</p>
</xsl:template>

--
Stefan Kuip

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.