Re: [xsl] Help with creating Task child of Task

Subject: Re: [xsl] Help with creating Task child of Task
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 02 Jan 2009 16:49:13 -0500
Hi Charles,

Alternatively, do this with modes.

The first time through the document, remove the reference-content by matching it with an empty template.

Then, from the template that directs this traversal from the top, select the reference-content for processing again, matching it in a special-purpose mode to create the new element.

So, something like:

<xsl:template match="task">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <!-- going to copy the taskbody -->
    <xsl:apply-templates/>
    <!-- now going to make any reference-content into a new task -->
    <xsl:apply-templates select="taskbody/reference-content" mode="new-task"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="reference-content"/>
<!-- dropping reference-content when we see it inside the old taskbody -->

<xsl:template match="reference-content" mode="new-task">
  <task>
    <title/>
    <taskbody>
      <context>
        <xsl:apply-templates/>
      </context>
    </taskbody>
  </task>
</xsl:template>

... plus the identity template to copy everything else.

Keep in mind that while it may appear that you are "opening" and "closing" elements, what you're really doing is grafting stuff onto the result tree. Using a second mode gives you a way of saying "don't attach it here" in one place and "attach it here" in another place.

Cheers,
Wendell

At 02:00 PM 12/31/2008, you wrote:
I'm creating an XML to XML transform. Transforming a kluged DITA model into standard DITA. I have to transform something called <reference-content> into a <task> child of the root task.

I can easily transform <reference-content> to <task>. Where I'm stuck is making the new task a sibling of the currently containing <taskbody>. Currently <reference-content> is a child of <taskbody>. Once <reference-content> is tranformed to <task>, I need to close the containing <taskbody> and make the new <task> a sibling of that <taskbody>.


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread