Position of child element within mixed content

Here should go questions about transforming XML with XSLT and FOP.
ra0543
Posts: 80
Joined: Wed Jan 14, 2009 12:50 pm

Position of child element within mixed content

Post by ra0543 »

I have data like the following (example 1):

<a>text <b>more text</b> text</a>

Sometimes the <b> child element occurs right at the start of the parent <a> (example 2):

<a><b>more text</b> text</a>

or right at the end (example 3):

<a>text <b>more text</b></a>

I want a template to match "a/b" which will let me treat these three scenarios differently; specifically I want to be able to replace the <b> child with different (or no) text depending on whether it is in the middle (as in 1) or at the start (2) or the end (3). For instance, I might want the result to be:

1: <a>text XXXXX text</a>
2: <a> text</a>
3: <a>text YYY</a>

I can do 1 easily:

<xsl:template match="a/b"><xsl:text>XXXXX</xsl:text></xsl:template>

But I'm not sure how to modify this to do 2 and 3. I guess this is easy, but I can't quite work out how to do it.
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: Position of child element within mixed content

Post by george »

You can use something like below

Code: Select all


  <xsl:template match="a/b[not(preceding-sibling::node())]" priority="10">
<xsl:text>FIRST</xsl:text>
</xsl:template>

<xsl:template match="a/b[not(following-sibling::node())]" priority="5">
<xsl:text>LAST</xsl:text>
</xsl:template>

<xsl:template match="a/b">
<xsl:text>MIDDLE</xsl:text>
</xsl:template>
Best Regards,
George
George Cristian Bina
Post Reply