[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Find the node name of the parent in the result tree?

Subject: Re: [xsl] Find the node name of the parent in the result tree?
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 22 Mar 2013 17:23:19 -0400

At 2013-03-22 14:14 -0700, Martin Holmes wrote:
If I have a template matching an attribute, and producing one in the output tree, like this:

  <xsl:template match="@style">
        <xsl:attribute name="style" select="."/>

The above could be done with:

  <xsl:template match="@style">
     <xsl:copy-of select="."/>

Is there any way to know the name of the element in the result tree which is the parent of the attribute being created?

In the result tree? No. It is write-only and cannot be queried. You'd have to keep track of what you are doing.

Some context: I'm turning TEI @style attributes into HTML @style attributes in the output, and I'd like to handle situations in which this kind of input:

<hi rend="text-align: center;">Centred text</hi>

results in output that doesn't work:

<span style="text-align: center;">NOT centred because it's a span</span>

If I knew the output element was a <span> or element which is inline by default, I could add "display: block" automatically to any @style attribute that contains a block-level CSS property such as text-align. I don't want to add "display: block" in all cases, because e.g. a <div> element might already have a class which floats it.

It is a challenge but you have to keep track yourself.

When using XSLT 2.0 something I've done is I've added a tunnel parameter to the apply-templates stack frame indicating that I'm in a DIV and so act on it later when it comes time to do the style attribute ... something along the lines of:

  <xsl:template match="some-block-element">
        <xsl:with-param name="in-a-div" tunnel="yes" select="true()"/>

  <xsl:template match="@style">
    <xsl:param name="in-a-div" tunnel="yes" select="false()"/>
    <xsl:attribute name="style"
      select="concat(.,if( $in-a-div ) then ';display:block' else'')"/>

Then you just have to remember to add that tunnel parameter for each of the blocks you create so that the processing knows that was done "way back when".

I hope this helps.

. . . . . . . . Ken

-- Public XSLT, XSL-FO, UBL and code list classes in Europe -- Apr 2013 | Contact us for world-wide XML consulting and instructor-led training | Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm | Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ | G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx | Google+ profile: https://plus.google.com/116832879756988317389/about | Legal business disclaimers: http://www.CraneSoftwrights.com/legal |

Current Thread