RE: [xsl] Moving an element to a new location in the Result-tree

Subject: RE: [xsl] Moving an element to a new location in the Result-tree
From: "Graves, Jim \(CONTR\)" <jim.graves@xxxxxxx>
Date: Wed, 7 Sep 2005 17:21:40 -0700
Yes, I'm probably am thinking in a procedural manner, more than
50%/time.
Please let me clarify my problem with more details:

Let's suppose my source document looks like this: (note: repeating "b1"
elements)

Source Tree:
<document1>
  <a/>
  <b1/>
    <b1data1>XYZ1</b1data1>
    <b1data2>XYZ2</b1data2>
  </b1>
  <b1>
     <b1data1>ABC1</b1data1>
     <b1data2>ABC2</b1data2>
  </b1>
  <b1>
     <b1data1>PDQ1</b1data1>
     <b1data2>PDQ2</b1data2>
  </b1>
  <c/>
  <d/>
  <e/>
</document1>

I want to move the former "b1" elements down the tree and re-name them
(all) along the way, yet maintain the actual data they contain ("XYZ1",
etc.).  The main thing is to move them down there, not re-name them.

The reason is that the XML file must conform to a schema that expects
the "b1" elements to be down there (after the "e" element) not between
the "a" and "b" elements. The XSLT processor follows the linear
sequence, yet I need it to 'skip ahead' and put the "b1" elements into a
part of the result tree that does not yet exist -- hence, the second
transform is needed (or is it). What method works best?

Result Tree:
<document222>
  <a/>
  <c/>
  <d/>
  <e/>
  <new_name_for_b1/>
    <b1data1>XYZ1</b1data1>
    <b1data2>XYZ2</b1data2>
  </ new_name_for_b1>
  < new_name_for_b1>
     <b1data1>ABC1</b1data1>
     <b1data2>ABC2</b1data2>
  </ new_name_for_b1>
  <new_name_for_b1>
     <b1data1>PDQ1</b1data1>
     <b1data2>PDQ2</b1data2>
  </new_name_for_b1>
</document222>

Ideas?

~Jim

-----Original Message-----
From: Christopher R. Maden [mailto:crism@xxxxxxxxx]
Sent: Wednesday, 07 September 2005 16:54
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Moving an element to a new location in the
Result-tree

Contra Jim Graves writes:
> What is the recommended method to move an element to a Result-tree
> location that will be created, yet has not yet been created, (using
> copy, & copy-of)? Common sense would indicate that I need to run a
> second XSLT transform -- once the first version of the tree has been
> created -- or is there a better way, such as building the tree's
> elements first, then populating them with data, in a following
template
> in the first and only transform? Since I usually build the element and
> populate it then too, when I'm there, I think I'll need a second XSLT.

I think you may be falling prey to the same procedural trap discussed
earlier today on this list.

If I have this input tree:

<document>
  <a/>
  <b/>
</document>

and I want to put a inside of b, I can do this:

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="a"/>

<xsl:template match="b">
  <xsl:copy>
    <xsl:apply-templates select="../a" mode="inside"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="a" mode="inside">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

which results in:

<document>
  <b><a/></b>
</document>

~Chris
--
Christopher R. Maden, Principal Consultant, crism consulting
XML-SGML-HTML-DTDs-schemas-XSL-DSSSL-conversion-training-ebooks-B2B
<URL: http://crism.maden.org/consulting/ >
PGP Fingerprint: BBA6 4085 DED0 E176 D6D4  5DFC AC52 F825 AFEC 58DA

Current Thread