Changing attribute value in a nested element

Hi,
I'm trying to find a way to change the attribute value of an element when it is nested inside another elment (which happens to be the context node).

Here is the html doc snippet I am trying to change

<p class="x-author-bio">Some text.</p>

<p><span class="x-first-para">Some text with inline element</span> Rest of the text.</p>

<p class="x01-body-indent-tcbr">Some more text.</p>

I'm using a xsl:choose element to find all the <p> tags and test against a certain criteria.


<xsl:template match="p">
      <xsl:choose>
...
<xsl:when test="./span[@class='x-first-para']">
                          <p> <xsl:copy-of select="node()"/>
                          </p>
                        </xsl:when>
...
</xsl:choose></xsl:template>
<!--this is supposed to change the value of the attribute in the span element-->
    <xsl:template match="span">
        <xsl:attribute name="class"><xsl:value-of select="'article_subhead'"/></xsl:attribute>
    </xsl:template>

When I do the transform, I get
<p><span class="x-first-para">Some text with inline element</span> Rest of the text.</p>
as the output.

I have googled my fingers blue. I am new to XSLT. Any suggestions are greatly appreciated.
PaeonAsked:
Who is Participating?
 
Geert BormansConnect With a Mentor Information ArchitectCommented:
<xsl:copy-of select="node()"/>
does a deep copy
if you want the template for span to have effect you need to apply-templates instead
<xsl:apply-templates select="node()"/>

but in the span template you need to reconstruct the span tag as well, using xsl:copy

<xsl:template match="span">
        <xsl:attribute name="class"><xsl:value-of select="'article_subhead'"/></xsl:attribute>
    </xsl:template>

should be
<xsl:template match="span">
<xsl:copy>
        <xsl:attribute name="class"><xsl:value-of select="'article_subhead'"/></xsl:attribute>
<xsl:copy-of select="node()/>
</xsl:copy>
    </xsl:template>


0
 
PaeonAuthor Commented:
Awesome. Thank you so much.
0
 
Geert BormansInformation ArchitectCommented:
welcome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.