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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gertone (Geert Bormans)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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PaeonAuthor Commented:
Awesome. Thank you so much.
0
Gertone (Geert Bormans)Information ArchitectCommented:
welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.