amelimcc
amelimcc

Reputation: 1

How do I apply italics to a tag inside an other tag in xsl?

I want to italicize the text between the tags (see code below) of my xml file via xsl. The <stage> tags are inside <p> tags. All text which is in <p> but not in <stage> should remain upright.

xml :

<div>
<sp>
    <p>Son cœur .... <stage>Chérubin arrache le ruban.</stage> Ah,
    le ruban!</p>
</sp>
</div>

As the <stage> tags are children of the <p> tags, I don't know how to apply the italics to the words "Chérubin arrache le ruban." only.

Here is the xsl I have tried for now:

<xsl:template match="div">   
    <xsl:for-each select="sp">
        <xsl:value-of select="speaker"/>
        <xsl:if test="stage!=''">
            <i><xsl:value-of select="stage"/></i>  
        </xsl:if>   
        <xsl:value-of select="p"/>      
    </xsl:for-each>
</xsl:template>

The problem with this is it only italicizes <stage> when it is outside the <p> tag, so in the example above it does not work.

I also tried using the substring-before/after method (https://www.peachpit.com/articles/article.aspx?p=1315438&seqNum=7) :

<xsl:value-of select="substring-before(., '&lt;stage')" />
<i><xsl:value-of select="stage"/></i>
<xsl:value-of select="substring-after(.,'&lt;/stage')" />

but still wasn't able to make it work.

Do you have a suggestion?

Upvotes: 0

Views: 165

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167436

The basic approach is a template for stage:

<xsl:template match="stage">
  <i>
    <xsl:apply-templates/>
  <i>
</xsl:template>

You can handle the other elements with separate templates, just make sure that the parent or ancestor templates keep processing child nodes by using <xsl:apply-templates/>.

Upvotes: 0

Related Questions