xslt text with inline elements

Here should go questions about transforming XML with XSLT and FOP.
gjledger2k
Posts: 16
Joined: Tue Aug 01, 2006 2:56 am
Location: Chicago

xslt text with inline elements

Post by gjledger2k »

Hi, I have a layout program that can accommodate the following pattern:

<element1>text1<element2>text2<element2>text3<element1>
I'm assuming that text1 and text3 as text nodes are siblings of element2.

What I need to do is match element 1, create an output element with the value of its attribute, output the first text node, select element2, create an output element with the value of its attribute, and output the final text node, as if it were all one paragraph.

Here is the transform I've been working on:

Code: Select all


<xsl:template match="text:p">
<xsl:for-each select=".">
<xsl:element name="{@text:style-name}">
<xsl:value-of select="text()"/>
<xsl:apply-templates select="text:span"/>
</xsl:element>
</xsl:for-each>

</xsl:template>
<xsl:template name="text:span">
<xsl:element name="{@text:style-name}">
<xsl:value-of select="self::node()"/>
</xsl:element>
</xsl:template>
I also tried using a choose/when (when the child of text:p is a text node, output the text node; when the child of text:p is an element, create a new element....

Argh, please help. What am I doing wrong? (neither Jeni nor Michael or Bob addresses this.)
gjledger2k
Posts: 16
Joined: Tue Aug 01, 2006 2:56 am
Location: Chicago

Re: xslt text with inline elements

Post by gjledger2k »

Okay, if you keep chipping away, suddenly the transform does what you wanted.

So here is the working transform steps:

Code: Select all



<xsl:template match="text:p">
<xsl:for-each select=".">
<xsl:element name="{@text:style-name}">
<xsl:apply-templates/>
</xsl:element>

</xsl:for-each>

</xsl:template>

<xsl:template match="text:p/node()">
<xsl:for-each select=".">
<xsl:choose>
<xsl:when test="self::text()">
<xsl:value-of select="."/>
</xsl:when>
<xsl:when test="self::text:span">
<xsl:element name="{@text:style-name}">
<xsl:value-of select="."></xsl:value-of>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
Now, say there is another span element inside a span element that also has text nodes. Is there a simple way to do this; to wit: how might I invoke a recursive call??
Post Reply