Position of child element within mixed content
Posted: Sat Feb 20, 2010 3:27 pm
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.
<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.