[XSLT 1.0]
I have an xml doc like this :
<doc>
<article>
<photo>
<caption>
<p>
caption text here with <i>italic</i> here and <b>bold</b> here<credit>photo
copyright</credit>
</p>
</caption>
</photo>
</article>
</doc>
So the <credit>
tag is nested in the <p>
tag.
I have templates to copy the <i>
and <b>
tags, like this :
<xsl:template match="i">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="b">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
What I need to get in the output is : the caption text with <i>
and <b>
tags, and then the photo copyright. I also need to have a line-break between both (I mean between caption text and photo copyright) ; and I don’t want to repeat the photo copyright (I don’t want to have it 2 times, in both lines in the output).
I can do this :
<xsl:apply-templates select="caption/p/text()"/>
<xsl:value-of select="caption/p/credit"/>
… but here I’m loosing my <i>
and <b>
tags, which I do need in my output… And I have no line-break.
I can also do this :
<xsl:apply-templates select="caption/p"/>
<xsl:value-of select="caption/p/credit"/>
… but here I have the photo copyright 2 times. And I have no line break either…
So I was looking for some trick to : 1/ keep my <i>
and <b>
tags – 2/ get a line break between caption text and photo copyright – 3/ get rid of the <credit>
node in my caption text output.
And I did find a way. In my xsl now I just have :
<xsl:apply-templates select="caption/p"/>
<xsl:value-of select="caption/p/credit"/>
Just like above… but I added a template like this :
<xsl:template match="credit">
<xsl:text>
</xsl:text>
</xsl:template>
And… I must say, this is working, in a way, as I do keep my <i>
and <b>
tags, I do get a line break between caption text and photo copyright, and I do get rid of the <credit>
node in my caption text output. Problem is : I feel this is quite awkward…
So please how would you do this in a better way ? I’m new to xsl and I have no idea…