954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Extract inner element to own paragraph

Hi,

I'm trying to extract an inner node and place it at the level
as the element . 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 , 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

skuip
Newbie Poster
3 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

<?xml version="1.0" encoding="UTF-8"?>

{.}

JohnBampton
Newbie Poster
15 posts since Feb 2011
Reputation Points: 10
Solved Threads: 6
 

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>
skuip
Newbie Poster
3 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

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 . 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 , 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

ibakir
Light Poster
26 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 
skuip
Newbie Poster
3 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: