XSL/XPath ancestor question

C

Colin Fox

Hi, all.

Within an xsl template, you can use the <xsl:attribute> tag to set an
attribute of the current output node. This is great.

However, I need to be able to set an attribute of an ancestor node.

Here's the relevant part of my xsl:

<xsl:template match="myform:field" mode="single">
<xsl:choose>
<xsl:when test="@type='htmltextarea'">
<xsl:attribute name="{../../onsubmit}">submitEditForm()</xsl:attribute>

This doesn't work -- the parser rejects the "../../" syntax on the name.
Also, this particular technique (even if it worked) is fragile, because
what I really want to do is to set the 'onsubmit' attribute of the Form
parent element, which is at a potentially arbitrary level above the
current node.

So -- is there an XPath selector that I can use in the name field of the
attribute node to set the value of the Form parent, or do I have to do a
select at this point and select the parent? And if so, what would that
select look like? (I haven't done any backwards search selectors yet).

Thanks in advance,
cf
 
J

Johannes Koch

Colin said:
Hi, all.

Within an xsl template, you can use the <xsl:attribute> tag to set an
attribute of the current output node. This is great.

However, I need to be able to set an attribute of an ancestor node.

Here's the relevant part of my xsl:

<xsl:template match="myform:field" mode="single">
<xsl:choose>
<xsl:when test="@type='htmltextarea'">
<xsl:attribute name="{../../onsubmit}">submitEditForm()</xsl:attribute>

You should make the xsl:choose on the grandparent element, and add the
attribute in the template for the grandparent.

<xsl:template match="myform:form">
<form>
<xsl:choose>
<xsl:when test="foo:bar/myform:field/@type='htmltextarea'">
...
</xsl:template>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top